CMS GC初始标记阶段为何采用串行线程而非并行?
Why CMS Initial Mark is Serial But Remark is Parallel?
Great question! This comes down to the different workloads and cost-benefit tradeoffs of each STW phase in CMS. Let’s break it down clearly:
Initial Mark: Serial for Efficiency
- What it does: This phase only marks objects directly reachable from GC Roots—think stack references, static class variables, JNI handles, etc. The size of this initial set is surprisingly small, so the whole operation finishes in microseconds to a few milliseconds—blink-and-you-miss-it fast.
- Why serial?: Parallelism isn’t free. Spinning up multiple threads, handling context switches, and syncing between them adds overhead that would actually lengthen the already tiny STW time here. Serial execution is simpler, faster, and avoids all that unnecessary complexity for such a minimal task.
Remark: Parallel to Cut STW Time
- What it does: After the concurrent marking phase (where user threads keep running), the remark phase has to clean up all the loose ends:
- Process SATB (Snapshot-At-The-Beginning) logs that track reference changes made by user threads during concurrent marking
- Update mark states for objects that were missed or had their references modified
- Scan edge-case GC Roots (like weak JNI references) that weren’t covered in initial mark
This is a way larger workload than initial mark, often taking tens to hundreds of milliseconds—long enough that users might notice pauses.
- Why parallel?: Here, the payoff of splitting work across threads far beats the synchronization overhead. Parallel execution slices the workload into chunks (e.g., assigning different SATB queues or GC Root subsets to each thread) and cuts the STW time of this phase significantly. The extra complexity is worth it to keep application pauses as short as possible.
In a nutshell: CMS picks the threading model based on how much work each phase needs to do. For tiny, fast tasks, serial is smarter. For bigger, slower tasks, parallel delivers real performance gains.
内容的提问来源于stack exchange,提问作者Stephan_Berlin
相关产品推荐
相关产品推荐

