Almost all of the object instances in the Java world are in the JVM heap, and the first thing the garbage collector can do before reclaiming heap memory is to determine which of these objects are still alive and which ones are dead (that is, objects that can no longer be used by any means).
In the mainstream business programming language, Java and C # are used to determine whether an object survives using the root search algorithm (GC Roots tracing). The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain, when an object to the GC Roots no reference chain connected (from the GC Roots to this object unreachable), proves that this object is not available.
The objects that can be used as GC roots in Java include the following:
1. The object referenced in the virtual machine stack (local variable table in the stack frame);
2. The object referenced by the class static property in the method area;
3, the object in the method area constant reference;
4. The object referenced by the native method in the local method stack;
If the object finds no reference chain connected to the GC roots after the root search, it will be first marked and filtered, and the condition is whether this object is necessary to execute the Finalize () method when the object does not overwrite the Finalize () method, or Finalize () Method has been called by the virtual machine, and the virtual machine treats both cases as "no need to execute".
If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue named F-queue, and then executed by a low-priority finalizer thread that is automatically created by the virtual machine at a later time. Later, the GC will mark the object in the F-queue for a second small size, and if the object is to successfully save itself in Finalize ()-as long as it is associated with any object on the reference chain, it will be removed from the "soon to be recycled" collection when the second token is taken.
Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source: Http://blog.csdn.net/lingzhm
Java Virtual machine garbage collection process