When the object is not hungry referenced, the object is dead, waiting for the GC to be recycled.
1. Reference counting method
Concept:
Add a reference counter to the object, the counter value increases by 1 whenever there is a reference to it, and when the application fails, the counter value is reduced by 1, and any time the counter is 0 is an object that cannot be used again.
But:
Mainstream Java virtual machines do not have a reference counter algorithm, one of the main reasons is that it is difficult to solve the object is mutually circular reference.
Advantages: The algorithm is simple to implement, the decision efficiency is high, most of the cases is a good algorithm. Many places to apply it
Disadvantages:
References and de-referencing accompany wigs and subtraction, affecting performance.
Fatal flaw: The object that is referenced by the loop cannot be reclaimed.
2. Root search algorithm (the algorithm used by the JVM)
Concept: A number of root objects are established, and when any one of the root objects (GC root) is unreachable to an object, the object is considered to be recyclable.
Note: It is mentioned here that a number of root objects are established, and when any one of the root objects is unreachable to an object, the object is considered to be recyclable.
Accessibility Analysis:
Starting from the root (GC root) object as the starting point, the search is traversed by a path called a "reference chain", which proves that this object is not available when an object is connected to the GC root without any reference chain (in the sense of cam, from GC root to the object unreachable).
As shown, OBJECTD and objecte are interrelated, but because GC root is not available to these two objects, eventually D and E will still be treated as GC objects, and if the reference counting method is used, none of the A-e five objects will be recycled.
Root (GC roots)
When it comes to GC roots (GC root), in the Java language, there are several objects that can be used as GC root:
1. The object referenced in the stack (local variable table of the stack frame)
2. Static members in the method area.
3. Constants in the method area reference objects (global variables)
4. The object referenced by JNI (generally referred to as the native method) in the local method stack.
Note: The first and the four refer to the local variable table of the method, the second expression is clearer, and the third is the constant value of final life.
Based on the root search algorithm, there are three kinds of garbage collection algorithms in the virtual machine implementation, which are tag-clear algorithm, copy algorithm and tag management algorithm. These three algorithms have expanded the root search algorithm, but they are well understood.
Two common algorithms for judging the death of Java objects