Java Virtual Machine Garbage Collector
Garbage collection algorithms used by the Garbage CollectorMark-Sweep Algorithm:
First, all objects to be recycled are marked, and all marked objects are recycled after marking. It has two main disadvantages: efficiency, but the efficiency of marking and clearing processes is not high. Another is space. After clearly marking, a large number of discontinuous memory fragments will be generated, too many space fragments may cause the program to fail to find enough continuous memory space when a large object needs to be allocated during future operation, so it has to start another garbage collection action in advance.
The execution process is shown in:
Copy algorithm:
It divides the available memory into two equal-size blocks by capacity, and uses only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space. In this way, memory is recycled for each part of the block. When there is no memory allocation, you do not need to consider the memory fragmentation and other complex situations. As long as you move the heap top pointer and allocate the memory in sequence, the implementation is simple, efficient operation.
Currently, commercial virtual machines use this collection algorithm to recycle the new generation. IBM's special research shows that 98% of the new generation's objects are born and died overnight, therefore, you do not need to divide the memory space according to the ratio of. Instead, you can divide the memory into a large Eden space and two smaller memory vor spaces. each time you use Eden and a memory vor. When recycling, copy the Eden and zoovor to another zoovor space at one time, and finally clear the space of the Eden and the used zoovor.
The execution process is shown in:
Tag-sorting algorithm:
The marking process is still the same as the marking-clear algorithm, but the subsequent steps do not directly clean the recyclable objects, but move all the surviving objects to one end, then, the unexpected memory at the end boundary is cleared directly.
The execution process is shown in:
Generation collection algorithm:
Currently, commercial Virtual Machine garbage Collection uses the Generational Collection algorithm. This algorithm has no new idea, but divides the memory into several blocks based on the different inventory cycles of objects. Generally, Java heap is divided into the new generation and the old age, so that the most appropriate mobile phone algorithm can be used according to the characteristics of each age. In the new generation, a large number of objects are found to die each time during garbage collection, and only a small number of objects survive. Therefore, the replication algorithm is used, and the collection can be completed only at the replication cost of a small number of surviving objects. In the old age, because the object has a high survival rate and no extra space to allocate a guarantee for it, it must be recycled using the tag-clean or tag-sort algorithm.
Garbage Collector
I drew a mind map to describe the Garbage Collector:
Common junk collector Parameters
| Parameters |
Description |
| UseSerialGC |
Use the collector combination of Serial + Serial Old for memory recovery |
| UseParNewGC |
Use the collector combination of ParNew + Serial Old for memory recovery |
| UseConcMarkSweepGC |
Use the collector combination of ParNew + CMS + Serial Old to recycle memory. The Serial Old collector will be used as the backup collector after a Concurrent Mode Failure occurs in the CMS collector. |
| UseParallelGC |
Use the collector combination of Parallel Scavenge + Serial Old for memory recovery |
| UseParallelOldGC |
Use the collector combination of Parallel Scavenge + ParallelOld for memory recovery |
| -XX: Invalid vorratio |
Capacity Ratio of the Eden region to the VOR region in the new generation. The default value is 8, which indicates Eden: Hangzhou vor = 8. |
| PretenureSizeThreshold |
The size of objects directly promoted to the old age. objects larger than this parameter will be directly allocated in the old age. |
| MaxTenuringThreshold |
The age of the person who was promoted to the old age. After a Minor GC operation is performed on each object, the age increases by 1. When this parameter is exceeded, it enters the old age. |
| UseAdaptiveSizePolicy |
Dynamically adjust the size of each region in the Java heap and the age of the old age |
| HandlePromotionFailure |
Whether the guarantee is allowed to fail, that is, the remaining space in the old age is insufficient to cope with the extreme situation that all objects in the entire Eden and region of the New Generation survive. |
| ParallelGCThreads |
Set the number of threads for memory collection during parallel GC. |
| GCTimeRatio |
The percentage of GC time to total time. Takes effect only when the Parallel Scavenge collector is used |
| MaxGCPauseMillis |
Set the maximum pause time for GC. Takes effect only when the Parallel Scavenge collector is used |
| CMSInitiatingOccupancyFraction |
Set the usage of the CMS collector in the old age to trigger garbage collection. Effective only when the CMS collector is used |
| UseCMSCompactAtFullCollection |
Sets whether the CMS collector performs a memory fragment after completing the garbage collection. Effective only when the CMS collector is used |
| CMSFullGCsBeforeCompaction |
Set the CMS collector to enable memory fragmentation after several garbage collection times. |