There are four common garbage collection algorithms in JVM:
Mark-clearing algorithm (Mark-Sweep);
Copying algorithm (Copying);
Mark-Compact;
Generational collection;
Let's introduce it one by one:
1. Mark-clearing algorithm (Mark-Sweep)
This is the most basic garbage collection algorithm. 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. Its main disadvantages are two: one is the efficiency problem, and the efficiency of marking and cleaning is not high; the other is the space problem, after marking is cleared, a large number of discontinuous memory fragments will be generated. Too many space fragments may cause insufficient large continuous space when allocating large objects, and another garbage collection action has to be triggered in advance.
Mark-clear algorithm diagram
2. Copying algorithm (Copying)
In order to solve the efficiency problem, with the "copy" algorithm, it divides the available memory into two blocks of the same size. Use only one piece at a time. When one piece of space is used up, copy the still-surviving object to another piece, and then clean up the newly used memory space at one time. This makes it possible to recycle one of the pieces every time, so that memory fragmentation and other complex situations are not required to be considered when allocating memory. Simple implementation and efficient operation. But the cost of this algorithm is to reduce the memory to half of the original, which is a bit too expensive. In fact, 98% of the objects in the new generation live and die, so there is no need to divide the memory in a 1:1 ratio, but divide the memory into a larger Eden space and two smaller Survivor spaces, each time using the Eden space and one of the Survivor spaces. When recycled, copy the still-surviving objects in Eden and Survivor to another Suivivor at one time, and finally clean up the space of Eden and the newly used Survivor.
Copy algorithm diagram
3. Mark-Compact
When the object survival rate is high, the replication and collection algorithm must perform more replication operations, and the efficiency will become lower. More importantly, if you don’t want to waste 50% of the space, you need to have additional space to allocate guarantees to deal with the extreme situation where all objects in the half-zone memory are 100% alive, so this algorithm cannot be directly used in the elderly.
Therefore, people proposed another "mark-compact" algorithm. Since objects in the elderly have a long survival cycle, some people proposed the "mark-compact" algorithm. The marking process is the same as "mark-cleaning", but while clearing dead objects, the surviving objects will be sorted, which can reduce fragmented space.
Mark-organization algorithm diagram
4. Generational collection
Currently, garbage collection for commercial virtual machines adopts the "generational collection" algorithm. There is no new idea in this algorithm, but the memory is divided into several pieces according to the different survival cycles of the object. Generally, Java heaps are divided into the new generation and the old generation, so that the most appropriate collection algorithm can be used according to the characteristics of each generation. In the new generation, every time a garbage collection is collected, a large number of objects are found dead and only a small number survive. Then use the copy algorithm, and the collection can be completed with a small amount of copy cost. In the elderly, because the survival rate of objects is high and the cycle is long, the "mark-tidy" or "mark-clear" algorithm is used to recycle it.
The above introduction to common garbage collection algorithms in JVM is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.