I. Recoverable object judgment
Reference counter algorithm: Place a reference counter for an object, add a counter when the object is referenced, or, if the counter of an object is identified as zero, that object can be recycled. This is a simpler approach, but it is not possible to resolve the case where objects are referenced to each other.
GC Roots algorithm: This method can solve the problem that the object in the above algorithm cannot judge the recoverable state by reference to each other. As the name implies, GC root refers to a program using objects, there will always be a "portal", which can be a local variable reference in the stack, it can also be a class static property reference in the method area, a constant reference, a JNI Java reference. When an object is not available to all GC roots along the object reference chain, this proves that the object is not accessible to the GC roots, which is the "ingress" that the object does not use, and is naturally recyclable.
(Figure 1:GC Roots algorithm)
1, when the Root3=null, OBJECT5, 6, 7 to all root unreachable, even if OBJECT6 and Object7 are mutually referential relationship can also be judged to be recyclable status. If you want to reclaim Object2, you need the reference chain for that object to ROOT1 and Root2 to be broken before you can reclaim it.
Second, the process of object recycling
(Figure 2:java Object recycling Flowchart)The object recycling process, as shown, two points to note are:1, an object GC roots is not reachable and has implemented a finalize method can be recycled;2. The Finalize method of an object will only be executed once;
Java Object Recycling Process