Java 连接到jvm
我想访问jvm堆来迭代对象。我发现了以下方法可以做到这一点。我使用Java 连接到jvm,java,jdi,Java,Jdi,我想访问jvm堆来迭代对象。我发现了以下方法可以做到这一点。我使用jdk1.7.0\u11 我尝试了以下代码: public static void main(String[] args) { RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); System.out.println(runtimeBean.getVmName()); System.out.println(runtimeBean
jdk1.7.0\u11
我尝试了以下代码:
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对象图并搜索该图
agent.attach(Integer.parseInt(jvmName.split("@")[0])); // exception here!!!!
关于上述内容,我认为您应该传递另一个jvm进程id,而不是当前进程id
您可以删除此行:
VM.initialize(null, false);
所以,我可以很好地工作。你成功了吗?@Tupac,那是很久以前的事了,不幸的是,我不记得事情是怎么结束的:(,可能我刚刚放弃了。你可以看看user2994487的答案,希望它能帮到你