Is there a simple way of obtaining all object instances of a specific class in Java

后端 未结 6 1496
终归单人心
终归单人心 2020-11-29 09:12

Currently I am working on a Java agent to assemble memory stats. With the help of the instrumentation API I can get a hold of the classes (and manipulate them). With plain J

6条回答
  •  独厮守ぢ
    2020-11-29 10:02

    The debugger in Eclipse can show you all the instances of a class, so I looked around Eclipse's sources. Eclipse uses the Java Debug Wire Protocol, which allows you (since Java 6) to look up all the instances of the requested class. If you want to go down this path, grab a copy of Eclipse sources and check out the instances method of org.eclipse.jdi.internal.ReferenceTypeImpl.

    A simpler way is to use the Java Debug Interface. Note the ReferenceType.instances method.

    I still haven't figured out how to use JDI to connect to a running process and how to obtain an instance of ReferenceType. The JDK contains several examples, so I'm sure it's doable.

提交回复
热议问题