How does the JVM judge whether an object is alive

1. Reference counting algorithm (this algorithm is rarely used in mainstream virtual machines)

          Add a reference counter to the object. Whenever there is a reference to it, the counter value is increased by 1; when the reference fails, the counter value is decreased by 1; an object whose counter is 0 at any time cannot be used anymore.

          Disadvantages: it is difficult to solve the problem of circular references between objects

          For example: objA.instance=objB and objB.instance=objA, two objects refer to each other, and these two objects cannot be accessed again, but because they refer to each other, their reference counter values ​​are not 0. So the reference counter algorithm cannot notify the GC collector to reclaim them

2. Reachability analysis algorithm (mainstream virtual machine algorithm)

   Using the objects of GC Roots as the starting point, starting from these nodes and searching downwards, the searched path becomes the reference chain. When an object is connected to the GC Roots without any reference chain, it proves that the object is unusable.

          (Image source network)

Although the objects Object5-Object7 are still related to each other, they are not reachable to the GC Roots, so they will be judged as recyclable objects.

 

The objects that can be used as GC Roots include the following:

1. Referenced objects in the virtual machine stack

2. Objects referenced by static properties of the class in the method area

3. Objects referenced by constants in the method area

4. Objects referenced by Native methods in the native method stack

3.finalize() method finally reclaims the object

The finalize() method is a method of recycling objects of GC. This method of any object will only be automatically called once by the system.

Even objects that are unreachable in the reachability analysis algorithm are not necessarily "death". At this time, they are temporarily in the "probation" stage. To truly declare an object dead, at least it has to go through the process of marking again.

Mark once:

If an object finds that there is no reference chain connected to GC Roots after the reachability analysis, it will be marked for the first time and a filter will be performed. The filter condition is whether it is necessary for the object to execute the finalize() method.

When the object does not cover the finalize() method, or finalize() has been called by the virtual machine, both cases are deemed unnecessary, and will not escape during the second mark, and will be recycled directly

Second mark:

If this object is determined to be necessary to execute the finalize() method, it will be placed in a collection (F-Queue) that is about to be recycled. If the object is not reconnected with any object in the reference chain in finalize(), it will It is marked twice until it is recycled; if there is a connection, such as assigning itself to a class variable or member variable of an object, then the secondary object will not be recycled.

Reference book: "In-depth understanding of the Java virtual machine"

 

 

Guess you like

Origin blog.csdn.net/qq_32923295/article/details/114953279