When the object is determined to be "dead", virtual will take some means to remove these objects from memory, that is, to recycle garbage, the recycling process has a certain algorithm. Here are some of the main garbage collection algorithms:
1. Tag-Clear algorithm
The algorithm is the most basic algorithm, divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects. All of them say that it is the most basic algorithm because the subsequent collection algorithms are based on this idea and improve their shortcomings. Its shortcomings are mainly two: one is the efficiency problem, the labeling and removal process is not efficient; the other is a space problem, the mark after the removal of a large number of non-wired memory fragmentation, too much memory fragmentation caused when the program ran into the need to allocate a large object can not find enough contiguous memory and have to trigger another garbage collection operation ahead. Tag-Clears the execution process of the algorithm such as:
2. Copying algorithms
To solve the efficiency problem, the "Copy" collection algorithm appears, which divides the available memory into two blocks of equal size, each using one piece. When this piece of memory is used, copy the surviving object to another piece, and then clean up the used memory space once. So that each time is a piece of memory recovery, memory allocation is not considered the problem of memory fragmentation, only need to move the heap top pointer, in order to allocate memory, easy to implement, efficient operation. Just the cost of this algorithm is to reduce the memory to half the original, the price is a little too high. The replication algorithm executes the process as follows:
Now commercial virtual machines are using this algorithm to recover the new generation, IBM's special research shows that the new generation of object 98% is facing the birth of the Dead, the life cycle is very short, so do not need to follow the ratio of 1:1 to divide the memory space, Instead, it divides the memory into a larger Eden space and two smaller survivor spaces, each time using Eden with one of the survivor spaces. When recycled, the objects that are still alive in Eden and survivor are copied to another survivor space at once, finally clearing out Eden and the survivor space just used. The hotspot virtual machine default Eden and survivor space ratio is 8:1, that is, each new generation of available memory for the entire Cenozoic capacity of 90% (80%+10%), only 10% of the new generation of memory is "wasted". Of course, 98% of the objects can be recycled only in the general scenario of the data, but there is no way to ensure that only a few more than 10% objects per collection to survive, when the survivor space is not enough, you need to rely on other memory (old age) to allocate security.
3. Labeling-Sorting algorithm
The replication algorithm will perform a lot of replication when the object has a high survival rate, the efficiency would be lower, if you do not want to waste 50% of space, you need to have additional space to guarantee to deal with the memory of all the objects in use 100% survival extreme situation, so in the old age generally can not directly select this algorithm.
According to the characteristics of the old age, the "tag-collation" algorithm is proposed, the tagging process is still the same as the "mark-purge" algorithm, but the next step is not directly to the recoverable object cleanup, but instead of all the surviving objects to move to one end, and then directly clear the memory outside the end of the boundary. The "Mark-organize" algorithm executes as follows:
4. Generation of collection algorithms
The current garbage collection of commercial virtual machines uses the "generational collection" (generational Collection) algorithm, which divides the memory into several blocks depending on the object's survival cycle. In general, the Java heap is divided into the new generation and the old age, so that the characteristics of each age can be based on the most appropriate collection algorithm. In the Cenozoic, each garbage collection found that a large number of objects died, only a small number of objects survived, the choice of replication collection algorithm, as long as the cost of a small number of surviving objects can be collected. In the old age, because of the high survival rate of the object and the lack of additional space to guarantee its allocation, it must be recycled using the "mark-clear" or "mark-sweep" algorithm.
In-depth understanding of Java Virtual Machine notes---garbage collection algorithm