Introduction to Garbage Collection Algorithm

1. Mark-and-sweep algorithm

Idea: The algorithm is divided into two parts : marking and clearing . First, all objects that need to be recycled are marked. After marking is completed, all marked objects are uniformly recycled. This algorithm is the most basic garbage collection algorithm, and subsequent algorithms are based on this. Improve it on the above, the specific process is as follows:
Insert picture description here
Disadvantages : (1) Low efficiency, marking and clearing are not efficient; resulting in the need for large memory later, it is impossible to find enough continuous memory space and have to trigger a recycling in advance Operation.
(2) Mark removal regrets that a large number of discontinuous memory fragments are generated, and the
advantages are simple to operate!

2. Copy algorithm (in order to solve the efficiency problem of the previous algorithm)

The idea is to divide the memory into two equal parts according to the capacity, and only use one of them at a time. When this block is used up, the living object is copied to another block, and then the used memory space is released. In this way, only half of the interval is reclaimed each time, and the memory allocation does not need to consider the problem of memory fragmentation. It only needs to move the pointer on the top of the heap and allocate it in order. The implementation is simple and efficient. But the cost of this algorithm is to reduce the memory by half, which is too costly. As shown in the figure below:
Insert picture description hereHowever, IBM's special research shows that 98% of the objects in the new generation are "dying", so there is no need to install a 1:1 ratio to divide the memory space, but divide the memory into a large block. Eden space and two smaller Survivor spaces. Each time you use Eden and one of the Survivor spaces, when reclaiming, copy the surviving objects in Eden and Survivor to another Survivor space at one time, and finally clean up Eden and one of the Survivor spaces. For the used Survivor space, the default ratio of Eden to Survivor is 8:1, so only 10% of the space is wasted. However, there will still be some problems (for example, we cannot guarantee that the objects collected each time are less than 10%) are not explained here.
Disadvantages : When the survival rate of the object is high, more copy operations are required, and the efficiency will be reduced. It becomes lower, so this algorithm is generally not directly used in the old generations. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

3. Marking-sorting algorithm

Mark-Compact algorithm, the marking process is the same as the "mark-clear" algorithm, but the subsequent steps are not to directly clean up recyclable objects, but to move all surviving objects to one end, and then clean up the end. Memory outside the boundary. The process is as follows:
Insert picture description here

4. Generational collection algorithm

This algorithm does not have any new ideas. It just divides the memory into several blocks according to the life cycle of the object. Generally, the Java heap is divided into the new generation and the old generation, so that different algorithms can be selected according to the characteristics of each era.
Cenozoic: Every time a collection, a large number of objects die, and only a few survive, using a replication algorithm.
Old age: Object survival rate is high, there is no extra space, using "mark-clean" or "mark-organize" algorithm

Guess you like

Origin blog.csdn.net/lq1759336950/article/details/99615603