Determine whether an object is dead---Java virtual machine



Determine whether an object is "dead" and whether it should be recycled—Java Virtual Machine

1. Judgment of "death" of objects in the Java heap

A large number of object instances are stored in the java heap, and the garbage collector GC is responsible for collecting garbage in the heap, but how do we judge whether an object has "dead" and should be recycled?

1.1 Reference counting algorithm

The reference counting algorithm uses a very easy-to-understand algorithm. Simply put, it is to add an object reference counter to an object, and every time a reference points to it, the counter is +1. On the contrary, if a reference no longer points to it, then the counter Just decrement by one. If an object's reference counter is 0. There is no reference to it, then the GC will collect it.

The principle of reference counting is simple and efficient. Although it takes up some extra space, it is still a good algorithm in general. However, it has many additional conditions to consider. This algorithm is not used in JVM. For example, Examples of errors:

public class GCTest
{
    
    
    public Object instance=null;

    public static void main(String[] args)
    {
    
    
        GCTest gcTest1=new GCTest();//这个对象实例现在有了一个引用
        GCTest gcTest2=new GCTest();//这个对象实例现在也有了一个引用
        gcTest1.instance=gcTest2;//这个对象实例现在又多了一个引用
        gcTest2.instance=gcTest1;//这个对象实例现在也又多了一个引用
        
        gcTest1=null;//对象实例减一
        gcTest2=null;//对象实例也减一
    }
}

It can be seen that after a series of operations on the two instance objects, their reference counters are not 0, but who can still obtain them? In other words, they do not meet the GC conditions of the reference counting method, but they Should be recycled.

1.2 Reachability analysis algorithm

The memory management subsystems of the current mainstream common programming languages ​​(Java, C#, etc.) use the Reachability Analysis algorithm to determine whether an object should be recycled. Ideas for reachability algorithm analysis The root object of a series of "GC Roots" is used as the starting node set, and the reference relationship of these nodes is searched from top to bottom. The entire search process is called the "reference chain". If An object is not in any reference chain, or in the terms of graph theory: the GCRoots vertex set is not reachable between the object point. In this way, it can be proved that the object cannot be used.

Insert picture description here

But even if an object is determined as a recyclable object in the reachability analysis, this does not mean that the object must be reclaimed. If an object is really "executed" in JVN, it will go through at most two trials .

  1. The first time is to analyze the reachability of the object. If it is unreachable, it will be marked once. Then the JVM will filter it once to determine whether the object needs to execute the finalize() method.
    1. If the object does not cover the finalize() method, or the finalize() method has been executed by the JVM, then the GC will reclaim the object and there is no need for a second trial
    2. If the object is determined to be necessary to execute the finalize() method, then it will get a "rebirth" opportunity. Go to the second step
  2. The object that gets the second chance will be placed in a queue named F-Queue, and later a low-priority Finalize thread automatically established by a virtual machine will execute their finalize() method. If the object is in When the finalize() method is executed, it successfully saves itself-establishes a connection with GC Roots. Then it comes to life.

2. Judgment of object "death" in method area

Compared with the garbage collection in the heap, the garbage collection in the method area can be said to be "cost-effective". It takes the same time and experience, and the GC harvest in the heap is much more than in the method area. This may also be Why is it not mandatory for the virtual machine to perform garbage collection in the method area in the <Java Virtual Machine Specification>. However, although the efficiency is low, things still need to be done. The method area mainly collects two parts:

  • Obsolete constant
  • Types no longer used

2.1 Judge the abandoned constant

The judgment of whether a constant is "dead" is somewhat similar to that of the centered object. If a constant in a constant pool

  • No object's value is it,
  • And there is no place to reference it in the virtual machine.

Then if the GC determines that it is necessary to collect it, it will be cleaned up

2.2 Determine whether a type is dead

If you want to judge whether a type belongs to a category that can no longer be used, this condition is relatively harsh.\

  1. All instances of the object have been recycled,
  2. The class loader that loaded the class has been recycled.
  3. The java.lang.Class object of this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere.

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109709779