HeapDumps: What are a they? Why would I want one? How do I generate them? How do I view one?

Posted on July 15, 2007 
Filed Under Programming

In response to my last post a colleague said:

Good post…..how did you create the HeapDump, and how did you view it?

Good questions.

A HeapDump is simply a dump of the live objects in a JVM’s heap at a given point in time. HeapDumps are usually used to debug memory leaks or to find the big memory consumers within a JVM. While most JVMs provide the ability to create HeapDumps the generation techniques vary from JRE to JRE.

IBM’s JRE has many options to deal with the generation and format of it’s HeapDumps, for all the details take a look at the the diagnostic guide. By default a HeapDump is only produced when an java.lang.OutOfMemoryError is hit. This can be seen by executing “java -Xdump:what”


gissel@uw-t60p:~/Workings/leak> java -Xdump:what

Registered dump agents
———————-
dumpFn=doSystemDump
events=gpf+abort
filter=
label=/home/gissel/Workings/leak/
core.%Y%m%d.%H%M%S.%pid.dmp
range=1..0
priority=999
request=serial
opts=
———————-
dumpFn=doSnapDump
events=gpf+abort
filter=
label=/home/gissel/Workings/leak/
Snap%seq.%Y%m%d.%H%M%S.%pid.trc
range=1..0
priority=500
request=serial
opts=
———————-
dumpFn=doSnapDump
events=uncaught
filter=java/lang/OutOfMemoryError
label=/home/gissel/Workings/leak/
Snap%seq.%Y%m%d.%H%M%S.%pid.trc
range=1..4
priority=500
request=serial
opts=
———————-
dumpFn=doHeapDump
events=uncaught
filter=java/lang/OutOfMemoryError
label=/home/gissel/Workings/leak/
heapdump.%Y%m%d.%H%M%S.%pid.phd
range=1..4
priority=40
request=exclusive+prepwalk
opts=PHD
———————-
dumpFn=doJavaDump
events=gpf+user+abort
filter=
label=/home/gissel/Workings/leak/
javacore.%Y%m%d.%H%M%S.%pid.txt
range=1..0
priority=10
request=exclusive
opts=
———————-
dumpFn=doJavaDump
events=uncaught
filter=java/lang/OutOfMemoryError
label=/home/gissel/Workings/leak/
javacore.%Y%m%d.%H%M%S.%pid.txt
range=1..4
priority=10
request=exclusive
opts=
———————-

This is a good setting when running in a production environment, but not the best for debugging. When debugging I usually like to take several HeapDumps during a test, as the heap grows, to see what what type of object is growing. This is best done by sending a SIGQUIT signal to the JVM process. To enable the creation of HeapDumps via the SIGQUIT signal set the JVM option “-Xdump:heap”. The total effect of this option can be seen by executing “java -Xdump:heap”


gissel@uw-t60p:~/zero-1.0.0.P20070712-1334/apps/employee.demo> java -Xdump:heap:?
JVMDUMP000E Dump Option unrecognised: -Xdump:heap:…?

Capture raw heap image:

-Xdump:heap[:defaults][:<option>=<value>, ...]

Dump options:

events=<name> Trigger dump on named events
[+<name>...] (see -Xdump:events)

filter=[*]<name>[*] Filter on class (for load,throw,catch,uncaught)
#<n>..<m> Filter on exit code (for vmstop)

file=<label> Output file
range=<n>..<m> Limit dumps
priority=<n> Highest first
request=<name> Request additional VM actions
[+<name>...] (see -Xdump:request)

opts=PHD|CLASSIC

Default -Xdump:heap settings:

events=gpf+user
filter=
file=/home/gissel/zero-1.0.0.P20070712-1334/apps /employee.demo/
heapdump.%Y%m%d.%H%M%S.%pid.phd
range=1..0
priority=40
request=exclusive+prepwalk
opts=PHD

Could not create the Java virtual machine.

An interesting option is “opts”. As seen above, the default “opts” value is PHD. PHD is actually the format of the HeapDump the other options are CLASSIC(text file), and PHD+CLASSIC (produces 2 files for each requested HeapDump, one in PHD and the other in TXT format). The HeapDump file format is important because some of the tools can only read either PHD or TXT format.

The tool I use to view HeapDumps varies depending upon which tool is most up-to-date . Currently, the first tool I use to get a general feel for what is in the heap is HeapAnalyzer. I also like HeapRoots.

heap_small1.JPG

Comments

Leave a Reply