java - 在没有 HotSpotDiagnosticMXBean 的情况下从应用程序内部创建堆转储

标签 java jmx heap-dump

如何在不使用类 HotSpotDiagnosticMXBean 的情况下从我的应用程序中创建堆转储。 由于 java/rt.jar 的访问限制,我无法使用对 HotSpotDiagnosticMXBean 的依赖性来编译它。我知道如何解决 eclipse.compiler 错误,但如何为我的构建修复它? 除了以编程方式创建堆转储之外,还有其他方法吗?

最佳答案

好吧,看来你可以通过使用反射绕过限制:

package lab.heapdump;

import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;


@SuppressWarnings("restriction")
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 Object 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
     */
    static void dumpHeap(String fileName, boolean live) {
        // initialize hotspot diagnostic MBean
        initHotspotMBean();
        try {
            Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
            Method m = clazz.getMethod("dumpHeap", String.class, boolean.class);
            m.invoke( hotspotMBean , fileName, live);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

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

    // get the hotspot diagnostic MBean from the
    // platform MBean server
    private static Object getHotspotMBean() {
        try {
            Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            Object bean = 
                ManagementFactory.newPlatformMXBeanProxy(server,
                HOTSPOT_BEAN_NAME, clazz);
            return bean;
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

    public static void main(String[] args) {
        // default heap dump file name
        String fileName = "D:\\heap.bin";
        // by default dump only the live objects
        boolean live = true;

        // simple command line options
        switch (args.length) {
            case 2:
                live = args[1].equals("true");
            case 1:
                fileName = args[0];
        }

        // dump the heap
        dumpHeap(fileName, live);
    }
}

关于java - 在没有 HotSpotDiagnosticMXBean 的情况下从应用程序内部创建堆转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12295824/

相关文章:

java - 查找 50 个最便宜的不匹配价格

java - 生成堆转储 Java JRE7

java - ActiveMQ 生产者生成堆转储

java - 设计模式根据条件返回子类或父类

java - 在 Windows 计算机上使用 Java Swing SystemLookAndFeel 会导致带有 JTextPanes 的 CachedPainter 中出现内存泄漏

java - 提交在/workspace 中生成的文件的容器更改(即使使用 makefile)不会持续到图像的新实例中

JBOSS,如何添加依赖javax.jmx?

java - JMX 垃圾收集和 System.gc() 之间的区别?

jboss - 获取 mbean 属性值?

java - Centos 64 位和 openjdk 7 上的堆转储错误