JVM5: How does GC determine whether an object is garbage

 

Find objects in memory that are no longer used

  • Reference counting

The reference counting method is that if an object is not pointed to by any reference, it can be regarded as garbage. The disadvantage of this method is that the existence of the ring cannot be detected.

  • 2. Root search algorithm

The basic idea of ​​the root search algorithm is to use a series of objects named "GC Roots" as the starting point, starting from these nodes and searching downwards. The path traversed by the search is called the Reference Chain.

When an object is not connected to the GC Roots by any reference chain, it proves that the object is unusable.

 

Reference counting

The following comparison illustrates through a piece of code:

1

2

3

4

5

6

7

8

9

10

11

public class MyObject {

    public Object ref = null;

    public static void main(String[] args) {

        MyObject myObject1 = new MyObject();

        MyObject myObject2 = new MyObject();

        myObject1.ref = myObject2;

        myObject2.ref = myObject1;

        myObject1 = null;

        myObject2 = null;

    }

}

 In the above code, myObject1 and myObject2 actually refer to each other. Their reference counts are both 1, but they are all null. If the reference counting method is used because the count is 1, they will not be reclaimed by the GC, but they themselves are null, which eventually leads to a memory leak.


If the reference counting algorithm is used:

return to the previous code. The main method of GcDemo is divided into 6 steps:

  • Step1: The reference count of GcObject instance 1 is incremented by 1, and the reference count of instance 1 is =1;
  • Step2: The reference count of GcObject instance 2 is incremented by 1, and the reference count of instance 2 is =1;
  • Step3: The reference count of GcObject instance 2 is increased by 1, and the reference count of instance 2 = 2;
  • Step4: The reference count of GcObject instance 1 is increased by 1, and the reference count of instance 1 = 2;

Step 4 is executed, then the reference counts of GcObject instance 1 and instance 2 are both equal to 2.

Next, continue to the result graph:

  • Step5: Obj1 in the stack frame no longer points to the Java heap, the reference count of GcObject instance 1 is decremented by 1, and the result is 1;
  • Step6: Obj2 in the stack frame no longer points to the Java heap, the reference count of GcObject instance 2 is decremented by 1, and the result is 1.

At this point, it is found that the counted references of GcObject instance 1 and instance 2 are not 0, so if the reference counting algorithm is adopted, the memory occupied by these two instances will not be released, which results in a memory leak.

 


Root search algorithm
This is the current mainstream virtual machine adopts the GC Roots Tracing algorithm, for example, Sun's Hotspot virtual machine uses this algorithm. The core algorithm of the algorithm is based on the GC Roots object as the starting point. Using the knowledge of graph theory in mathematics, the reachable object in the graph is the living object, and the unreachable object is the garbage memory that needs to be recycled. Two concepts are involved here, one is GC Roots and the other is reachability.

Then it can be used as the object of GC Roots (see the figure below):

  • Objects referenced by the local variable table of the stack frame of the virtual machine stack;
  • The object referenced by the JNI of the local method stack;
  • Objects referenced by static variables and constants in the method area;

The object of reachability is the object that can form a connected graph with GC Roots, as shown in the following figure:


From the above figure, reference1, reference2, and reference3 are all GC Roots, you can see:

  • reference1-> Object instance 1;
  • reference2-> Object instance 2;
  • reference3-> Object instance 4;
  • reference3-> Object instance 4 -> Object instance 6;

 

It can be concluded that object instances 1, 2, 4, and 6 all have GC Roots reachability, that is, surviving objects, objects that cannot be recycled by GC.
For object instances 3 and 5, although they are directly connected, there is no GC Roots connected to them. This is an object that is not reachable by GC Roots, and this is a garbage object that GC needs to recycle.

At this point, I believe you should be able to fully understand the difference between the reference counting algorithm and the root search algorithm .

Looking back at the first instance again, although GcObject instance 1 and instance 2 are not 0 from the reference count, from the perspective of the root search algorithm, they are all objects that are unreachable by GC Roots.

The metadata area is introduced after jdk8 , and the objects in the same metadata area are not reachable to the  method area, program counter, virtual machine stack, and local method stack are garbage


In short, for the case of circular references between objects, the reference counting algorithm, the GC cannot reclaim these two objects, while the root search algorithm can correctly reclaim them.

 

Transfer from: Zhihu Gityuan homepage https://www.zhihu.com/people/gityuan/answers

Guess you like

Origin blog.csdn.net/zhaofuqiangmycomm/article/details/113868905
Recommended