I was wondering how the garbage collector in Java deals with the following situation.
Object A has a reference to Object B and Object B has a reference to Object C. The
In fact, garbage collection in java is a very sophisticated thing, far more than in Ruby interpreter, as an example.
Anyway, the theoretical basis is the same.
The GC identifies objects graphs that are no more reachable by program code (that's to say they have no more reference in active code). When talking about object graph, I precisely talk about B->C object graph. once it is unreachable, it can be GC'ed, but you can't tell when it will be, due to the GC trying to optimize as much as possible its work to avoid slowing the application down.
Should Object B and Object C now been collected by the Garbage Collector?
Yes. Well, they are candidates for collection because there's no way to reach Object B and C through the root that is A.
You can't count on the garbage collector to work at a specific time,since its behavior is unpredictable,all you can say is that objects B and C are only eligible for garbage collection