How to take Heap dumps? 8 different options

You can take HEAP dumps in a number of ways. Jvm options like optionHeapDumpOnOutOfMemoyError, HeapDumpOnCtrlBreak, jcmd, Jmx, Jmap, HotSpotDiagnostic MBean, JVisualVM any way can be used to generate heap dump.

A heap dump is a snapshot representing memory allocation of java processes. The snapshot contains information about classes and Java objects. The JVM allocates memory for objects of all class instances and arrays, from the heap. The garbage collector reclaims the heap memory whenever an object is no longer needed and there is no reference to the object.

The heap dump should be generated at different intervals, most preferably after the full GC in order to eliminate unnecessary “noise” from non-referenced objects. Memory leaks, sudden memory jumps, too many objects creation resulting in memory overflow are different problems which can be identified easily from heap dumps.

The heap dump is in HPROF binary format, and so it can be analyzed using any tools that can import this format. For example, the jhat tool can be used to do rudimentary analysis of the dump.

1. HeapDumpOnOutOfMemoyError Jvm option

The -XX:+HeapDumpOnOutOfMemoryError command-line option tells the HotSpot VM to generate a heap dump when an allocation from the Java heap or the permanent generation cannot be satisfied. There is no overhead in running with this option, and so it can be useful for production systems where OutOfMemoryError takes a long time to surface.

The ConsumeHeap fills up the Java heap and runs out of memory. When java.lang.OutOfMemoryError is thrown, a heap dump file is created. In this case the file is 507MB and is created as java_pid.hprof in the current directory. By default the heap dump is created in a file called java_pidpid.hprof in the working directory of the VM.

-XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512m ConsumeHeap
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid2262.hprof ...
Heap dump file created [531535128 bytes in 14.691 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at ConsumeHeap$BigObject.(
        at ConsumeHeap.main(

In above example, if there are multiple heap dumps then new dump file will override the existing one. In case you want to retain all heap dumps, an additional option HeapDumpPath can be provided to set the heap dumps. After that, whenever the application will throw OutOfMemoryError, the Heap dump will automatically be generated.additional option HeapDumpPath can be provided to set the location for heap dumps. After that, whenever the application will throw OutOfMemoryError, the Heap dump will automatically be generated at specified path.

-XX:HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/logs/dumps/

2. HeapDumpOnCtrlBreak Jvm option

The -XX:HeapDumpOnCtrlBreak option adds the hprofdump diagnostic command to the list of commands that run automatically when the Ctrl-break keys are pressed (similar to the print_threads diagnostic command). An additional option HeapDumpPath can also be provided to set the location for heap dumps. After adding this option, every time when a ctrl + break (Kill -3) signal is sent to the JVM, a heap dump file is generated.

-XX:HeapDumpOnCtrlBreak -XX:HeapDumpPath=/logs/dumps/


The jcmd utility is used to send diagnostic command requests to the JVM, where these requests are useful for controlling Java Flight Recordings, troubleshoot, and diagnose JVM and Java Applications. It must be used on the same machine where the JVM is running, and have the same effective user and group identifiers that were used to launch the JVM.

Execute jcmd without any parameters to list all JVM processes, starting with a PID. The command jps also does the job.

Execute jcmd <pid> GC.heap_dump filename=Myheapdump to create a heap dump.

4. JMX

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (such as printers) and service-oriented networks. Those resources are represented by objects called MBeans (for Managed Bean). You can use any JMX client like JMC, Jconsole, Jmxsh etc to access JMX interface.

5. Jmap

The JDK has an inbuilt command based tool to generate heap dumps. Jmap tool can be used with the PID of the Java process. This tool can be used as follows:

jmap [options] pid


jmap -dump:file=~/dumps/dump.bin 1234

Pid: The process ID for which the information specified by the options is to be printed. The process must be a Java process. To get a list of Java processes running on a machine, use either the ps command or, if the JVM processes are not running in a separate docker instance, the jps command.

Jmap command is unsupported and might not be available in future releases of the JDK.

6. Programmatically via Jmap

Programmatically we can take heap dump using Jmap or HotSpotDiagnostic MBean.  

        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.substring(0, name.indexOf(“@”)); // jmap tool usage 
        String[] cmd = {“
            “-dump: file = /usr/tmp / newdump.bin”,
        Process process = Runtime.getRuntime().exec(cmd);

7. Programmatically via HotSpotDiagnostic MBean

This is simple to use option. There is an oracle article on this Mbean to be used in java application. For details refer here

ManagementFactory.getDiagnosticMXBean().dumpHeap(“/usr/tmp/newdump.bin “, true);

8. JvisualVM

You can use Java VisualVM to take a heap dump of a local running application. When you use Java VisualVM to take a heap dump, the file is only temporary until you explicitly save it. If you do not save the file, the file will be deleted when the application terminates. You can take a heap dump by doing either of the following:

  • Right-click the application in the Applications window and choose Heap Dump.
  • Click Heap Dump in the Monitor tab of the application.

References ‘

Leave a Reply here