1. Tag-Clear Algorithm
Mark Clear algorithm is the most basic collection algorithm , other collection algorithms are based on this idea. The markup cleanup algorithm is divided into "tags" and " Clear the two stages: first mark the objects that need to be recycled, and then unify the clears the object.
Its main disadvantage:①. The marking and purging process is inefficient.
②. After the mark is cleared, a large amount of discontinuous memory fragmentation occurs.
、
2. Copying Algorithms
It divides the available memory capacity into two blocks of equal size, using only one piece at a time. When this piece is exhausted, copy the surviving object to the other piece, and then remove the memory space that has been used once . In this way, each time it is a piece of memory recovery, no fragmentation and so on, as long as the mobile heap-ordered pointers, in order to allocate memory, simple, efficient operation.
Main drawback: memory is reduced to half the original.
3. Labeling -sorting algorithm
The tag action is consistent with the tag-purge algorithm, and subsequent operations do not just clean the object directly, but instead let all surviving objects move toward one end after cleaning up the useless objects and update the pointers that reference their objects.
The main drawback: on the basis of tag-purge, the object movement is relatively expensive, and the benefit is that it will not produce memory fragmentation.
4, Generational collection algorithm
Divides the memory into several blocks depending on the life cycle of the object. In general, 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 new generation, each 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 copy cost can be completed collection. In the old age, because of the high survival rate of the object and the lack of additional space to guarantee his distribution, it must be recycled using the "mark-and-tidy" algorithm.
JVM garbage collection (tagging-clearing, copying, tagging-sorting, generational) algorithms