Let's start with the idea of the Accessibility analysis algorithm: Starting with an object called GC roots, if an object is connected to a GC roots without any reference chain, then this object is not available.
There are several types of objects that can be used as GC roots in Java:
Objects referenced in the virtual machine stack, object referenced by the method area class static property, objects referenced by the method area constant pool, objects referenced by the local method stack JNI reference
Although these algorithms can determine whether an object can be recycled, an object may not necessarily be recycled when the above conditions are met. When an object is not up to GC roots, this object is not immediately recycled, but is in a reprieve phase, and it takes two tokens to be truly recycled. If the object does not have a reference chain to the GC roots in the accessibility analysis, it is then first flagged and filtered for the condition that it is necessary to execute the Finalize () method. When an object does not overwrite the Finalize () method or has been called by a virtual machine, it is considered unnecessary.
If the object is necessary to execute the Finalize () method, then this object will be placed in a queue called F-queue, the virtual machine can trigger a finalize () thread to execute, the thread is low priority, and the VM will not commit to wait until it runs out. This is due to the fact that if finalize () performs slowly or a deadlock occurs, it causes the F-queue queue to wait, causing the memory recovery system to crash. The GC is marked for the second time in an object in F-queue, and the object is removed from the collection that is about to be recycled, waiting to be reclaimed.