Skip to main content

Java Heap Dump


A heap dump is a snapshot of all the objects that are in memory in the JVM at a certain moment. They are very useful to troubleshoot memory-leak problems and optimize memory usage in Java applications.
Heap dumps are usually stored in binary format hprof files.


Depending on the heap dump type, includes[1]
  • Objects: Class, fields, primitive values, and references.
  • Classes: Class loader, name, super class, and static fields.
  • Garbage collection: Objects defined to be reachable by the JVM.
  • Thread Stacks and Local Variables: Call-stacks of threads at the moment of the snapshot, and information about local objects on a frame by frame basis.
A heap dump does not contain allocation information, therefore you cannot work out what created the objects or where the objects were created.

Capture heap dump

There are multiple options to capture heap dumps.[2]
  • jmap:
    jmap -dump:format=b,file=<file-path> <pid>
  • HeapDumpOnOutOfMemoryError:
    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/tmp/heapdump.bin
  • jcmd:
    jcmd <pid> GC.heap_dump /opt/tmp/heapdump.bin
  • JVisualVM:
    JVisualVM is a visual monitoring, troubleshooting tool that is packaged within the JDK.
  • JMX:
  • IBM Administrative Console:
  • Programmatic Approach:
    There is a good article from Oracle which gives a good insigt of how to programmatically dumping heap from Java applications