JVM – Heap Dumps ( Generation and Analysis )

Looking at a heap dump is a great way of to ensure that your code is performing as you designed it. It also extremely useful for finding memory leaks.

The basic tools needed to generate a heap dump are available as part of the JDK or off the web. There are multiple analysis tools available either off the web or included in the JDK (starting with JDK 6).

click here – checkout my course on java and whitebox if you are interested. i cover JVM in details.

Generating a heap dump

The calls below fail to work on OS X. The following from Ken Sipeis claimed to fix it. (Thanks Chris)Let’s get to fixing the issue1. Download JSR-223’s engines https://scripting.dev.java.net/files/documents/4957/37593/jsr223-engines.zip
2. Download Rhino http://www.mozilla.org/rhino/download.html
3. Copy jsr223-engines/javascript/build/js-engine.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/
4. Copy rhino1_7R1/js.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/

  • you have to be sudo to do steps 3 and 4.

now jrunscript -q should look like this:
Language EmbeddedECMAScript 1.6 implemention “Mozilla Rhino” 1.6 release 2
Language AppleScript 2.0.1 implemention “AppleScriptEngine” 1.0
Language ECMAScript 1.6 implemention “Mozilla Rhino” 1.6R7

… and jrunscript will put you in a js shell.
… and jhat heap.out now works! Point your browser at http://localhost:7000 and object browse!

Also something I didn’t realize until I was preparing for this post… jmap worked for the console dump if I sudo’d the command. However it would not do a file dump. I did the file dump using jconsole and jmx. Now that the scripting issue is fixed, jmap is improved in the following manner:

1. if you type jmap , it now prompts you for your admin password (instead of just failing)
2. jmap -dump:live,format=b.file=heap.out 5510 now works

On Out Of Memory Error

You can instruct the JVM to generate a dump file whenever an OutOfMemory error is thrown. This is done by adding the following parameters to the command line.

-XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=<path to dump file>
Manual Heap Dump Generation JDK 5

You can use the jmap tool that comes with JDK 1.5 to generate a heap dump manually.

I was not able to get this to work on OS X.
jmap -heap:format=b -d64 <jvm pid>

This generates a file called heap.bin.

Manual Heap Dump Generation JDK 6

The jmap tool for JDK 6 has been upgraded and the options have changed.

I have not tried this to ensure the options are correct
jmap -J-d64 -dump:format=b,file=<path to dump file>  <jvm pid>

Heap Dump Analysis

There are several tools available to analyze heap dump files.

Heap Analysis Tool (HAT)

This easy to use tool is available as part of the JDK 6 installation as the command jhat. It is available for JDK 5 from https://hat.dev.java.net/. Launching the tools creates a web server on port 7000 that can be used to view the heap analysis.

I was not able to get either form to work on OS X.

JDK 5:

java -Xmx512m -jar ha26.jar <path to dump file>

JDK 6:

jhat <path to dump file>

Note : when generating heaps, it's almost always a good idea to use the :live option to only dump living objects. The times when you care about dead objects in the heap are few and far between.

<!– –>



Filed under Java, JVM

6 responses to “JVM – Heap Dumps ( Generation and Analysis )

  1. when running jmap on MacOSX you’ll need to execute the command with superuser priviledges. Thus use:
    $> sudo jmap …..

  2. Mmakwena

    i a trying to run java -Xmx512m -jar ha26.jar and i get the error
    Unable to access jarfile ha26.jar: and i am running it in the directory where the file is :C:\Program Files\ha426>not sure what could be the problem.

  3. Please make sure the file is accessible

  4. Willie C.

    nice article.

  5. Pingback: How To Fix Heap Dump Generation Failed: 0x8007139f Errors - Windows Vista, Windows 7 & 8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s