Allain Lalonde
Allain Lalonde

Reputation: 93348

Getting list of objects referring to an Object

Given a Java Object, how can I get a list of Objects that referring to it?

There must be extension mechanisms in the GC for doing this kind of thing, just can't seem to find them.

Upvotes: 3

Views: 360

Answers (5)

Stephen C
Stephen C

Reputation: 718886

The GC does not support this, though the JDPA APIs do. But I'd be very cautious about doing this kind of thing in a Java application. It is likely to be prohibitively expensive in both time and memory.

Upvotes: 0

Rich Seller
Rich Seller

Reputation: 84038

I'm not sure if exactly what you're after is simply accessible.

The JPDA (Java Platform Debugger Architecture) enables construction of debuggers, so is a good starting point if you want to delve into the internals. There's a blog on the JPDA that you may also find useful. Check out the Sun Developer Network JPDA page for links to documentation, FAQs, sample code and forums.

Two interfaces that may be good starting points are:

  • com.sun.jdi.ObjectReference: An instance of java.lang.Class from the target VM
  • com.sun.jdi.VirtualMachine: A virtual machine targeted for debugging

Upvotes: 2

Fredrik
Fredrik

Reputation: 5839

It depends a little bit on how you want to use it but if you need it to analyze your memory usage, taking a heapdump and open it in MemoryAnalyzer or JHat will probably give you the information you need. Different ways of taking a heapdump can be found here.

Upvotes: 0

Harold L
Harold L

Reputation: 5254

If you're looking for a memory leak, I find analyzing heap dumps with Eclipse MAT to be very helpful. You can select an object and ask for paths to "GC roots", i.e. show me all chains of references that are keeping this object from being garbage collected.

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284836

I don't think there is such a mechanism, and there is no real reason the GC would need one.

Upvotes: 0

Related Questions