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

io.robe.common.system.HeapDump Maven / Gradle / Ivy

There is a newer version: 0.5.0.0-1039
Show newest version
package io.robe.common.system;

import com.sun.management.HotSpotDiagnosticMXBean;
import io.robe.common.utils.FileOperations;

import javax.management.MBeanServer;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.UUID;

/**
 * Helper class to get heapdump of the current application.
 * Only works with Oracle HotSpot
 */
public class HeapDump {
    private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";

    private static volatile HotSpotDiagnosticMXBean hotspotMBean;

    /**
     * Call this method from your application whenever you
     * want to dump the heap snapshot into a to a temp file.
     *
     * @param live flag that tells whether to dump only the live objects
     * @return heap file
     */
    public static File dump(boolean live) {
        initHotspotMBean();
        try {
            String tempName = FileOperations.TEMP_DIR + UUID.randomUUID().toString().replaceAll("-", "");
            hotspotMBean.dumpHeap(tempName, live);
            return new File(tempName);
        } catch (Exception e) {
            throw new RuntimeException("exception:"+e);
        }
    }

    /**
     * Initialize MBean
     */
    private static void initHotspotMBean() {
        if (hotspotMBean == null) {
            synchronized (HeapDump.class) {
                if (hotspotMBean == null) {
                    hotspotMBean = getHotspotMBean();
                }
            }
        }
    }

    /**
     * @return {@link HotSpotDiagnosticMXBean} instance
     */
    private static HotSpotDiagnosticMXBean getHotspotMBean() {
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            return  ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException("exception:"+exp);
        }
    }
    private HeapDump(){

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy