jvm knowledge points - GC knowledge points

First, how to determine whether an object is alive two methods

  1), reference counting method:

  The so-called reference counting method is to set a reference counter for each object. Whenever another object refers to this object, the reference counter is incremented by one. Whenever the reference fails, the reference counter is decremented by one. When the reference counter is decremented to zero , indicating that this object is a "dead object" and can be recycled by GC,

  Of course, the reference counter is also flawed. His flaw is that it cannot solve the problem of circular references. For example, when object A refers to object B, and object B also refers to object A, the reference counters of objects A and B are not zero, but the two objects are not "Dead objects" referenced by other objects cannot be recycled according to the reference counter rules.

  2) Reachability algorithm (reference chain method)

   The idea of ​​​​the algorithm is: start from an object called GC Roots to search down, when the whole is searched, there are other objects that have not been found, then these remaining objects are "dead objects"

 There are the following objects that can be used as GC Roots in java

Objects referenced in the virtual machine stack

The object referenced by the static property in the method area

The object referenced by the constant pool in the method area

Objects referenced by the native method stack JNI

Although these algorithms can determine whether an object has been reclaimed, when the above conditions are met, an object may not be reclaimed. When an object cannot reach the GC Root, the object will not be reclaimed immediately, but will be in a stage of death delay. There are two marking phases to be recycled

If the object does not have a reference chain with the GC Root in the reachability analysis, then it will be marked and screened for the first time. During the screening process, it is judged whether it is necessary to execute the finalize() method. If the current object does not override finalize() When the method is called or has been called by the jvm, the name does not need to be executed.

If it is necessary for the object to execute the finalize() method, then the object will be placed in a queue called F-Queue, the virtual machine will trigger a finalize thread to execute, this thread has a low priority, and the virtual machine will not promise to always Waiting for the operation to complete, this is because if finalize() is slow or deadlock occurs, it will cause the F-Queue queue to wait all the time, causing the memory recovery system to crash. The GC marks the object in the F-Queue for the second time. At this time, the object will be removed and the collection will be collected, waiting for recycling.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326978882&siteId=291194637