JVM - Garbage Collector (2) - Garbage Collection Algorithm

content:

1. Mark-Sweep Algorithm

2. Replication algorithm

3. Marking-Organizing Algorithms

4. Generational collection algorithm

 

1. Mark-Sweep Algorithm

The most basic collection algorithm. Just like his name, 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 uniformly recycled after the marking is completed.

Disadvantages: ① Efficiency problem : The efficiency of both marking and clearing processes is not very high. ②Space problem : After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much fragmented space may cause that when a large object needs to be allocated during the running process of the program, it cannot find enough continuous memory and has to trigger the garbage collection action in advance.

 

2. Replication algorithm

To address the efficiency issue, a collection algorithm called "replication" emerged.

It divides the available memory into two equal-sized blocks according to the capacity, and only uses one of them at a time. When this block of memory is used up, the surviving objects are copied to the other block, and then the used objects are copied. The memory space is cleared at one time.

Advantages: ① No need to consider complex situations such as memory fragmentation. ②Simple implementation and efficient operation.

Disadvantages: Only general memory can be used, and the memory sacrifice is too large.

Improved optimization : Today's commercial virtual machines use this collection algorithm to recycle the young generation. Special research by IBM shows that 98% of the objects in the new generation are "dead", so there is no need to divide the memory space according to the ratio of 1:1. Instead, the memory is divided into a larger Eden space and two smaller Survivor spaces . Use Eden and one of the Survivor pieces each time. When recycling, copy the surviving objects on Eden and Survivor to another Survivor at one time, and finally clear the space of Eden and Survivor. (The ratio is 8:1:1 in HotSpot)

Of course, 98% of the objects that can be recycled are only data in general scenarios. We have no way to ensure that no more than 10% of the objects survive each recycling. When the Survivor space is not enough, we need to rely on other memory (here refers to the old age). ) for distribution guarantees .

If another piece of Survivor space does not have enough space to store the surviving objects collected by the last young generation, these objects will enter the old generation directly through the allocation guarantee mechanism.

3. Marking-Organizing Algorithms

The copy collection algorithm needs to perform more copy operations when the object survival rate is high, and the efficiency will be lower. More importantly, if you don't want to waste 50% of the space, you need additional space for allocation guarantees to deal with the extreme case where all objects in the used memory are 100% alive. Therefore, in the old age, this algorithm cannot be used directly.

According to the characteristics of the old age, another "mark-sort" algorithm has been proposed. The marking process is the same as before, but the follow-up is to move all live objects to one end, and then directly clean up the memory outside the end boundary.

 

4. Generational collection algorithm

Currently, the garbage collection of commercial virtual machines adopts the "generational collection" algorithm. This algorithm does not have any new ideas, but divides the memory into several blocks according to the different life cycles of objects. Generally, the Java heap is divided into the new generation and the old generation, so that the most appropriate collection algorithm can be adopted according to the characteristics of each generation.

In the new generation, a large number of objects will be found to die during each garbage collection, and only a few survive, so the replication algorithm is used;

In the old age, because the object has a high survival rate and there is no extra space to allocate it, it must use the "mark-clean" or "mark-clean" algorithm for recycling.

Guess you like

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