Summary, advantages and disadvantages of CMS in four stages of garbage recycling G1 Collector characteristics G1 Four stages of garbage recycling When should you consider using G1G1 to set parameters
Four stages of CMS garbage recycling
Initial mark concurrent mark re mark concurrent cleanup
Initial marking stage: it will stop all threads, that is, Stop the World status
Concurrency marking stage: track all objects. This stage is the most time-consuming. However, this phase runs concurrently with the system, so it will not affect the system operation
Re marking stage: since the second stage is executed concurrently, the old object will become a garbage object when marking garbage objects and creating new objects. Therefore, the third phase will also enter the Stop the World state and be marked again. It marks a few objects that have changed in the second phase, so the running speed is very fast
Concurrent cleaning phase: this phase will also take a lot of time, but because it runs concurrently, it will not have a great impact on the system
Summary, advantages and disadvantages of CMS
CMS uses the mark clean algorithm to mark garbage objects and clear garbage objects. The algorithm is implemented based on the older generation, because the new generation cannot accept the fragment garbage generated by the algorithm.
Advantages: concurrent collection, low pause
Insufficient:
Floating garbage cannot be processed. Concurrent collection will cause too many memory fragments. Because the concurrent marking and concurrent cleaning phases are executed concurrently, additional CPU resources will be consumed
Features of G1 reclaimer
G1 is used to replace 1.5 CMS types of jdk9, which has been implemented in jdk9. By default, jdk9 uses G1 collector and removes all CMS related content. G1 has several characteristics compared with CMS:
Control the time of garbage collection: This is the advantage of G1. It can control the time of garbage collection. It can also establish a pause time model, select a group of appropriate regions as the collection goal, and achieve the goal of real-time collection. Space sorting: Like CMS, it uses the mark cleaning algorithm, but G1 does not produce space debris, so it effectively uses continuous space, It will not cause insufficient continuous space to trigger GC in advance
G1 divides Java memory into multiple equal parts and regions. Logically, there is the concept of the new generation and the old generation, but there is no strict distinction between them
Feel the texture:
The concept of the old age of the Cenozoic still exists, but there is no strict distinction. There are 2048 regions at most
Processing of large objects
In addition to the above advantages, there is another advantage, that is, the processing of large objects. In the CMS memory, if an object is too large, and it is larger than the allocated area when entering S1 and S2, the object will enter the old generation directly. G1 will judge whether an object is larger than 50% of the size of a region when processing large objects. If it is larger than 50%, it will be stored across multiple regions
G1 Four stages of garbage recycling
Initial mark concurrent mark final mark filter recycle
Initial mark: mark the objects that can be directly associated with GC Roots. This phase requires thread pause but takes a short time
Concurrency flag: search for surviving objects, which can be executed concurrently with other programs, taking a long time
Final marking: During concurrent marking, the user program will cause changes in the marking records (like an aunt cleaning up garbage while another person throws garbage). The virtual opportunity records the changes during this period in the Remembered Set Logs. The final marking stage will merge the changes in the concurrent marking stage into the Remembered Set. This phase requires thread pause, and can also be executed concurrently
Filter recovery: sort the recovery costs of each region, and formulate recovery plans according to user-defined recovery time
When should G1 be considered
Refer to official documents:
Real time data occupies more than half of the heap space. The speed of object allocation or promotion changes. It is highly desirable to eliminate long GC pauses (more than 0.5-1 seconds)
G1 setting parameters
Control the time of G1 recycling garbage
-20: MaxGCPauseMillis=200 (default 200ms)