JVM Interview Review Series: How to determine garbage during JVM garbage collection? Do you know what GC Roots are

JVM interview summary

How to determine garbage during JVM garbage collection? Do you know what GC Roots are

What is rubbish

Simply put, the space in the memory that is no longer used is garbage

How to judge whether an object can be recycled

Reference counting

In Java, references and objects are related. If you want to manipulate objects, you must use references.

Therefore, it is obvious that a simple way is to judge whether an object can be recycled by reference counting. Simply put, add a reference counter to the object

Whenever there is a place to reference it, the counter value is increased by 1

Whenever a reference fails, the counter value is decreased by 1

The object whose counter value is zero at any time cannot be used anymore, then this object is a recyclable object.

So why is this method not used in mainstream Java virtual machines? The main reason is that it is difficult to solve the problem of circular references between objects.

This algorithm exists but no one has used it at present, and it can't solve the problem of circular references, just understand it.

image-20200318213301603

Enumerate root nodes for reachability analysis

Root search path algorithm

In order to solve the circular reference problem of the reference counting method, Java uses the method of reachability analysis:

image-20200319113611244

The so-called GC Roots or the "root set" of Tracing Roots is a set of references that must be active

The basic idea is to use a series of objects named GC Roots as the starting point, starting from this object called GC Roots and searching downwards. If an object is not connected to the GC Roots by any reference chain, it means that this object is not available. That is, given a set of references as the root, traverse the object graph through the reference relationship, the objects that can be traversed (reachable) are judged to be alive, and the objects that are not traversed are judged to be dead.

image-20200319114526625

You must start with the GC Roots object, which is similar to Linux / which is the root directory

The blue part is starting from GC Roots, which can be cyclically reachable

The white part, starting from GC Roots, cannot be reached

Understand GC Roots in one sentence

Suppose we now have three entities, namely human, dog, and sweater

Then the relationship between them is: people lead the dog, and the dog wears a sweater. They are strongly connected.

One day people disappeared, leaving only dogs and sweaters. At this time, think of people as GC Roots, because the rope connection between people and dogs is lost.

Then the dog may be recycled, that is, caught by the police and sent to a stray dog ​​boarding facility

Assuming a strong connection between a dog and a person, the dog will not be regarded as a stray dog

Those objects can be used as GC Roots

  • Reference objects in the virtual machine stack (local variable area in the stack frame, also called the local variable table)
  • Objects referenced by static properties of the class in the method area
  • Objects referenced by constants in the method area
  • Reference object of JNI (Native method) in the native method stack

Code description


/**
 * 在Java中,可以作为GC Roots的对象有:
 * - 虚拟机栈(栈帧中的局部变量区,也叫做局部变量表)中的引用对象
 * - 方法区中的类静态属性引用的对象
 * - 方法区中常量引用的对象
 * - 本地方法栈中的JNI(Native方法)的引用对象
 */
public class GCRootDemo {
    
    


    // 方法区中的类静态属性引用的对象
    // private static GCRootDemo2 t2;

    // 方法区中的常量引用,GC Roots 也会以这个为起点,进行遍历
    // private static final GCRootDemo3 t3 = new GCRootDemo3(8);

    public static void m1() {
    
    
        // 第一种,虚拟机栈中的引用对象
        GCRootDemo t1 = new GCRootDemo();
        System.gc();
        System.out.println("第一次GC完成");
    }
    public static void main(String[] args) {
    
    
        m1();
    }
}

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/110309027