Home >Java >javaTutorial >JVM Advanced Features-Garbage Collection Algorithm Tutorial
1. Mark-clear algorithm
Mark-clear algorithm is the most basic collection algorithm. The execution process is the same as the name. It is divided into two stages. Mark And clearing
First, mark the objects that need to be recycled. After the marking is completed, the marked objects will be recycled uniformly, as shown in The following figure.
For an introduction to the specific marking process, please refer to the "reachability analysis" introduced in the previous article "Determining Object Survival Algorithm for Garbage Collection"
Recycling Previous state
Disadvantages:
1. The efficiency is not high. The efficiency of the two actions of marking and clearing is not too high 2. As can be seen from the picture, the memory after clearing There will be a large number of fragments in the object. The disadvantage of this is that when allocating memory space to a large object, it may not be able to find a large enough continuous space, causing a GC to occur in advance.
2. Copy Algorithm
The principle of the copy algorithm is to divide the memory space into two parts of the same size. When garbage collection is required, copy the surviving objects
in the exhausted memory to another piece of memory, and then clear the previous piece of memory. As shown in the picture below
## Recycling Post-state
Advantages: The efficiency of the replication algorithm is higher
Disadvantages: This The space sacrificed by the algorithm is larger. After all, the usable memory space has become half of the original number
3. Marking sorting algorithmThis is an algorithm commonly used in the old generation, because the objects in the old generation are stored for a long time. Marking and sorting algorithm The marking process is the same as the mark-cleaning algorithm, but the subsequent steps are different. The marking and sorting algorithm moves the surviving objects to one end, Then clean up all the memory at the other end of the surviving end border, as shown in the figure below
## Previous state
4. Generational collection algorithm
Current virtual machines all use the generational collection algorithm. This algorithm divides the memory according to the different survival cycles of the objects. For different areasGenerally, the Java heap is divided into the new generation and the old generation
5. The algorithm used in the new generation
In commercial virtual machines, the replication algorithm is used to garbage collect the new generation. The reason is that 98% of the objects in the new generation have a very short survival time.
Therefore, it is not necessary Divide the memory into two blocks of the same size, leaving only a smaller space for surviving objects to be copied. Therefore,memory is usually divided into a larger Eden area and two smaller survivor areas. When recycling, the surviving objects in the eden area and survivor area are copied to another area
In the survivor area, just clean up eden and the used servivor.The above is the detailed content of JVM Advanced Features-Garbage Collection Algorithm Tutorial. For more information, please follow other related articles on the PHP Chinese website!