Array Allocation in Java

I have been doing a lot of work lately trying to get my Android Java project to run faster. The problem I quickly found was that I was loosing precious processing time to the Java Garbage collector. As a result, I was seeing an awofal lot of these:

DEBUG/dalvikvm(509): GC_FOR_ALLOC freed 4623K, 26% free 17648K/23751K, paused 260ms

My problem was not a memory leak - I did several rounds of heap snapshotting previously and my app's memory usage always returned to the same level. The problem was that this app does lots of data processing on double arrays. These arrays are newly allocated to hold some temporary data and then left for the GC to take care of. This was happening a lot causing the GC to run too often.

If you google around, you will discover that this is an age old problem and the common solution seems to be some form of Object Pooling. In my case, my problem was with all the double[]s, so I wrote a DoubleArrayAllocator class that is used like so:

public static final DoubleArrayAllocator GLOBAL_ALLOCATOR = new DoubleArrayAllocator();

... 
try {

  double[] tmp = GLOBAL_ALLOCATOR.alloc(500);

  // do some work with tmp
   ...

  //now tmp is no longer needed to free it

} finally {
  GLOBAL_ALLOCATOR.free(tmp);
}

Replacing most of the new double[] with DoubleArrayAllocator.alloc() got me ~10x improvement! In fact, now I only see the GC pausing my app once or twice while my app is processing, whereas before it was almost constant.

This technique does not come without danger. So be aware! First of all, you have to remember to free the array. But most importantly, you have to be absolutely sure your code is truly done with the allocated array when you free it. Otherwise you will introduce weird and seemingly random bugs in your code which are very difficult to reproduce and fix. Also, a lot of Java programers will recoil in horror with this technique because it is so contrary to Java concept of using "new is cheep" and "trust the garbage collector". Its true, I felt very dirty for doing this, but it was only after careful investigation into my particular problem that I was convinced I need to use it.

Paul Soucy

Read more posts by this author.