JVM study notes 20180206 bis generation recycling algorithm

The garbage collection we generally discuss is mainly aimed at the new generation and the old generation in the Java heap memory. It is precisely because of the difference in the structure of the new generation and the old generation that a generational recycling algorithm is generated, that is, the garbage collection of the new generation and the old generation. Garbage collection uses a different collection algorithm.
For the new generation, the replication algorithm is mainly used.
For the old generation, the mark-sweep algorithm or the mark-sort algorithm is usually used for recycling.

replication algorithm

The idea of ​​the copy algorithm is to divide the memory into two regions of equal size and use one of them at a time. When the memory of this block is used up, the surviving objects are copied to another area, and then the memory of the block is reclaimed. An example graph is shown below:

The colors of the original image were indistinguishable, so a new image was drawn.

This algorithm is simple to implement and relatively efficient, but at the cost of sacrificing half of the memory space for copying. Studies have shown that 98% of objects in the new generation have a very short lifespan, so it is not necessary to divide the entire new generation at a ratio of 1:1. The usual practice is to divide the new generation memory space into a larger Eden area and There are two smaller Survivor areas, and the sizes of the two Survivor areas are the same. Each time you use Eden and one of the Survivor areas, when recycling, copy the surviving objects to another Survivor space, and finally clear the Eden area and the Survivor area used at the beginning. If the Survivor area used for replication cannot hold surviving objects, the objects will be stored in the old age.

The default size ratio of Eden and Survivor in the HotSpot virtual machine is 8:1:1, which means that 10% of the space in the new generation is sacrificed instead of half of the space.

mark-sweep algorithm

The Mark-Sweep algorithm is divided into two stages: mark stage and sweep stage;

In the marking phase , the object space that needs to be reclaimed will be marked, and then the marked object space will be reclaimed in the next clearing phase . There are two main problems with this algorithm: one is the inefficiency of marking and clearing, and the other is that a large number of non-contiguous memory fragments will be generated after clearing, which will lead to the inability to find enough contiguous memory when allocating large objects. Trigger another garbage collection action. An example diagram of the mark-sweep algorithm is shown below:

mark-collate algorithm

The Mark-Compact algorithm effectively prevents the problem of excessive memory fragmentation in the Mark-Sweep algorithm. After marking the objects for reclamation, it moves all the surviving object spaces together and then performs cleanup. An example graph is shown below:

Mark-Organization Algorithm Diagram

Mark-cleaning is usually used as an alternative in the mark-sweep algorithm, in order to prevent a large amount of memory fragmentation after mark-sweep and cannot allocate enough memory for large objects, such as the Serial Old collector mentioned later (based on mark- The tidying algorithm implementation) will be an alternative to the CMS collector (based on the mark-sweep algorithm implementation).

Guess you like

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