All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dstadler.commons.testing.HeapDump Maven / Gradle / Ivy

There is a newer version: 1.0.0.21
Show newest version
package org.dstadler.commons.testing;

import com.sun.management.HotSpotDiagnosticMXBean;

import java.io.IOException;
import java.lang.management.ManagementFactory;

public class HeapDump {
    // This is the name of the HotSpot Diagnostic MBean
    private static final String HOTSPOT_BEAN_NAME =
            "com.sun.management:type=HotSpotDiagnostic";

    // field to store the hotspot diagnostic MBean
    private static volatile HotSpotDiagnosticMXBean hotspotMBean;

    /**
     * Call this method from your application whenever you
     * want to dump the heap snapshot into a file.
     *
     * @param fileName name of the heap dump file
     * @param live flag that tells whether to dump
     *             only the live objects
     */
    public static void dumpHeap(String fileName, boolean live) throws IOException {
        // initialize hotspot diagnostic MBean
        initHotspotMBean();
        hotspotMBean.dumpHeap(fileName, live);
    }

    // initialize the hotspot diagnostic MBean field
    private static void initHotspotMBean() throws IOException {
        if (hotspotMBean == null) {
            synchronized (HeapDump.class) {
                if (hotspotMBean == null) {
                    hotspotMBean = getHotspotMBean();
                }
            }
        }
    }

    // get the hotspot diagnostic MBean from the
    // platform MBean server
    private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException {
        return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
                        HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy