我想访问jvm堆来遍历对象。我发现下面的example是如何做到这一点的。我使用jdk1.7.0_11。
我尝试了以下代码:
public static void main(String[] args) {
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
System.out.println(runtimeBean.getVmName());
System.out.println(runtimeBean.getVmVendor());
System.out.println(runtimeBean.getVmVersion());
String jvmName = runtimeBean.getName();
BugSpotAgent agent = new BugSpotAgent();
agent.attach(Integer.parseInt(jvmName.split("@")[0])); // exception here!!!!
VM.initialize(null, false);
VM vm = VM.getVM();
System.out.println(vm.getVMInternalInfo());
ObjectHeap heap = vm.getObjectHeap();
heap.iterate(new CustomHeapVisitor());
}输出如下:
Java HotSpot(TM) 64-Bit Server VM
Oracle Corporation
23.6-b04和例外:
Exception in thread "main" sun.jvm.hotspot.debugger.DebuggerException: Windbg Error: AttachProcess failed!
at sun.jvm.hotspot.debugger.windbg.WindbgDebuggerLocal.attach0(Native Method)
at sun.jvm.hotspot.debugger.windbg.WindbgDebuggerLocal.attach(WindbgDebuggerLocal.java:152)
at sun.jvm.hotspot.bugspot.BugSpotAgent.attachDebugger(BugSpotAgent.java:789)
at sun.jvm.hotspot.bugspot.BugSpotAgent.setupDebuggerWin32(BugSpotAgent.java:712)
at sun.jvm.hotspot.bugspot.BugSpotAgent.setupDebugger(BugSpotAgent.java:515)
at sun.jvm.hotspot.bugspot.BugSpotAgent.go(BugSpotAgent.java:492)
at sun.jvm.hotspot.bugspot.BugSpotAgent.attach(BugSpotAgent.java:331)
at mishanesterenko.jdi.Main.main(Main.java:27)还应该做些什么才能使其正常工作呢?最后,我想访问堆中的jvm对象图并搜索该图。
发布于 2013-11-21 16:21:18
agent.attach(Integer.parseInt(jvmName.split("@")[0])); // exception here!!!!关于上面,我认为你应该传递另一个jvm进程id,而不是当前的进程id。
您可以删除此行:
VM.initialize(null, false);所以,我可以很好地工作。
https://stackoverflow.com/questions/16625191
复制相似问题