JVM Memory Management: Introduction to GC

Source: Internet
Author: User

Why do we need to understand GC policies and principles?

The reason in the previous chapter has actually touched, is because in peacetime work and research, inevitably will encounter the memory overflow and the memory leaks the question. If you are not aware of the GC strategy and principles of the problem encountered before, many times it will be overwhelmed.

When we understand the relevant knowledge, although sometimes still can not solve the problem quickly, but it is certain that at least there will be no way out of the situation.

What problems does the GC policy solve?

Since it is to be automated GC, there must be a corresponding strategy, and these strategies to solve the problem, roughly speaking, mainly the following points.

1, which objects can be recycled.

2. When to recycle these objects.

3, what kind of way to recycle.

What algorithm is used in GC policy

For the three questions mentioned above, one of the most important questions is the first, which are the objects that can be recycled.

There is a relatively simple and intuitive approach, it is more efficient, known as reference counting algorithm. However, this algorithm has a fatal flaw, that is, the circular reference to the object can not be recycled. Imagine that the JVM uses this GC strategy, and the following code is not expected to appear when the ape is writing a program.

Public class Object {
    
    object field = null;
        
    public static void Main (string[] args} {
        thread thread = new Thread (new Runnable () {public
            void run () {
                objec T objecta = new Object ();
                Object OBJECTB = new Object ();//1
                Objecta.field = OBJECTB;
                Objectb.field = OBJECTA;//2
                //to do something
                objecta = null;
                OBJECTB = Null;//3
            }
        });
        Thread.Start ();
        while (true);
    }
        
}

This code seems a bit deliberate, but in fact, in the actual programming process, is often appear, such as two one-to-one relational database objects, each holding each other's references. The last infinite loop just to keep the JVM from quitting doesn't make sense.

For the GC we now use, when the thread thread runs, all objecta and OBJECTB are treated as objects to be reclaimed. And if our GC uses the reference counting algorithm mentioned above, then these two objects will never be recycled, even if we display the objects as null values after use.

LZ here to explain roughly, in the code LZ labeled 1, 2, 33 digits, when the 1th place after the execution of the statement, two objects reference count of 1. When the statement in the 2nd place is executed, the reference count of two objects becomes 2. When the statement in the 3rd place is finished, that is, after the two are all null, the reference count for both is still 1. According to the collection rule of the reference counting algorithm, the reference count is not recycled when it is not 0.

Root Search algorithm

Because of the drawbacks of the reference counting algorithm, the JVM generally uses a new algorithm called the root search algorithm. It is handled by setting up a number of root objects that can be reclaimed when any one of the root objects is unreachable to an object.

For example, OBJECTD and objecte are interrelated, but because GC roots to these two objects, eventually D and E will still be treated as GC objects, and if the above illustration uses reference counting, then a-e five objects will not be reclaimed.

Speaking of GC roots (GC root), in the Java language, there are several objects that can be used as GC roots:

1. The referenced object in the virtual machine stack.

2, the object referenced by the class static property in the method area.

3, the object referenced by the constants in the method area.

4. The object referenced by JNI in the local method stack.

Both the first and the fourth refer to the local variable table of the method, the second meaning is more clear, and the third refers to the constant value declared 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 is which objects can be recycled.

But garbage collection clearly needs to address the latter two issues, when to recycle and how to recycle. Based on the root search algorithm, in the realization of modern virtual machines, there are three kinds of algorithms for garbage collection, namely, tag-purge algorithm, replication algorithm, labeling-sorting algorithm. These three algorithms all extend the root search algorithm, but they are very well understood.

Conclusion

Limited to the article length not too long, this time does not specifically introduce three kinds of garbage collection algorithm, the next chapter and you discuss.

Author: zuoxiaolong (Zao Jianrong)

Source: Blog Park Zao Jianrong Technology Blog--http://www.cnblogs.com/zuoxiaolong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.