java – 从应用程序内创建堆转储,不需要HotSpotDiagnosticMXBean

前端之家收集整理的这篇文章主要介绍了java – 从应用程序内创建堆转储,不需要HotSpotDiagnosticMXBean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在我的应用程序内创建堆转储,而不使用HotSpotDiagnosticMXBean类.
由于 java / rt.jar的访问限制,我无法使用依赖关系对HotSpotDiagnosticMXBean进行编译.我知道如何解决eclipse.compiler错误,但是我如何解决它的构建?
有没有办法通过编程方式创建堆转储?

>这个解决方案不行,因为它是HotSpotDiagnosticMXBean依赖关系:HotSpot-dependent Heap Dump

解决方法

好的,好像你可以通过使用反射来绕过限制:
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);
    }
}
原文链接:https://www.f2er.com/java/122151.html

猜你在找的Java相关文章