Garbage collection algorithm
- Tag-Purge algorithm
- The most basic algorithm is the tag-purge (mark-sweep) algorithm. There are two stages of marking and purging: first marking out all objects that need to be recycled, and then uniformly reclaiming all tagged objects when the tag is complete. It is the most basic collection algorithm, because the subsequent collection algorithms are based on this idea and the shortcomings of the improvement is obtained. It has two main shortcomings: one is efficiency labeling and removal efficiency is not high, and the other is the space problem marked by the removal of a large number of discontinuous memory fragmentation, Too much space fragmentation can cause a large number of objects to be allocated later in the program to fail to find enough contiguous memory and to trigger another garbage collection action in advance. The execution process of the markup cleanup algorithm:
- Replication Algorithms
- To solve the efficiency problem, a collection algorithm called Replication (Copying) appears, dividing memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, the surviving object is copied to the other piece, and then the used memory space is cleaned out once. This allows the entire half of the memory to be reclaimed each time, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer in order to allocate memory to achieve a simple run efficient this algorithm is the cost of reducing memory to half the original. The execution process is as follows:
-
- Today's commercial virtual machines use this collection algorithm to reclaim the new generation. IBM research shows that the new generation of object 98% is "dying in the face", this memory allocation does not require 1:1, but instead divides the memory into a larger Eden space and two smaller survivor space, each using Eden and one piece of survivor. When reclaimed, the objects that are still alive in Eden and survivor are copied one at a time to another survivor space, and finally the Eden and the survivor space you have just used are cleaned up. The hotspot default Eden and Survior ratio is 8:1, so that each new generation of usable memory capacity is 90%, only 10% of the memory will be wasted. When more than 10% of the objects are alive, you need to rely on other memory for allocation guarantees.
- Tagging-sorting algorithms
- Replication collection algorithms require more replication operations when the object has a higher survival rate, and the efficiency will be reduced. There will be all objects in memory 100% the extreme situation of survival the old age is generally not able to use this algorithm directly. According to the characteristics of the old age, it was suggested that the "mark-and-mark-compact" algorithm, marking the process and "mark-clear", but the next step is not to clean the recyclable objects directly, but to let all the surviving objects move to one end, and then clean out the memory algorithm beyond the end of the boundary is as follows
- Generational collection Algorithms
- All modern commercial virtual machines use the "generational collection" (generational Collection) algorithm, which is divided into several blocks based on the lifetime of the object, generally dividing the Java heap into the new generation and the old age, so that the appropriate collection algorithm can be used according to the characteristics of each age. In the Cenozoic, every garbage collection found that a large number of objects died only a small amount of survival, only a small number of surviving objects can be copied cost to complete the collection. In the old age, where there is no additional space for allocation guarantees due to high object survival, the tag-purge, Mark-and-tidy algorithm must be used for recycling.
Java garbage Collection Garbage Collection (one, garbage collector algorithm)