How Does the CMS Garbage Collector Minimize Pause Times? A Step-by-Step Guide
This article explains the Concurrent Mark Sweep (CMS) garbage collector, detailing its five phases—Initial Mark, Concurrent Mark, Remark, Concurrent Sweep, and Concurrent Reset—while highlighting how each step works to minimize pause times during old‑generation memory reclamation in Java.
CMS (Concurrent Mark Sweep) is a concurrent garbage collector designed to achieve the shortest possible pause times, primarily used for the old generation in Java.
1. Initial Mark (Initial Mark)
This single‑threaded phase stops all application threads (STW) and marks objects in the old generation that are directly reachable from GC roots, such as static variables and thread stacks. Because it only marks directly reachable objects, the pause time is short.
2. Concurrent Mark (Concurrent Mark)
Starting from the objects marked in the initial phase, the collector traverses object references to mark all reachable objects while the application continues to run. New objects created during this period are considered “floating garbage”.
3. Remark (Remark)
This STW phase corrects any marking errors caused by concurrent execution, rescanning the old generation to capture objects missed during the concurrent mark. It uses the incremental update algorithm of the three‑color marking scheme.
4. Concurrent Sweep (Concurrent Sweep)
Running concurrently with the application, the collector sweeps the old generation, reclaiming memory occupied by unmarked objects. Objects allocated during this phase are marked black in the three‑color scheme and are not processed until the next collection.
5. Concurrent Reset (Concurrent Reset)
This final concurrent phase clears and resets the data structures used by the CMS collector, preparing it for the next garbage‑collection cycle.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
