15

如何在我的应用程序中创建堆转储,而不使用类 HotSpotDiagnosticMXBean。由于访问限制,java/rt.jar我无法使用对HotSpotDiagnosticMXBean. 我知道如何解决 eclipse.compiler 错误,但是如何为我的构建修复它?是否有替代方法以编程方式创建堆转储?

4

5 回答 5

19

好的,似乎您可以使用反射绕过限制:

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);
    }
}
于 2012-09-06T09:47:25.933 回答
3
package ru.test;

import com.sun.management.HotSpotDiagnosticMXBean;
import org.junit.Test;

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

public class TestDump {
    @Test
    public void test() throws IOException {
        long i = 6789;
        String s = "HELLO";
        System.out.println(i);
        System.out.println(s);

        HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
                "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
        mxBean.dumpHeap("C:\\temp\\dump\\heap1.bin", true);
    }
}

出去:

6789
HELLO    


Process finished with exit code 0
于 2020-02-07T07:56:39.807 回答
0

您可以将具有 HotSpotDiagnosticMXBean.class 的 rt.jar 复制到其他位置。使用“添加外部 jar”在构建路径中引用复制的 jar。这允许您创建对象并获取堆转储。

new HotSpotDiagnostic().dumpHeap("d:\\HeapDump1",true);

我能够以这种方式生成 Heapdump。我正在寻找 jars 错误的任何运行时冲突。幸运的是没有。

于 2017-07-10T18:08:14.767 回答
-1

VisualVM 可以进行堆转储。

您也可以在 linux 系统上尝试 jhat 命令。

于 2012-09-06T08:28:34.443 回答
-1

此类是公共的,因此您可能无法访问它的唯一原因是您的 JVM 版本中没有它,因为它太旧了。

链接到的示例在 Java 6 和 7 中编译并运行良好。如果可能,请尝试升级到最新版本的 Java。

于 2012-09-06T08:54:07.147 回答