Home  >  Article  >  Java  >  JVM memory management------Introduction to GC

JVM memory management------Introduction to GC

黄舟
黄舟Original
2016-12-28 15:31:201513browse

Why do we need to understand GC strategies and principles?

The reason has actually been touched upon in the previous chapter. It is because in daily work and research, it is inevitable to encounter memory overflow and memory leak problems. If you encounter the problems mentioned above without understanding the GC strategy and principles, it will often make people feel at a loss.
After we understand the relevant knowledge, although sometimes we still cannot solve the problem quickly, what is certain is that at least we will not be at a loss.

What problems does the GC strategy solve?

Since automatic GC is to be carried out, there must be corresponding strategies. What problems do these strategies solve? Roughly speaking, the main points are as follows.
1. Which objects can be recycled.
2. When to recycle these objects.
3. What kind of recycling method is used.

[b]What algorithm is used in the GC strategy[/b]

Regarding the three questions mentioned above, in fact, the most important question is the first one, which is which Objects are recyclable.
There is a relatively simple and intuitive method, which is more efficient and is called the reference counting algorithm. However, this algorithm has a fatal flaw, that is, it cannot recycle objects with circular references. Imagine that if the JVM adopts this GC strategy, then when programmers write programs, code like the following should not be expected to appear again.

public class Object {  
    Object field = null;  
      
    public static void main(String[] args) {  
        Thread thread = new Thread(new Runnable() {  
            public void run() {  
                Object objectA = new Object();  
                Object objectB = new Object();//1  
                objectA.field = objectB;  
                objectB.field = objectA;//               //to do something  
                objectA = null;  
                objectB = null;//3  
            }  
        });  
        thread.start();  
        while (true);  
    }  
      
}

This code seems a bit deliberate, but in fact, it often occurs in the actual programming process, such as two database objects in a one-to-one relationship, each maintaining a reference to the other. The last infinite loop is just to keep the JVM from exiting, and has no practical significance.
For the GC we are using now, when the thread thread ends, both objectA and objectB will be used as objects to be recycled. And if our GC adopts the reference counting algorithm mentioned above, these two objects will never be recycled, even if we explicitly classify the objects as null after use, it will have no effect.
Here is a rough explanation from LZ. In the code, LZ marked three numbers: 1, 2, and 3. After the statement in the first place is executed, the reference counts of the two objects are all 1. When the statement in the second place is executed, the reference counts of the two objects all become 2. After the statement in the third place is executed, that is, after both are classified as null values, the reference counts of the two are still 1. According to the recycling rules of the reference counting algorithm, the reference count will not be recycled until it reaches 0.

Root search algorithm

Due to the flaws of the reference counting algorithm, the JVM generally uses a new algorithm called the root search algorithm. Its processing method is to set up several root objects. When any root object is unreachable to a certain object, the object is considered to be recyclable.

JVM memory management------Introduction to GC

Take the above picture as an example. ObjectD and ObjectE are related to each other. However, since GC roots are not reachable to these two objects, D and E will still be deleted in the end. As GC objects, if the reference counting method is used in the above figure, none of the five objects A-E will be recycled.
Speaking of GC roots (GC roots), in the JAVA language, the following objects can be used as GC roots:
1. Objects referenced in the virtual machine stack.
2. The object referenced by the class static property in the method area.
3. The object referenced by the constant in the method area.
4. The object referenced by JNI in the local method stack.
The first and fourth methods both refer to the local variable table of the method. The second expression has a clearer meaning. The third mainly refers to the constant value declared as final.

Garbage Collection Algorithm

The root search algorithm solves the basic problem of garbage collection, which is the first problem mentioned above and the most critical problem, which objects can be recycled .
However, garbage collection obviously still needs to solve the last two problems, when to recycle and how to recycle. Based on the root search algorithm, there are three main garbage collection algorithms in the implementation of modern virtual machines, namely the mark-clear algorithm, the copy algorithm, and the mark-sort algorithm. These three algorithms all extend the root search algorithm, but they are still very easy to understand.

Conclusion

The above is the content of JVM memory management------Introduction to GC. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn