1

I want to get access to jvm heap to iterate over objects. I found following example of how this could be done. I use jdk1.7.0_11.

I tried following code:

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());
}

There is following output:

Java HotSpot(TM) 64-Bit Server VM
Oracle Corporation
23.6-b04

and exception:

   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)

What else should be done to make this work? Finally I want to get access to jvm object graph in heap and search this graph.

michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

1 Answers1

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.

user2994487
  • 121
  • 2
  • 4