Two determination methods of GC

1. Reference counting method

If the object is referenced somewhere, it will be +1. If it is invalid, it will be -1. When it is 0, it will be recycled. However, the JVM does not use this method because it cannot determine the situation of mutual circular references (A refers to B, B refers to A).

 

 

2. Reference chain method (accessibility analysis)

Through a GC ROOT object (object referenced in the virtual machine stack (local variable table in the stack frame), object referenced by a static attribute in the method area, object referenced by a constant in the method area, and JNI in the local method stack (that is, Generally speaking, the Native method) refers to the object). If there is a chain that can reach the GC ROOT, it means that the object is still being referenced. If it cannot reach the GC ROOT, it means that the object is no longer referenced and can be recycled.

Reachable:

 

 Unreachable:

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/12687886.html