Java – what if you only reference objects from garbage?
Suppose there is an object a of class A, which holds a reference to another object B of class B This is the only reference for B So now, if all references to a are deleted, a can use GC Does this mean that B is also ready to collect garbage? Because, although B has a reference (within a), it is unattainable because a is unattainable
So how does this work? I mean the order of garbage collection
Solution
Once an object is inaccessible from the root directory, it is collected For a description of the GC root, see this question
The entire subgraph will be collected (as you described), assuming that there are no reachable nodes in the subgraph
Java (and. Net) uses tag and scan garbage collection to deal with this problem
A system based on a reference count (for example, STD:: shared_ptr of C) when the cyclic dependency remains unreachable
Reprint the original text: Java - if you only reference objects from garbage- Code log