Sobes.tech
Senior

If several objects refer to each other in a circle but are not used anywhere else, will the GC delete them?

sobes.tech AI

Answer from AI

Yes, the garbage collector (GC) in Java will remove objects that refer to each other in a cycle if there are no other references to them from live objects (roots).

GC in Java works based on reachability tracking: objects are considered alive if they can be reached from root objects (stack, static fields, etc.). If a group of objects refers to each other but is unreachable from the roots, then the entire group is considered garbage and will be collected.

Thus, cyclic references do not prevent garbage collection, unlike some other memory management systems (for example, reference counting without additional cycle detection).