Deep understanding of Java Virtual Machine (Second Edition)-Garbage Collection Algorithm

table of Contents

 

3.3.1 Mark-Clear Algorithm

3.3.2 Copy Algorithm

3.3.3 Marking-sorting algorithm

3.3.4 Generational collection algorithm


3.3.1 Mark-Clear Algorithm

The most basic collection algorithm is the "Mark-Sweep" algorithm. Like its name, the algorithm is divided into two stages: "Marking" and "Sweeping": first, all objects that need to be recycled are marked. After the completion, all marked objects are recovered uniformly. The marking process has actually been introduced in the previous section about object marking determination. The reason why it is said to be the most basic collection algorithm is that subsequent collection algorithms are based on this idea and improved on its deficiencies.
It has two main shortcomings : one is the efficiency problem , the efficiency of the marking and clearing processes is not high; the other is the space problem , after the marking is cleared, a large number of discontinuous memory fragments will be generated, and too much space fragmentation may cause Later, when a larger object needs to be allocated during the running of the program, it cannot find enough contiguous memory and has to trigger another garbage collection in advance. The execution process of the mark-clear algorithm is shown in Figure 3-2.

 

3.3.2 Copy Algorithm

In order to solve the problem of efficiency, a collection algorithm called "Copying" appeared, which divides the available memory into two pieces of equal size according to the capacity, and only uses one piece at a time. When this block of memory is used up, the surviving objects are copied to another block, and then the used memory space is cleaned up at once. In this way, the entire half area is reclaimed every time, and there is no need to consider memory fragmentation and other complex situations when memory is allocated. As long as the pointer on the top of the heap is moved and the memory is allocated in order, the implementation is simple and the operation is efficient. It's just that the cost of this algorithm is to reduce the memory to
half of the original, which is too high. The execution process of the replication algorithm is shown in Figure 3-3.

  • Current commercial virtual machines all use this collection algorithm to recycle the new generation. IBM's special research shows that 98% of the objects in the new generation are "live and die", so there is no need to divide them in a 1:1 ratio. The memory space, but the memory is divided into a larger Eden space and two smaller Survivor spaces, each time you use Eden and one of the Survivor. When recycling, copy the surviving objects in Eden and Survivor to another Survivor space at one time, and finally clean up Eden and the Survivor space just used. The default ratio of Eden to Survivor for HotSpot virtual machine is 8:1, which means that the available memory space in each new generation is 90% (80%+10%) of the entire new generation capacity, and only 10% of the memory will be "wasted" ". Of course, 98% of objects that can be reclaimed are only data in general scenarios. We have no way to ensure that no more than 10% of the objects survive each time. When the Survivor space is not enough, you need to rely on other memory (here refers to the old generation). Distribution guarantee (Handle Promotion).
  • The memory allocation guarantee is like going to the bank to borrow money. If we have a good reputation, we can repay on time in 98% of the cases, so the bank may assume that we can repay the loan on time and in amount next time, only one guarantee is needed. People can guarantee that if I can deduct money from his account when I cannot repay, then the bank thinks there is no risk. The memory allocation guarantee is also the same. If another piece of Survivor space does not have enough space to put the surviving objects collected in the new generation, these objects will directly enter the old generation through the allocation guarantee mechanism. The content of the allocation guarantee for the new generation will be explained in detail later in this chapter when the garbage collector execution rules are explained.

3.3.3 Marking-sorting algorithm

The copy collection algorithm will 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 to have extra space for allocation guarantees to deal with the extreme situation where all objects in the used memory are 100% alive, so this is generally not directly used in the old age. algorithm.
According to the characteristics of the old age, someone proposed another "Mark-Compact" algorithm. The marking process is still the same as the "Mark-Clear" algorithm, but the subsequent steps are not directly cleaning up recyclable objects. Instead, all the surviving objects are moved to one end, and then the memory outside the end boundary is directly cleaned up. The schematic diagram of the "marking-sorting" algorithm is shown in Figure 3-4.

3.3.4 Generational collection algorithm

The current garbage collection of commercial virtual machines uses the "Generational Collection" algorithm. This algorithm does not have any new ideas, but divides the memory into several blocks according to the life cycle of the object. Generally, the Java heap is divided into new generation and 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 are found to die every time a garbage collection, and only a few survive, then the replication algorithm is selected, and the collection can be completed only by paying the replication cost of a small amount of living objects. In the old age, because the object has a high survival rate and no extra space for its allocation guarantee, it is necessary to use the "mark-clean up" or "mark-clean up" algorithm for recycling.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/someby/article/details/103880289