Four Garbage Collection Algorithms

1. Mark removal algorithm :

The algorithm is divided into two stages: marking and clearing. First, all objects that need to be recycled are marked, and all marked objects are collected uniformly after the marking is completed.
insert image description here

It can be seen from the figure that the disadvantage of this algorithm is that a large number of discontinuous memory fragments are caused after the garbage is collected. Too many fragments may lead to the inability to find sufficient contiguous memory when large objects need to be allocated in the future, thus frequently triggering garbage collection and reducing system efficiency.

2. Replication algorithm :

In order to solve the problem of the "mark, clear" algorithm, an algorithm called copying appeared, which divides the memory into two parts evenly, and only uses one of them at a time. When this block is full, it triggers garbage collection and will survive Copy the object of the object to another piece of memory, and then clear this piece of memory, so that there will be no problem of memory fragmentation.

The picture comes from "In-depth understanding of Java virtual machine"

3. Marking Algorithm :

The copy algorithm needs to copy more operations when there are many surviving objects. The most important thing is that only half of the memory can be used. The mark sorting algorithm can solve this problem. The marks in the mark sorting algorithm are the same as the mark clearing algorithm. After the recovered objects are found, move all surviving objects to one end, and then clean up the rest of the memory directly.

The picture comes from "In-depth understanding of Java virtual machine"

4. Generational collection algorithm:

The generational collection algorithm divides the memory into the new generation and the old generation. The new generation is further divided into: a larger Eden area (accounting for 80%) and two Survivor areas (each accounting for 10%). The newly created objects are stored in the new generation Eden District.

Because the vast majority of objects (98%) live and die, when there is not enough space in the Eden area, the virtual machine will initiate a Minor Gc. After the Minor Gc, the surviving objects will enter one of the Survivor areas, and the Eden area will be destroyed. Empty, if there is not enough memory in the Survivor area, it will directly enter the old age. The next Minor Gc will copy the surviving objects in Eden and the Survivor area to another Survivor area, and clear the Eden area and the Survivor area.

Here we can see that compared to the copy algorithm, the wasted space is only 10%. But the premise is that most (90%) objects will be recycled during Mino Gc. If this condition is not met, the size of the Survivor area is not enough, and the surviving objects will directly enter the old age.

Why is the Survivor area needed?

Imagine if there is no Survior area, every time Minor Gc is performed, the surviving objects will directly enter the old generation, and the memory of the old generation will be full. Major Gc will occur, and the execution speed of Major Gc is more than ten times slower than that of Minor Gc. When there is a Survivor area , the surviving objects are tossed in the two Survior areas, and the objects that have survived a certain number of times in the new generation (configurable through parameters) will be put into the old generation. This reduces the frequency of Major Gc occurrences.

Why do we need two Survivor zones?

In order to avoid the problem of memory fragmentation, let’s think about how the copy algorithm solves the problem of memory fragmentation in the mark-and-sweep algorithm. Divide the memory into two pieces of equal size, and copy the surviving objects to another piece of memory. The same is true here. reason.

Old generation:

Because the vitality of the objects in the old generation is relatively tenacious, if the copy algorithm is used, those surviving objects need to be copied many times, so the old generation uses the mark sorting algorithm.

What kind of objects will enter the old age?

1. Large objects will directly enter the old generation:

The so-called large objects refer to objects that require a large amount of continuous memory space. Typical large objects are very long strings and arrays. If large objects are directly allocated memory space in the Eden area, a large amount of memory will be copied between Eden and Survior, which is very difficult. Consumes performance, so it goes directly to the old generation.

2. Long-lived objects will enter the old age

The so-called long-term surviving objects are objects that have survived a certain number of times in the new generation as mentioned above (configurable through parameters).

Guess you like

Origin blog.csdn.net/weixin_44330810/article/details/126147935