Java Track for Silicon Valey Code Camp 2011 is Available

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!

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


A SpecialTeas.com Relacement

I have found a decent replacement for SpecialTeas.com. It’s Upton Teas Imports.

So, SpecialTeas.com went under which was a family micro-tragedy. After searching for awhile, I have found a decent replacement for SpecialTeas.com. It is Upton Tea Imports [1]. The most important thing is that the selection is very good and on par with SpecialTeas.com. The shipment is pretty fast. The tea is packaged well, and the samples are coming in tins:

We are happy campers again.

References

  1. Upton Tea Imports

SCI-FI Movies That Will Blow Your Mind

There is something in the makeup in software people’s brain that makes them love SCI-FI. Here are SCI-FI movies that I am sure you’ll love.

After having my mind blown twice in a row recently I thought it would benefit many if I logged those awesome SCI-FI movies so that you can enjoy them, too. I intend to keep this list growing. Here they go, in no particular order:

  • Sunshine
  • Lathe of Haven
  • Absolon
  • Alien
  • Aliens
  • Dark City
  • Blade Runner
  • Screamers
  • Metropia
  • Cypher

Regards,

Slava Imeshev


Presenting on Best Practices for Scaling Java Applications with Distributed Caching at Silicon Valley Code Camp

I will be giving a talk on best practices for scaling Java applications with distributed caching on October 8 or 9, 2011 at Silicon Valley Code Camp.

This vendor-neutral session will explore ways for improving reliability, scalability, performance and concurrency of Java applications through local and distributed caching. You will learn how to identify data best fit for caching; deepen your knowledge with best practices for caching; understand distributed caching and ways it enables scalability of Java applications. We will discuss common usage scenarios for distributed caching and will provide best practices for the supporting network infrastructure.

Register here, it’s completely free: http://www.siliconvalley-codecamp.com/Sessions.aspx?ForceSortBySessionTime=true&AttendeeId=169

I will post the slides online after the session. If you cannot make it to Code Camp but want to get notified when the slides are available, drop me a line at simeshev@cacheonix.com.

See you there!

Regards,

Slava Imeshev