Detecting a Java memory leak

It is all too common to find out about a memory leak by the JVM stopping due to an OutOfMemoryError. Before releasing a Java-based product, it should generally be tested for memory leaks. The standard use cases should be run for some duration, and the live set should be measured to see that no memory is leaking. In a good test setup, this is automated and tests are performed at regular intervals.

Note

We got overconfident and failed to heed our own advice in JRockit Mission Control 4.0.0. Normally, we use the Memory Leak Detector to check that editors are reclaimed properly in JRockit Mission Control during end testing. This testing was previously done by the developers themselves, and had failed to find its way into the formal test specifications. As a consequence, we would leak an editor each time a console or a Memleak editor was opened. The problem was resolved, of course, using the Memory Leak Detector.

A memory leak in Java can typically be detected by using the Management Console to look at the live set attribute. It is important to know that a live set increase over a shorter period of time does not necessarily have to be indicative of a memory leak. It could be the case that the load of the Java application has changed, that the application is serving more users than before, or any other reason that may trigger the need to use more memory. However, if the trend is consistent, there is very likely a problem that should be investigated.

There are primarily two different ways of doing detailed heap analysis:

  • Online heap analysis, using the JRockit Memory Leak Detector
  • Offline heap analysis from a heap dump

For online analysis, trend analysis data is collected by piggybacking on the garbage collector. This is virtually without overhead since the mark phase of a GC already needs to traverse all live objects on the heap. The resulting heap graph is all the data we need to do a proper trend analysis for object allocation.

Note

The heap dump format used by JRockit is the same as produced by the Java Virtual Machine Tool Interface (JVMTI) based heap profiler HPROF, included with the JDK. Consequently, the dumps produced by JRockit can be analyzed in all tools supporting the HPROF format. For more information about HPROF, see the following file in the JRockit JDK:

JROCKIT_HOME/demo/jvmti/hprof/src/manual.html

For more information about JVMTI, see the JDK Documentation:

http://java.sun.com/javase/6/docs/platform/jvmti/jvmti.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.57