Programmatically Dump JVM Heap and Analyze with jhat: A Step-by-Step Guide

This guide shows how to programmatically generate a JVM heap dump using HotSpotDiagnosticMXBean, retrieve the binary file, and analyze it offline with the jhat tool, including code snippets, usage notes, and visualization of memory usage.

FunTester
FunTester
FunTester
Programmatically Dump JVM Heap and Analyze with jhat: A Step-by-Step Guide

Programmatic JVM heap dump

This example shows how to trigger a heap dump from inside a running Java application by using the com.sun.management.HotSpotDiagnosticMXBean. The static method dumpHeap(String fileName, boolean live) ensures the MXBean is initialized and then calls hotspotMBean.dumpHeap(fileName, live). The live flag determines whether only live objects are included (true) or the entire heap (false). Errors are logged via SLF4J.

package com.fun.utils;

import com.fun.frame.SourceCode;
import com.sun.management.HotSpotDiagnosticMXBean;
import org.slf4j.Logger;
import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;

public class HeapDumper extends SourceCode {
    private static Logger logger = getLogger();
    private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
    private static volatile HotSpotDiagnosticMXBean hotspotMBean;

    static void dumpHeap(String fileName, boolean live) {
        initHotspotMBean();
        try {
            hotspotMBean.dumpHeap(fileName, live);
        } catch (Exception e) {
            logger.error("Failed to generate heap dump!", e);
        }
    }

    private static void initHotspotMBean() {
        if (hotspotMBean == null) {
            synchronized (HeapDumper.class) {
                if (hotspotMBean == null) {
                    try {
                        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                        hotspotMBean = ManagementFactory.newPlatformMXBeanProxy(
                            server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
                    } catch (Exception e) {
                        logger.error("Failed to initialize MBean!", e);
                    }
                }
            }
        }
    }
}

Important note: When multiple heap snapshots are taken from the same JVM, the objects in different dumps cannot be directly correlated because tools such as jmap use the raw object address as the identifier, and these addresses may change after a garbage‑collection cycle. Correlation is only possible through aggregated statistics like histograms.

Analyzing the dump with jhat

After the dump file (e.g., heap.bin) is generated, copy it to a local machine or keep it on the server and start the Java Heap Analysis Tool: jhat -port 8888 heap.bin The tool launches an embedded HTTP server listening on the specified port. Open http://localhost:8888 in a browser to explore the heap contents. The UI provides class histograms, object reference graphs, and the ability to inspect individual objects as they existed at the time of the dump.

jhat heap analysis screenshot
jhat heap analysis screenshot
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMPerformance MonitoringHeap DumpjhatHotSpotDiagnosticMXBean
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.