views:

137

answers:

5

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.

A: 

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

Matthew Flaschen
GC needs to know which references exist to an Object to know whether to garbage collect it or not.
notnoop
@msaeed: No, the GC needs to know if the object is referenced or not, it doesn't need to know anything about which objects that is keeping a reference, if it finds one reference during mark (depending on gc strategy of course) the objects stays alive and that it all it needs to know.
Fredrik
@Fredrik: So how do you explain circular references being garbage collected?
zakovyrya
@Fredrik: as zakovyrya, to handle circular references, GC keeps a object reference graph.
notnoop
@zakovyrya: It is a bit hard to do in 600 characters... but trust me, it doesn't need to do it like that. A circular reference will be gc:ed simply because if none of the objects in the chain with the circular reference is reachable from a GC root during the mark phase none of them are marked. As a result, all of them can be collected despite the fact that they are referencing each other.The following is a rather good link on how things works: http://chaoticjava.com/posts/how-does-garbage-collection-work/
Fredrik
It doesn't _keep_ a reference graph. It walks down from the roots marking objects as it goes, yes, but it doesn't need to preserve the information about who references what.
Pavel Minaev
@msaeed: No, it is how you think it needs to do it but it is not how it works. It IS however exactly what JHat and MemoryAnalyzer do when they analyze the heapdumps. Theoretically I guess a GC could work like you think it does but it would be dead slow and you would see full stops lasting for minutes and get nothing good out of it compared to how GCs have worked for over a decade now.
Fredrik
+2  A: 

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.

Harold L
+1  A: 

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
Rich Seller
A: 

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.

Fredrik
A: 

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.

Stephen C