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

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.

Leave a Comment