views:

165

answers:

3

I have a Java application that is leaking memory. I know which objects are not being freed during garbage collection, but I can't work out what is referencing them.

Is it at all possible to have some sort of visibility of the object graph that is being held internally by the JVM?

It is at all otherwise possible to find out which objects are referencing another object?

A: 

Profilers do this. JProfiler for example. Back in the day I used Optimizeit (which seems to be dead now).

The conclusion was that listeners were being passed to some collection and not being removed from that collection. The listeners were anonymous inner classes defined on a Frame or a corresponding control class that referenced the Frame, and an anonymous inner class has an implicit reference to its outer class. So that little listener held the whole frame and all its attendant objects in active memory.

Yishai
+2  A: 

On a really basic level, you can use the commands jhat and jmap to read a heap file from a running Java process and then process it - it starts a small web server on a local port. It's not exactly easy to read, but you don't have to buy a profiler to use it.

Mat Mannion
Wow. That's awesome.
izb
A: 

JProbe has the ability to visualise the object graph. It then allows you to delete references to see if that frees up the object for garbage collection. It is expensive but I have found it extremely useful on more than one occasion.

Mark