The full name of the CMS CMS garbage collector is Concurrent Mark Sweep. From the name, we can also see that the garbage collector is implemented based on the mark clearing algorithm. First, "Concurrency" means that the GC thread can execute concurrently with the user thread. At the same time, since it is a mark and clear algorithm, it means that this garbage collector will generate many fragments, which is the disadvantage of the mark and clear algorithm. At the same time, CMS works in the old age, and the garbage collection frequency of the old age is lower than that of the young generation. CMS garbage collection has four process initialization marks: concurrency mark: re mark: concurrent clear: when the initial mark is made, it is a STW (stop the world) process, and all user threads will stop. At this time, it just marks the objects that GC Roots can directly reach. Because it is only marking one layer, the overall speed will be relatively fast. The concurrent mark is a GC Roots scanning process, which scans the objects that can be recycled for the entire link mark; Because the entire link will be long, it will take a little longer. However, because this process is concurrent, it has no impact on the operation of user threads. Just as the name implies, remarking is a process of remarking and also a process of STW. The reason for this process is that the user thread is still running in the process of concurrent marking in the previous step, so the reference relationship of objects will change and new garbage will be generated when running. Only objects that have changed in the previous step will be marked here. Although STW is possible, it is also fast. Concurrent cleanup is the last stage. This stage is relatively time-consuming because it needs to clear all garbage objects scanned before. However, this stage can be performed concurrently, so it will not affect the operation of user threads. After the above four processes, a complete GC is completed. As we mentioned earlier, the entire CMS garbage collector is based on the mark and clear algorithm. First, mark the objects to be cleaned through three processes, and then clean them. The initial marking and re marking will trigger STW in the whole process, and the other two stages are carried out concurrently. The mark and clear algorithm will generate memory fragments, so it is not suitable for the young generation that needs frequent recycling, so it is only suitable for the old age. Fragmentation is the disadvantage of CMS, and concurrency is the advantage of CMS. After all, any collector has advantages and disadvantages. Before G1, we finished talking about CMS. Next, we will talk about G1. The full name of G1 is Garbage First. Before we talk about the details of G1 garbage collector, the first thing we need to know is that G1 redefines the entire heap space. The old and young generations in G1 are no longer physically isolated, but logically isolated. In G1, the entire heap space is divided into region blocks of the same size, and multiple region blocks logically form the young generation and the old generation. This is because it is not necessary to scan the entire heap space during garbage collection, and garbage collection can be performed according to the specified pause time. G1 will quantify the recovery cost of each region to achieve a cost control. Recovery can be completed within a limited pause time, which is the biggest feature of G1. G1 recovery is also divided into four processes: initial marking: initial marking and CMS are also only scanning the objects directly reached by GC Roots. This stage also requires STW, but the time is also very short; Concurrency flag: start from GC Roots to analyze the accessibility of objects in the heap and find the surviving objects. This phase takes a long time, but it can be executed concurrently with the user program; Final marking: The idea of final marking and CMS re marking has always been to correct the part of objects whose marking changes due to the concurrent running of user programs during the concurrent marking period, but the difference is that G1 will record the object changes during this period in the thread Remembered Set Logs, In the final marking stage, the data of the Remembered Set Logs needs to be merged into the Remembered Set. In this stage, the thread needs to be paused, but it can be executed in parallel; Filtering and recycling: the last step is the biggest difference between G1 and CMS. G1 first quantifies and sorts the costs of each region to be recycled, and then formulates a recycling plan based on the expected GC pause time. The expected recycling time is specified by the - XX: MaxGCPauseMillis parameter. This phase can also be executed concurrently with the user program, but because only a part of the region is recycled, the time is controllable by the user, and pausing the user thread will greatly improve the collection efficiency. A Remembered Set memory set mentioned above is used to record object references. It will be recorded here when there are changes in object references during concurrent marking, and will be corrected when the final marking is made. On the whole, G1 uses the tag collation algorithm for garbage collection, and does not generate content fragments like CMS does. Therefore, G1 can perform garbage collection for young and old generations at the same time. Compared with CMS, G1 is more flexible, and because G1 divides memory into regions, it will not cause the problem of space waste caused by the replication algorithm. First of all, CMS and G1 are both concurrent and generational garbage collectors with low latency; CMS is based on the mark and clear algorithm. It is only suitable for the young generation, with unpredictable pause time. At the same time, the young generation and the old generation are physically isolated. G1 is a mark and sort based garbage collector with high throughput and predictable pause time. It can be used in the young generation and the old generation at the same time. At the same time, the young generation and the old generation are logically isolated.
Transferred from the official account: Java geek technology