views:

453

answers:

3

If I have two objects on the heap referring to each other but they are not linking to any reference variable then are those objects eligible for garbage collection?

+5  A: 

Check this out: How does Java Garbage Collector Handle Self References.

You may want to check java.lang.ref.WeakReference

Hosam Aly
+15  A: 

Yes, they are. Basically the GC walks from "known roots" (static variables, local variables from all stack frames in alll threads) to find objects which can't be garbage collected. If there's no way of getting to an object from a root, it's eligible for collection.

EDIT: Tom pointed this out, which I thought was worth lifting into the answer itself:

Technically, static variables are not roots - they are referenced by classes which are referenced by class loaders which are referenced by classes which are referenced by object which are referenced by root references.

The difference is likely to be irrelevant most of the time, but it's good to know :)

Jon Skeet
Technically, static variables are not roots - they are referenced by classes which are referenced by class loaders which are referenced by classes which are referenced by object which are referenced by root references.
Tom Hawtin - tackline
@Tom: Good call. Editing...
Jon Skeet
+4  A: 

Skeet's on the money, as usual. I would only add that the situation you describe is the reason that reference counting (a standard strategy with early C++ smart pointers) isn't used.

Jim Nelson