- Tag-Purge algorithm
- Replication Algorithms
- Tagging-sorting algorithms
- Generational collection Algorithms
1. Tag-Clear algorithm
The most basic collection algorithm, such as its name, is the "mark" and "purge" two phases: first, all objects that need to be reclaimed are marked, and all tagged objects are collected uniformly after the tag is complete.
Two deficiencies:
1) efficiency problem, marking and clearing two process efficiency is not high;
2) space problem, after the mark is cleared, there will be a lot of discontinuous memory fragmentation, too much space fragmentation may cause later when the program is running, you need to allocate a large object, you can not find enough contiguous memory and have to advance to start another garbage collection action.
2. Copying algorithms
This algorithm is designed to solve the problem of efficiency. It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. This makes every time the entire half of the memory collection, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, easy to implement, efficient operation.
But the cost of this algorithm is to reduce the memory for the original half, a little higher.
3. Labeling-Sorting algorithm
The replication collection algorithm will perform more replication operations when the object has a higher survival rate, and the efficiency is reduced. What's more, if you don't want to waste half of your space, you need to have extra space for the allocation guarantee to cope with the extreme situation where all the objects in the memory being used are all alive, so you can't use this algorithm directly in the old age.
According to the characteristics of the old age, someone proposed another "marker-collation" algorithm, the marking process is still the same as the "mark-clear" algorithm, but the next step is not to organize the recyclable objects directly, but to let all the surviving objects move to one end, and then directly clean out the memory outside the end of the boundary.
4. Generation of collection algorithms
Speaking of 3, however, there is no egg use, the current commercial virtual machine garbage collection is using "generational collection algorithm."
However, this algorithm does not have any new ideas, but according to the different life cycle of the memory divided into several pieces, generally the Java heap is divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm.
In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost can be completed collection.
In the old age, because of the high survival rate and the lack of additional space to guarantee it, it was necessary to use the "mark-clean" or "mark-up" algorithm for recycling.
jvm--garbage Collection algorithm