如何在应用程序内部创建堆转储文件,而不使用HotSpotDiagnosticMXBean类。由于无法访问java/rt.jar
,我无法将其编译为依赖项HotSpotDiagnosticMXBean
。我知道如何解决eclipse.compiler错误,但是我该如何解决构建问题?是否有其他方法可以通过编程方式创建堆转储?
- 基于HotSpotDiagnosticMXBean的解决方案无法使用:HotSpot-dependent 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);
}
}
HotSpotDiagnosticMXBean
类即可。 - Chrisspackage 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
你可以复制包含HotSpotDiagnosticMXBean.class的rt.jar文件到另一个位置。使用"添加外部jar"在构建路径中引用复制的jar文件。 这样就可以创建对象并获取堆转储。
new HotSpotDiagnostic().dumpHeap("d:\\HeapDump1",true);
VisualVM可以生成堆转储。
您还可以在Linux系统上尝试jhat命令。
这个类是公共的,所以你可能无法访问它的唯一原因是你的JVM版本太旧了。
链接的示例在Java 6和7中编译和运行良好。如果可能的话,请尝试升级到最新版本的Java。