1

我想访问 jvm 堆以迭代对象。我发现了如何做到这一点的以下示例。我用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 对象图并搜索该图。

4

1 回答 1

0
agent.attach(Integer.parseInt(jvmName.split("@")[0])); // exception here!!!!

about above, I think you should pass another jvm process id, not current process id.

you could remove this line:

VM.initialize(null, false);

So, I can work fine.

于 2013-11-21T08:21:17.617 回答