This is the 20th day of my participation in the Gwen Challenge.More article challenges

Preface:

Recently, because the project GC is too frequent, I have been optimizing the project, and I have been reviewing the GC garbage collector. With the iterative GC collector in Java, I have also been optimizing the GC garbage collector. Today, I will introduce CMS and G1 collectors and analyze the advantages and disadvantages of both collectors.

CMS collector:

CMS (Concurrent Mark Sweep) collector is a collector aimed at obtaining the shortest collection pause time. It is implemented based on Concurrent “Mark Sweep”, which does not cause user threads to be unable to locate reference objects during the Mark Sweep process. Only works on old age collections. Its steps are as follows:

  1. CMS Initial Mark: exclusive CPU, stop-the-world, marks only objects that GCroots can directly associate with, which is relatively fast;
  2. CMS Concurrent mark: can be executed concurrently with user threads, marking all reachable objects with GCRoots Tracing;
  3. CMS remark: exclusive CPU, stop-the-world, marking and correcting garbage objects generated by user threads running in concurrent marking phase, and updating escaped objects;
  4. CMS Concurrent sweep: Can be performed concurrently with user threads to clean objects marked as recyclable in repeated tags.

Advantages:

  • Supports concurrent collection.
  • Low pauses, because CMS can control the simultaneous execution of two time-consuming stop-the-world operations at the right time with the user thread and ensure that the execution is completed in a short period of time, thus achieving the purpose of approximate concurrency.

Disadvantages:

  • CMS collector is very sensitive to CPU resources. Although it will not lead to user thread pause in the concurrent stage, it will occupy a part of THE CPU resources. If the APPLICATION is insufficient in the case of CPU resources, there will be significant lag.
  • Floating garbage cannot be handled: While performing the concurrent cleanup step, the user thread also generates a portion of the recyclable objects, which will only be collected the next time the cleanup is performed. A ‘Concurrent Mode Failure’ occurs when insufficient memory is reserved for user threads during cleanup, and when this occurs, the SerialOld collection Mode is switched to.
  • After the CMS is cleaned up, there will be a large amount of memory fragmentation, and FullGC will be triggered when there is not enough contiguous space for new objects or promoted to old objects. It was abolished in 1.9.

Usage scenarios

It focuses on the minimum pause times (low pauses) for garbage collection, which is appropriate in older, infrequent GC scenarios.

G1 collector

The G1 collector’s memory structure is completely different from CMS’s, weakening CMS’s original generation model (which can be discontinuous space) and dividing the heap memory into regions (1MB to 32MB, 2048 partitions by default) so that the collection does not have to be carried out in the whole heap. The main feature is controlled pause times and the ability to specify how long a collection should take to complete, which means that G1 provides near-real-time collection features. Its steps are as follows:

  1. Initial Marking: Mark objects that GC Roots can be directly associated with, with a normal Young GC, and modify the value of NTAMS (Next Top at Mark Start) so that the Next stage of user programs running concurrently can create new objects in the correct Region available. This phase is a stop-the-world operation.
  2. The root interval scan, which marks all Survivor interval object references and scans Survivor to old age references, must end before the next Young GC occurs.
  3. Concurrent Marking: An analysis of the reachability of objects in the heap starting with GC Roots to identify viable objects. This phase is time-consuming but can be executed concurrently with user programs and can be interrupted by the Young GC.
  4. Final Marking: The virtual machine will record the changes in the thread Remembered Set Logs during concurrent marking. The virtual machine will record the changes in the thread Remembered Set Logs. The final marking phase requires the consolidation of data from the Remembered Set Logs into the Remembered Set. This phase is a stop-the-world operation using the snapshot-at-the-beginning (SATB) algorithm.
  5. Live Data Counting and Evacuation: First, the collection value and cost of each Region are sorted, a collection plan is developed based on the expected GC downtime of the user, and regions with no living objects are reclaimed and added to the queue of available regions. This phase can also be executed concurrently with the user program, but because only part of the Region is reclaimed, the time is controlled by the user, and halting the user thread greatly improves collection efficiency.

The characteristics of G1

  • Parallelism and concurrency: The G1 takes full advantage of multi-core capabilities, using multiple cpus to shorten stop-the-world times,
  • Generational collection: G1 can manage the collection of created objects and new objects in different generations itself.
  • Spatial consolidation: G1 is based on the ‘mark-tidy’ algorithm as a whole, and on the ‘copy’ algorithm locally (related two regions). Neither algorithm generates memory space fragmentation.
  • Predictable pauses: It allows you to customize the pause time model by specifying that no more time will be spent with the garbage collector over a period of time than expected.

Usage scenarios

The G1 GC shards heap memory into multiple regions to avoid many GC operations across the entire Java heap or the entire young generation. The G1 GC only cares if you have any objects in stock, which are collected and put into an available Region queue. The G1 GC is region-based GC for large memory machines. Even with large memory and Region scanning, performance is high.

Ok! This is the end of today’s article, I hope it can be helpful to you, there are wrong places I hope you can put forward, grow together;

Neat makes for great code, and there’s only so much detail