First, determine whether the object survival--Accessibility analysis algorithm
Basic idea: Through a series of objects that become "GC Roots", starting from these nodes, the search path is called the reference chain (Reference Chain), and when an object is connected to the GC Roots there is no reference to prove that the object is not available. After judging the chain, there will be two times the marking process, the real recovery, temporary.
Second, garbage collection algorithm 1, mark-Clear algorithm
Mark all objects that need to be recycled and mark the end of the unified collection. Cons: Mark, clear 2 process efficiency is not high, a lot of discontinuous memory fragmentation.
2. Copy algorithm
Divide the memory into 2 blocks of the same size, one of which runs out, copies the surviving copy to the other, and cleans up the used piece once. Pros: No need to consider fragmentation, simple implementation, efficient operation. Cons: Space waste is great.
Current commercial virtual machine companies are using this algorithm to reclaim the new generation.
Improvement: Because object 98% is "to die", then it is not 1:1, but divided into a larger eden,2 block smaller survivor Space, Eden:1 block survivor 8:1, so that the space utilization of 90%, and 10% for each recovery of the remaining mobile copy. And if 10% is not enough to replicate, it needs to be secured with other memory (old age).
Disadvantage: More replication is required for higher object survival, and the efficiency will be reduced. More crucially, if you don't want to waste 50% of your space, you'll need extra space to make the allocation guarantee. therefore, in the old age generally can not directly choose this algorithm.
3, marking--finishing algorithm
Based on the characteristics of the old age, this algorithm is proposed, the tagging process is still the same as "mark-erase", but the next step is not to clean up the recyclable objects directly, but to let all surviving objects move to one end, and then directly clean out the memory outside the end of the boundary.
4, Generational collection algorithm
The current commercial virtual machine garbage collection uses the "generational collection" algorithm, compared with the previous algorithm does not have a new idea, just based on the object survival period of the different memory divided into several pieces.
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 Cenozoic, each garbage collection found that a large number of objects died, only a small number of survival, the choice of replication algorithm, only a small amount of object replication costs can be completed collection. In the old age, because the object has a high survival rate and there is no additional space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm.
Java JVM garbage Collection algorithm