JVM review---determine whether the object is alive

There are two methods for judging whether an object is alive: reference counting method and reachability analysis method.

Reference counting

This algorithm logic is very simple, but Java does not use this method, because this method has fatal problems.

The logic of the reference counting method is: when storing objects in the heap, each object has an object header, and a counter is maintained in the object header. Whenever there is a reference to this object, it will counter++, and when the reference is broken, the counter— . If the counter is 0, it means that there is no place to reference and it can be GC.

However, if object A refers to object B, and object B refers to object A, then no other objects refer to it, so that both of them can be GC, but since Counter is always equal to 1, these two objects will always be GC. Will not be recycled.

Accessibility analysis

This is the algorithm currently used by JVM. His logic is to first define a series of objects, called GC Roots, and then start from GC Roots, search down all the objects, if you find that there is no reference chain from an object to GCRoots, it proves the object If it is unusable, it can be recycled. Even in the case of A and B above, if only A and B do not reach the GC Roots, they will be recycled.

GC Roots:

  • Referenced objects in the virtual machine stack (local variable table in the stack frame)
  • Objects referenced by static properties of the class in the method area
  • Objects referenced by constants in the method area
  • Object referenced by JNI (Native method) in the native method stack

Survive or die

Although the object is unreachable after the accessibility analysis, it does not mean that the object must be dead. At this time, he is in a state of probation, and he will still mark it twice before death:

  1. If after the reachability analysis, it is found that this object does not have a reference chain with GC Roots, then it will be marked and filtered for the first time at this time. The filtering condition is whether the object needs to execute the finalize method, and if the object is not covered The finalize method or has been executed, it is deemed unnecessary, then it will die
  2. If the finalize method is overridden, the object will be put into a queue called F-Queue, which will be executed later by a low-priority Finalizer thread automatically created by a virtual machine, and this method will be triggered at this time , But it does not mean that the virtual opportunity promises to let his finalize method be executed. This is because if the finalize execution time of a certain object is too long, it will affect the finalize execution of all subsequent objects and cause the entire memory recovery system to crash.

Therefore, the finalize method is the last chance for the object to escape death. As long as it rewrites the finalize method and reattaches it to any reference chain of GC Roots, it will not be killed. At this time, it will be removed from the collection that is about to be recycled when it is marked for the second time. If the object has not escaped at this time, it proves that it can really be recycled.

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114703013