Because love so insist, because love so wait. Through the long days without drama to play, finally for the spring of life, mutual encouragement!!

1. The description of the CMS

①. In the JDK1.5 era, HotSpot introduced a garbage collector that could almost be considered revolutionary in strong interactive applications: CMS (Concurrent one Mark one Sweep) collector, the first truly Concurrent collector in the HotSpot VIRTUAL machine, for the first time allowing garbage collection threads to work simultaneously with user threads

②. The CMS collector is concerned with minimizing the pause time of user threads during garbage collection. The shorter the pause times (low latency), the better the application that interacts with the user, and the better the response speed improves the user experience.

③ CMS garbage collection algorithm uses the tag – clean algorithm, and also **” stop – the – world”**

Unfortunately,CMS, as an older collector, does not work with the Parallel Insane, a new collector that already exists in JDK 1.4.0. When CMS was used to collect older generations, the new generation had to choose between ParNew or Serial

⑤. CMS was widely used before G1 appeared. Today, there are still many systems using CMS GC

CMS collector deprecated in JDK9 and removed in JDK14

2.CMS Process (Principle)

①. Initial Mark only marks the objects that can be directly associated with GCRoots, with the phenomenon of STW and a very short temporary time

Concurrent Mark stage: the process of traversing the entire object graph from directly associated objects in GC Roots. This process takes a long time but does not require the user thread to pause and can be run concurrently with the garbage collection thread (the Concurrent Mark stage is marked with tricolour, as documented below).

③ Re-marking phase: In the concurrent marking phase, the worker thread of the program and the garbage collection thread will run at the same time or cross each other. Therefore, in order to correct the mark record of the part of the object whose mark changes due to the continuous operation of the user program during the concurrent marking phase, The pause time in this phase is usually slightly longer than in the initial tagging phase, but also much shorter than in the concurrent tagging phase.

(4) concurrent clearance: this stage clears and deletes the dead objects judged by the marking stage to release memory space. Since there is no need to move live objects, this phase can also be concurrent with the user thread

⑤. Supplementary remarks:

  • Because the most time-consuming concurrent marking and concurrent cleanup phases do not require pauses, the overall collection is low.
  • You should also ensure that the application user threads have enough memory available during CMS reclamation. Therefore, the CMS collector does not wait until the old age is almost completely filled, as other collectors do. Instead, the CMS collector starts collecting when the heap memory usage reaches a certain threshold to ensure that the application still has enough space to run while the CMS is working. If the CMS is running without enough memory to meet the program’s requirements (garbage generation faster than cleanup), a “Concurrent Mode Failure” occurs, at which point the VIRTUAL machine starts a backup: temporarily enabling Serial 0ld** collector to redo the old garbage collection so that the pause time is very long.
  • The GARBAGE collection algorithm of the CMS collector adopts the mark-clean algorithm, which means that after each collection, some memory fragments will inevitably be generated because the memory space occupied by the useless objects that perform the collection is most likely to be discrete chunks. The CMS will not be able to use the Bump the Pointer technique to allocate memory for new objects, and will only be able to select the Free List to allocate memory.

⑥ Since Mark Sweep causes memory fragmentation, why not change the algorithm to Mark Compact?

If the user thread can continue to execute, the resource it is running on will not be affected. If the user thread can continue to execute, the resource it is running on will not be affected.

3. Advantages and disadvantages of CMS

Advantages: concurrent collection, low latency

②. Disadvantages of CMS:

  • Memory fragmentation can result in the Full GC having to be triggered early if large objects cannot be allocated after cleaning up

  • **CMS collectors are very sensitive to CPU resources. ** In the concurrent phase, it does not cause user pauses, but it does slow down the application and reduce overall throughput by taking up a portion of the threads

  • The CMS collector cannot handle floating garbage. A “Concurrent Mode Failure” may occur, resulting in another Full GC. In concurrent mark phase due to program of worker threads and garbage collection threads are running at the same time or cross running, so in concurrent mark phase if waste generated new objects, CMS will not be able to mark the garbage objects, eventually lead to the new waste generated in the object is not timely recovery, and only when the next GC is not before the release of the recovery Memory space

4. Set CMS parameters

-xx :+UseConcMarkSweepGC:** Manually specifies that the CMS collector should be used to perform memory reclamation tasks (enabling this parameter automatically turns on a XX:+ UseParNewGC. ParNew (Young) +CMS (0LD) +Serial 0LD

* * (2). – XX: CMSlnitiatingOccupanyFraction: * * set the heap memory usage threshold, once reached the threshold, began to recycle

  • The default value for JDK5 and previous versions is 68, which means that a CMS collection is performed when the space usage of the older generation reaches 68%. The default value for JDK6 or later is 92%
  • If the memory growth is slow, you can set a larger threshold. A larger threshold can effectively reduce the triggering frequency of the CMS, and reduce the number of old reclaim times, which can significantly improve application performance. Conversely, if your application’s memory usage is growing rapidly, you should lower this threshold to avoid triggering the old serial collector too often. Therefore, this option can effectively reduce the number of Full GC executions

* * (3). – XX: + UseCMSCompactAtFullCollection: * * is used to specify the execution of the Full after GC to compress the memory space, so as to avoid the generation of memory fragments. The problem, however, is that the pause times become longer because the memory compacting process cannot be executed concurrently

* * (4). – XX: CMSFullGCsBeforeCompaction: * * set after how many times perform Full GC to compress the memory space

-xx :ParallelCMSThreads:** Sets the number of CMS threads

  • The default number of threads started by CMS is (Parallel****GCThreads+3)/4
  • ParallelGCThreads is the number of threads for the young generation of parallel collectors. When CPU resources are tight, application performance can be very poor during the garbage collection phase due to CMS collector threads.)

5. CMS changes in later JDK versions

  • New JDK9 feature: CMS has been marked Deprecate

Preparation: If the CMS collector is enabled on a HotSpot VIRTUAL machine with JDK 9 or above using the -xx :+UseConcMarkSweepGC parameter, the user will receive a warning that the CMS will be deprecated in the future.

  • New in JDK14: Remove the CMS garbage collector

Trained: CMS garbage collector removed. If -xx: +UseConcMarkSweepGC is used in JDK14, the JVM will not give an error, only a warning message, but no exit. The JVM automatically falls back and starts the JVM in the default GC mode

Reference video: Silicon Valley JVM complete tutorial, millions of playback, the peak of the network (Song Hongkang details Java VIRTUAL machine) reference books: in-depth understanding of the Java virtual machine