Daily sentence

However, it is impossible to ignore the fact that many successful people, with more ambitious goals in life, in the face of a more dangerous environment, dealing with more complex personnel relations, they still lead unswervingly, pass through, to the success of the brilliant.

The 7 steps of a CMS

Key steps interpretation

Initial Mark

  • Target: Perform reachability analysis and mark objects to which GC ROOT can be directly associated.
  • Mark range: Young Gen + Old Gen.
  • Threads: JDK1.7 is single-threaded, JDK1.8 is multi-threaded
XX: + CMSParallelInitialMarkEnabled adjustment)Copy the code
  • STW: Triggers stop-the-world
  • Features: Extremely fast speed

Concurrent Mark

  • Target: Iterate over the live objects initially marked in phase 1, and then continue to recursively mark objects reachable by these objects. (Black, white and gray marking method).

  • Mark range: Young Gen + Old Gen.

  • STW: No trigger

  • Features: Slow, time-consuming.

  • Special operation: the Old chronology is logically divided into equal Card pages in advance through Card Marking in the Old area.

If the reference relationship changes, the JVM marks the changed area as a bit “Dirty Card.”

The JVM keeps a record of objects created by the application thread during the “concurrent tag” phase and references modified to the object (mod-Union Table).

Preclean

  • Target: The dirty areas recorded in the (mod-union Table) during the concurrent marking phase will be found, the reference relationship will be refreshed, and the “dirty areas” flag will be cleared.

  • Mark range: Old Gen.

  • Threads: The GC thread and the application thread also execute concurrently

  • STW: No trigger

  • Features: Average speed

Concurrent Abortable Preclean

  • Goal: To reduce the pause time for applications during garbage collection. The most time-consuming step in the whole process is step 5(final/relabelling), so step 4 is done earlier.

  • Trigger condition: This stage is started when the Eden usage exceeds 2M. Of course, 2M is the default threshold and can be modified through parameters.

  • Mark range: Young Gen + Old Gen.

    • Handles objects in the From and To sections and marks objects that can reach the old age. As in phase 3, objects in the Dirty Card are scanned.
  • Termination of logic: the CMS in order to avoid this stage didn’t wait for Minor GC and an endless waiting, CMSMaxAbortablePrecleanTime provides parameters, defaults to 5 s; If the interruptible pre-clean execution lasts for more than 5s, the phase will be aborted regardless of whether Minor GC occurs, and Remark will be entered, as well as the space threshold of Eden’s memory usage.

  • Threads: The GC thread and the application thread also execute concurrently.

  • STW: No trigger

  • Features: Average speed

  • Special: The next phase is the most time-consuming and STW Remark phase, which operates on the entire YoungGen as GC-root. It would be nice if the four phases trigger one MonitorGC.

Final ReMark

  • Goal: The second (and final)STW phase of a GC event, the goal of which is to complete the marking of all living objects in the old age.
  • Mark range: Young Gen + Old Gen.
  • Thread: The GC thread executes exclusively
  • STW: Triggers stop-the-world
  • Features: Slow speed, can be said to be the bottleneck of the entire CMS-Old GC.

Special:

  1. Iterate over new generation objects and re-mark them
  2. Re-label according to GC Roots
  3. Go through old Dirty cards and re-mark them

Concurrent Sweep

  • Target: Removes garbage objects based on marking results.
  • Mark range: Old Gen.
  • Threads: The GC thread and the application thread also execute concurrently
  • STW: No trigger
  • Features: Average speed.

Concurrent Reset

  • Objective: To reset internal data related to the CMS algorithm in preparation for the next GC cycle.
  • Threads: The GC thread and the application thread also execute concurrently
  • STW: No trigger
  • Features: Average speed.

Summary of FREQUENTLY asked Questions about CMS

  1. What are the initial marks in the first phase of CMS recycling? STW?

STW is required for the initial tag, and to ensure low pauses, only objects referenced directly by GC-root are marked.

  1. What are the “concurrent marks” in the second phase of CMS recycling? STW?

Concurrent tokens do not STW, and the garbage collector thread works at the same time as the worker thread. Concurrent markup is extended based on the GC-root of the first-phase markup.

  1. What are the “re-marks” of the third stage of CMS recycling? STW?

STW is required for re-marking, new objects created in phase 2 need to be marked & existing objects may lose references and become garbage.

  1. Will CMS recycle phase 4 “concurrent cleanup” STW?

Concurrent cleanup is time-consuming and requires object cleanup. Garbage collection threads run concurrently with worker threads, but STW is not available and floating objects may be generated to generate 1.

  1. When CMS collects “concurrent marking” and “concurrent cleaning”, garbage collection threads and worker threads run in parallel, which will cause CPU resources to become a bottleneck. How to set the number of concurrent collection threads?

Default number of garbage collection threads started by CMS :(number of CPU cores +3) /4

  1. The CMS collector may occur during the concurrent cleanup phase"Concurrent Mode Failure"The problem? Why is that? How does the JVM resolve a Concurrent Mode Failure

In the concurrent cleanup phase, the system thread is working, which generates “floating garbage” (objects entered by YoungGC). In order to ensure that there is a certain amount of memory during the collection period, the CMS will set aside some space for some objects to enter the old age. – XX: CMSInitiatingOccupancyFraction setting percentage (68%) by default, the remaining 1.8 later changed to 92%.

  1. What if the CMS has less free space than the promoted object size during the recycle period?

A “Concurrent Mode Failure” error occurs, which means that Concurrent garbage collection fails, and the JVM updates its processing policy: The Serial Old garbage collection will be automatically enabled to replace CMS collection. Serial Old will force STW, re-trace gC-roots to mark all garbage objects, do not allow new objects to be generated, clean garbage objects once, and then restore system threads.

  1. CMS memory defragmentation will STW? Why is that?

First of all, memory defragmentation will STW, and memory fragmentation will block the allocation of continuous space, and the JVM will trigger FullGC frequently. So the CMS will be according to the Settings – XX: CMSFullGCsBeforeCompaction = n mean in the last time the CMS concurrent GC after the execution, exactly how many times have to perform full GC will do compression.

  • The default is 0, which means that in the default configuration, compression is performed every time the CMS GC fails and goes to full GC.

  • If the CMSFullGCsBeforeCompaction configured for 10, you will make the above said the first condition of every 10 times to do a real full GC compression.

  1. How many situations can trigger GC execution in older generations?

    1. The available space of the old generation is smaller than the size of all objects of the new generation. If the space guarantee policy is not enabled, FullGC will be executed. Generally, the default guarantee policy is enabled.
    2. The average size of Cenozoic GC after entering the old age is larger than the available space in the old age.
    3. The space used in the old age is greater than-XX:CMSInitiatingOccupancyFractionSet the threshold.
  2. What is distributive guarantee? Is it on by default? Can the allocation guarantee be removed? Why do we need continuous space?

  • When a Minor GC occurs, the virtual machine checks whether the maximum contiguous space available for the old generation is greater than the total space for all objects of the new generation.

    • If it is, the Minor GC is safe.
    • If less, the virtual machine looks at HandlePromotionFailure Settings to assign guarantees.
  • HandlePromotionFailure=true, and whether the maximum contiguous space available in the old age is greater than the average size of the objects promoted to the old age, if so, a Minor GC is attempted.

  • HandlePromotionFailure=false or The maximum contiguous space available in the old age is less than the average size of the objects promoted to the old age, execute FullGC.

It is enabled by default after 1.6. You can remove it, which will increase the probability of FullGC. The vm stops temporarily, causing throughput and performance deterioration. The new generation uses the replication algorithm, which determines the need for continuous space.