Saturday, May 14, 2011

Analyzing Java memory usage

Even when using Java, one might encounter memory issues. In those cases, it is good to know that there are tools to help you out, some of which are bundled with Sun's JDK distribution.

Creating a Java heap dump
The first thing to do is obtaining a heap dump, in which Java writes the current content of its heap to a file. There are many ways to achieve this. This can be done explicitly by issuing commands from a command line, by using Java's JMX, or by instructing the JVM to make a heap dump when an OutOfMemoryError eccurs.

The simplest way to get a heap dump from a running system is by issuing the command jmap from a command line, for instance

$ jmap -dump:live,format=b,file=heap.bin 6114

will dump the live objects in the heap of the Java process with process ID 6114 to a file called heap.bin. The process ID can be found by using jps or ps, if on a Unix system.

But what if one wants a dump of the heap as it is when an OutOfMemoryError occurs? One of the arguments the JVM accepts is: -XX:+HeapDumpOnOutOfMemoryError. As the name of the argument suggests, this tells the JVM to create a dump of the heap when an OutOfMemoryError occurs. If you also supply the argument -XX:HeapDumpPath, you can instruct the JVM on where to put the generated dump file (by default it is created in the working directory of the VM). For instance:

$ java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.bin ...
Analyzing the heap dump
The heap dump file obtained by the above is a binary file not suited for manual inspection. However, also in the JDK distribution is a tool called jhat. This is a tool for analyzing a binary heap dump file (Java Heap Analysis Tool). It parses the heap file and launches a web server, allowing you to navigate the information in the heap in a web browser. The command

$ jhat heap.bin

will parse the heap in file heap.bin. When launching a web browser and entering localhost:7000 in the address bar, I am presented with the following:

This is simply a listing of all classes loaded. When clicking on one of the class names, a new screen appears with more details on the selected class:

It is worth noticing that in the far bottom of the first screen (All classes), there are a number of useful links, for instance the "Heap Histogram", which shows the number of instances and the total size for each class. Another link take you to a new page where you can execute Object Query Language (OQL) queries. One example of a OQL query is:
select s from java.lang.String s where s.count >= 100
which will show you all Strings of length 100 or more. See the "OQL Help" page for more on OQL.

Another way of inspecting the obtained heap dump is with the NetBeans IDE. Simply download the simplest version of it, launch it, and import your heap dump with "Load Heap Dump..." under "Profile" on the menu bar. This will give you the same information as the jhat-tool, but in a more user-friendly way.