In-depth understanding of JVM (4) - garbage collection algorithm

We all know that the biggest difference between java language and C language is automatic memory recycling, so how does JVM control memory recycling, this article will introduce several algorithms of JVM garbage collection, so as to understand the basic principles of memory recycling.

stop the world

    Before introducing the garbage collection algorithm, we need to understand the word "stop the world". Stop the world will be generated when a certain garbage collection algorithm is executed. In order to perform garbage collection, the JVM will temporarily execute the java application and wait for garbage. After recycling is complete, continue running. If you have used JMeter to test a java program, you may find that during the test, the java program has irregular pauses. In fact, this is "stop the world", and the JVM is doing garbage collection when it pauses. So reducing the time to stop the world as much as possible is our main goal of optimizing the JVM. Next, let's take a look at the common garbage collection algorithms currently available.

reference counting

The reference counting method, as the name implies, is to count the number of times an object is referenced. When the reference count is increased by one, it is increased by 1, and when the reference count is decreased by one, it is decreased by 1.

image

The above figure shows that the references of 3 Teachers point to the Teacher object in the heap, then the reference count of the Teacher object is 3, and so on, the reference count of the Student object is 2.

image

 

The above figure shows that the reference of the Teacher object is reduced to 2, and the reference of the Student object is reduced to 0 (the reason for the reduction is that the reference points to null, such as teacher3=null). According to the reference counting algorithm, the memory space of the Student object will be reclaimed.

The principle of the reference counting algorithm is very simple, and it is the most primitive recycling algorithm, but this algorithm is not used in Java for 2 reasons. 1. Frequent counting affects performance, 2. It cannot handle the problem of circular references.

For example, the Student object is referenced in the Teacher object, and the Teacher object is referenced in the Student object. In this case, the object will never be recycled.

mark clear

The mark-and-sweep algorithm, which is the basis of many garbage collection algorithms, simply has two steps: mark and clear.

Mark: Traverse all GC Roots and set objects reachable from GC Roots as live objects;

Clear: traverse all objects in the heap, and clear the objects that are not marked reachable;

image

Pay attention to the gray objects in the picture above, because they cannot be traversed from the GC Root (although they have a reference relationship, they cannot be traversed from the GC Root), so they are not marked as live objects and will be recycled during the cleanup process. .

It should be noted here that during the execution of the mark clearing algorithm, "stop the world" will be generated, and the java program will pause and wait to ensure that no new objects will be generated during the mark clearing process. Why must the java program be suspended? For example, if a new object is generated after the marking process is completed, and the object has missed the marking period, then in the subsequent clearing process, the newly generated object will be regarded as unmarked because it is not marked. is cleared for unreachable objects, so the program will fail, so while the mark-sweep algorithm is executing, the java program will be suspended, resulting in "stop the world".

Next we summarize the mark removal algorithm:

1. Because a large amount of memory traversal work is involved, the execution performance is low, which will also lead to a long "stop the world" time and a reduction in the throughput of the java program;

2. We noticed that after the object is cleared, the cleared object leaves a vacant position in the memory, resulting in discontinuous memory and a waste of space.

Next, let's see if other algorithms can improve these problems?

tag compression

You may have already thought of the mark compression algorithm, which is the addition of the compression process on the basis of the mark removal algorithm.

image

After the mark clearing is completed, the memory space is compressed to save the memory space and solve the problem of discontinuous memory of the mark clearing algorithm.

Note that the markup compression algorithm also produces "stop the world", which cannot be executed concurrently with the java program. During the compression process, the memory addresses of some objects will change, and the java program can only continue after the compression is completed.

replication algorithm

    The copy algorithm is simply to divide the memory into two parts, but only use one of them. During garbage collection, copy the surviving objects in the memory that is being used to another empty memory, and finally use The objects in the memory space are cleared and garbage collection is completed.

image

image

 

 

image

image

    The replication algorithm is more concise and efficient than the tag compression algorithm, but its shortcomings are also obvious. It is not suitable for the situation where there are many surviving objects, because there are many objects to be copied, and the replication performance is poor, so the replication algorithm is often used for memory Garbage collection of the young generation in the space, because there are fewer surviving objects in the young generation, and the cost of copying is lower. Another disadvantage is that the cost of memory space is high, because it does object replication based on two memory spaces, only one memory space is used in the non-garbage collection cycle, and the memory utilization rate is low.

summary

    Above we have introduced common garbage collection algorithms. These algorithms have their own advantages and disadvantages, but in the JVM, we do not simply use a specific algorithm, but use something called a garbage collector. The garbage collector can see Doing a series of different combinations of algorithms and using appropriate garbage collectors in different scenarios can achieve twice the result with half the effort. We will introduce the garbage collector in the next article.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325261941&siteId=291194637