How to generate an Heap Dump in Java

A heap dump is a snapshot of the Java Virtual Machine’s memory, capturing the state of objects and their relationships. Analyzing heap dumps aids in diagnosing memory-related issues, identifying memory leaks, and optimizing application performance. Let’s explore five different methods to generate a heap dump in Java.

1. Using jmap Command-Line Tool

Step 1: Identify Java Process ID (PID)

  • Find the PID of the Java process using tools like jps or system-specific commands.

Step 2: Run jmap Command

  • Open a terminal and execute:
jmap -dump:file=/path/to/dump.hprof <PID>

Replace /path/to/dump.hprof with the desired dump file path and <PID> with the Java process ID.

2. Leveraging jcmd Tool

Step 1: List Running Java Processes

$ jcmd
35738 /home/francesco/jboss/wildfly-30.0.0.Final/jboss-modules.jar -mp /home/francesco/jboss/wildfly-30.0.0.Final/modules org.jboss.as.standalone -Djboss.home.dir=/home/francesco/jboss/wildfly-30.0.0.Final -Djboss.server.base.dir=/home/francesco/jboss/wildfly-30.0.0.Final/standalone

Step 2: Generate Heap Dump

  • Use jcmd to create a heap dump:
jcmd <PID> GC.heap_dump /path/to/dump.hprof

3. Using VisualVM Tool

Step 1: Launch VisualVM

  • Open VisualVM and select the process from the applications list.

Step 2: Generate Heap Dump

  • Navigate to the application and click “Heap Dump” to trigger the dump generation.
how to generate an heap dump in java

4. Employing JConsole

Step 1: Launch JConsole

  • Start JConsole and connect it to the target Java process.

Step 2: Generate Heap Dump

  • In the MBeans/com.sun.management/HotSpotDiagnostic/Operations click on dumpHeap:
java capture heap dump

5. Programmatically Using HeapDumper

Step 1: Add HeapDumper Class

  • Include a custom HeapDumper class in your codebase which uses HotSpotDiagnosticMXBean to trigger an Heap Dump:

For example:

import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.ManagementFactory;

public class HeapDumpGenerator {
    public static void main(String[] args) {
        generateHeapDump("dump.hprof");
    }

    public static void generateHeapDump(String filePath) {
        try {
            // Get the HotSpotDiagnosticMXBean
            HotSpotDiagnosticMXBean mxBean = ManagementFactory.getPlatformMXBean(
                HotSpotDiagnosticMXBean.class);

            // Invoke the dumpHeap method to generate the heap dump
            mxBean.dumpHeap(filePath, true);

            System.out.println("Heap dump generated successfully at: " + filePath);
        } catch (Exception e) {
            System.err.println("Error occurred while generating heap dump: " + e.getMessage());
        }
    }
}

Finally, it is worth to note that you can also add a JVM start up option to generate an Heap Dumpif an OutOfMemory Error happens: How to configure the HeapDumpOnOutOfMemoryError parameter in Java

Conclusion

Heap dumps play a vital role in diagnosing memory-related issues in Java applications. Utilize these five methods—jmap, jcmd, VisualVM, JConsole, and programmatically—to generate heap dumps efficiently. Analyze the generated heap dumps using various profiling tools to identify memory leaks and optimize your Java applications.