What do you know about CMS?

Concurrent mark sweep CMS(concurrent mark sweep) has been used in jdk1.5, which was released on September 30,2004. The goal of CMS design is to obtain the minimum stop the world pause time, which is based on the mark-clear algorithm. A common scenario is an Internet web site (with high service response requirements), which is an old garbage collector that can be used in conjunction with the Serial collector and the Parallel New collector. The CMS degrades to Serial Old when concurrent mode failure occurs.

What is the workflow (steps) of the CMS collector?

Four main stages

  • Initial tag: only tag objects that can be directly connected to GC Roots, fast, stop the world
  • Concurrency flag: Execute concurrently with the application thread, traversalInitial tagPhase marked objects, marking the reachable objects of these objects.
  • Re-mark: due toConcurrent tagsIs executed concurrently with the application thread, so some marked objects have changed. This process ratioInitial tagFor longer, but less thanConcurrent tagsPhases are short. Stop the world
  • Concurrent cleanup: Runs with application threads. Clean objects directly based on tag objects.

Disadvantages of CMS?

Debris problem

  • Reason: Because CMS usesMark-clearAlgorithm, so inevitably there will be memory fragmentation.
  • Solution: Use-XX:+CMSFullGCsBeforeCompaction=nIt means last timeCMSconcurrentGCAfter implementation, how much more to doFull GCJust do the compression. The default is 0, which means every timeCMS GCCan’t hold up the turnFull GCAll the time.

Concurrent mode failure

  • Cause: THE CMS garbage cleanup thread and the application thread execute concurrently, if the old age space is insufficient to accommodate the new object during the cleanup process.
  • Solution: Use-XX:+UseCMSInitiatingOccupancyOnlyand-XX:CMSInitiatingOccupancyFraction=60To specify that GC starts when the CMS memory usage reaches 60%.

The re-marking phase takes too long

  • Solution: Use-XX:+CMSScavengeBeforeRemark, in theTo markBefore you do it, do it onceYoung GCThe purpose is to reduce the invalid reference of the young generation to the old generationTo markOverhead.

Why is it configuredCMS GC“But it was triggeredFull GC?

  • When allocating large objects, the young generation can not put down, directly go to the old age, the result of the old age can not put down.
  • Memory fragmentation issues (usingMark-clearDisadvantages of algorithm)
  • CMS GCFailure (caused by concurrent mode failure)
  • Jmap-histo executed the command manually