OverviewA 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.
- 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.
Capture heap dumpThere are multiple options to capture heap dumps.
jmap -dump:format=b,file=<file-path> <pid>
jcmd <pid> GC.heap_dump /opt/tmp/heapdump.bin
JVisualVM is a visual monitoring, troubleshooting tool that is packaged within the JDK.
IBM Administrative Console:
There is a good article from Oracle which gives a good insigt of how to programmatically dumping heap from Java applications