Java / Scala Memory Leak Investigation

Every now and then a memory leak comes along, in my case typically raised via a java.lang.OutOfMemoryError. For me, the following steps have helped to narrow the issue down.

Step 1 – Consider speed of parallel execution and heap size

Especially in parallel executing programs, the creation of a thread or a Future in Scala is simple and fast. This however can lead to the issue that the Future is allocated but not yet run. If many Futures are created in sequence, their memory allocations will remain in memory and make the program fail.

The solution is to process chunks in parallel that are sure to stay within the memory limits, then wait for this to finish and start the next chunk.

Alternatively, if operating on very large heaps and/or highly concurrent applications, the garbage collection may slow down significantly. Consideration to an alternative garbage collector should be given, such as specifying the concurrent collector.

-XX:+UseConcMarkSweepGC

Step 2 – Investigate the source

To get to the root cause, it helps to investigate the memory on failure in detail. This can be done by adding the following parameters to the execution of Java:

-XX:+HeapDumpOnOutOfMemoryError -verbose:gc -Xmx512m -XX:+PrintGCDetails

Setting the maximum heap to 512MB or a reasonable small size for your particular program helps to hit the limit quicker in a leaking application.

The verbose GC information helps to see the performance of the garbage collector on the fly. The output is similar to the following:

[GC 173630K->74442K(216064K), 0.0066210 secs]
[Full GC 74442K->36371K(208384K), 0.1041700 secs]

The first two numbers indicate the size of objects before and after collection. The number in parentheses is the committed size of the heap, followed by the time it took to perform the small or full garbage collection.

The option “-XX:+PrintGCDetails” adds further detail to this output.

As indicated by the first option, a heap dump will be created on failure.

Step 3 – Analyze

With the heap dump available, various tools can be used to investigate the possible issue in detail. I personally prefer to use Eclipse Memory Analyzer for further investigation: http://www.eclipse.org/mat/

Additional links

Leave a comment