Building Java Docker Images
Posted: July 1, 2021 | Author: simeshev | Filed under: Java | Leave a comment »Found this article on building Java Docker images. Good stuff:
https://www.javacodegeeks.com/2019/02/docker-ise-java-application-maven-plugin.html
Java Track for Silicon Valey Code Camp 2011 is Available
Posted: September 22, 2011 | Author: simeshev | Filed under: Java | Leave a comment »Silicon Valley Code Camp is a free community event where developers learn from fellow developers. All are welcome to attend. The Java track is now available and open for registration:
http://www.siliconvalley-codecamp.com/Track2011-Java
Bad Timer!
Posted: September 15, 2011 | Author: simeshev | Filed under: Java | Leave a comment »java.util.Timer has a possibility of a memory leak. Here is how to fix it.
java.util.Timer provides an concise API for scheduling tasks in Java. TimerTask is an interface that is used to implement the scheduled work.
TimerTask has a method cancel() that is supposed to cancel a task previously scheduled for execution by the timer. Here is what Javadoc for TimerTask.cancel() says:
/** * Cancels this timer task. If the task has been scheduled for one-time * execution and has not yet run, or has not yet been scheduled, it will * never run. If the task has been scheduled for repeated execution, it * will never run again. (If the task is running when this call occurs, * the task will run to completion, but will never run again.) */ public boolean cancel();
Yet, the following innocent code fragment will cause OutOfMemoryError in just a few moments:
class MyTask extends TimerTask { public void run() { // Do nothing for the example purposes } } ... Timer timer = new Timer(); ... for (int i = 0; i < 10000000; i++) { final MyTask task = new MyTask(); timer.schedule(task, 30000); task.cancel(); }
You may assume that, per documentation, canceling the task will completely remove it from the internal Timer structures. Unfortunately, this is not what happens in real life. What TimerTask.cancel() does is that it just marks the task as canceled. The actual removal occurs only when the timer executes a next scheduled task. If you have a lot of cancellations, and the next task is scheduled to run far in the future, this may cause OutOfMemoryError because all those canceled tasks will still be sitting in timer’s queue.
The simplest solution is to call Timer.purge() after each call to TimerTask.cancel(). This will force the timer to immediately remove canceled tasks from the timer queue:
... Timer timer = new Timer(); ... for (int i = 0; i < 10000000; i++) { final MyTask task = new MyTask(); timer.schedule(task, 30000); task.cancel(); timer.purge(); }
Hope this helps.
Regards,
Slava Imeshev
Cacheonix | Clustered Java Cache