Tuesday, 22 May 2012

Generating Java Heap Dumps on the go !

Ever come across a stituation where you need to take a look at the heap dumps , as to whats eating up the memory ?

Well depends which version of JDK u r on. The couple I know of are:


  1. jmap - a very old command which is now un supported . If u r using old versions of JDK prior to 1.5 this command is handy.
  2. jconsole - you can aquire the heap dump using the hotspotdiagnostics using the javax Mbeans. For jconsole to recognize the process , we should start the java process with the following :
-Dcom.sun.management.jmxremote.port=<PORT>   
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false
  1. jrmc - jrockit mission control - If you are on the latest versions of JDK from jrockit , this is an ultra cool utility provided to be able to diagnose and monitor and profile your jvm. Go to the Advanced ->Diagnose->heapdumpprof util and generate the heap dump at the desired location.
  2. When you startup the jvm add the arguments  -XX:+|-HeapDumpOnOutOfMemoryError and                                 -XX:HeapDumpPath=<Location where the dump should be written to>
  3. Alternatively if you are  using Jrockit you can provide the following startup arguments                                   -Djrockit.oomdiagnostics=true and  -Djrockit.oomdiagnostics.filename=d:\heapdump.log
  4. You could also monitor the memory using visualvm provided in latest jdks which give you better insight on the young heap and the old heap.
  5. Eclipse memory analyzer can also be used to generate a heap dump and also view the dum generated by any of the above processes. This is very helpful m when you want to identify memory leaks etc

        The dump thus generated usually can is a log file which you can read or a hprof file which can be analysed using a memory analyzer(eclipse plugin http://www.eclipse.org/mat/downloads.php).