Skip to content

In java, is there a way to examine how an object is still reachable from garbage collection root?

The codebase is somewhat complex, so I was wondering if there was a tool to examine an object at runtime and determine how the JVM still sees this object as reachable.

In particular, there’s one object type that I would only expect to be one instance of at a time throughout the life of the app (new ones can be created, but the old instance should be garbage collected when this happens). However, when I examine the heap, I see that multiple instances of this object type are accumulating. The class is pretty complex, so it’d be fairly time-consuming to track down what is still referencing the old object instances. Anyone know of a tool, or a way in java, to figure out why these objects can’t be collected?



JProfiler has such a view: You can follow the referring objects up till the root.

And it has a lot of other useful debugging views, so I’ve always considered it worth the price tag.