Understanding JVM Garbage Collection – Part 8 Concurrent Mark Sweep (CMS) Collector

The CMS collector uses an evacuating collector for the young generation and a mostly concurrent mark sweep (non-compacting) algorithm for the tenured. CMS young generation GC is very similar to the parallel collector. The CMS garbage collector also requires coordination between the young and tenured GC. This is why CMS uses a parallel collector (ParNew) with a callback interface for the young generation GC. CMS's major difference is in its old generation GC.

CMS uses a STW parallel evacuating collector to collect the young generation. This is similar to the parallel collector, also known as Parallel Savage. CMS utilizes a parallel collector called ParNew, which is essentially the same as Parallel Savage but includes a callback interface. The concurrent collector uses this callback interface.

CMS performs a concurrent cycle to clean data from the old generation (CMS). The CMS garbage collection runs GC threads alongside application threads. The main goal of the CMS collector is to minimize garbage collection pauses, which involves a tradeoff of sharing application processing resources with garbage collection. Performing GC concurrently with application threads becomes problematic only if both compete for the same CPU cycles. Doing GC in conjunction with running application threads leads to a more complex set of GC phases.

The CMS GC cycle includes the following phases:

The CMS GC cycle consists of the following phases:

  • Initial Mark: A stop-the-world operation that identifies objects reachable from roots, followed by resuming all application threads.
  • Concurrent Mark: Calculation of the reachable object graph using one or more GC threads in conjunction with application threads.
  • Concurrent Pre-clean: Retracing modified sections of the object graph using a single thread.
  • Remark (STW): A stop-the-world operation that retracts sections of the roots and object graph modified since the pre-clean phase. Application threads resume at the phase's end.
  • Concurrent Sweep: Use of a single processor to add unreachable objects to the free lists for future allocation. Concurrent Reset: Use of a single processor to prepare for the next GC cycle.

he CMS collector also employs an evacuating collector to free space in the young generation, while utilizing a mark and sweep collector in the old generation. CMS does not compact the tenured generation. When compaction becomes necessary, CMS falls back on using ParallelOld. CMS falls back on using ParallelOld under two main circumstances:

  • Concurrent mode failure - Occurs when Tenured does not have sufficient space to accommodate objects promoted from the young generation. Essentially, the allocation pressure on Tenured was higher than CMS's cleaning rate for Tenured.
  • Fragmented tenured - When Tenured is too fragmented, it prevents memory allocation.

In both cases, CMS resorts to using ParallelOld for garbage collection on Tenured. If necessary, CMS can also perform a full GC.

Tuning CMS is a non-trivial task that requires both GC tuning and application modifications. Making adjustments to tune CMS in the application can be particularly challenging. As of Java 9, CMS has been deprecated and is scheduled to be removed in JDK 14.

No comments yet.

Leave a Reply

2 × four =