java - 为什么JVM CMS(concurrent mark-and-sweep) GC需要两次停顿?

标签 java garbage-collection concurrent-mark-sweep

我想知道为什么 CMS 需要两个阶段(以及两个暂停)的标记:即初始标记和备注。能不能简单的做一个标记,然后扫一扫?我想这可以是一个更快的暂停。有人可以帮助解释第二个标记的主要目的是什么以及我们为什么需要它吗?谢谢!

最佳答案

这在 HotSpot 内存管理白皮书中有很好的解释:

A collection cycle for the CMS collector starts with a short pause, called the initial mark, that identifies the initial set of live objects directly reachable from the application code. Then, during the concurrent marking phase, the collector marks all live objects that are transitively reachable from this set. Because the application is running and updating reference fields while the marking phase is taking place, not all live objects are guaranteed to be marked at the end of the concurrent marking phase. To handle this, the application stops again for a second pause, called remark, which finalizes marking by revisiting any objects that were modified during the concurrent marking phase. Because the remark pause is more substantial than the initial mark, multiple threads are run in parallel to increase its efficiency. At the end of the remark phase, all live objects in the heap are guaranteed to have been marked, so the subsequent concurrent sweep phase reclaims all the garbage that has been identified.

关于java - 为什么JVM CMS(concurrent mark-and-sweep) GC需要两次停顿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371984/

相关文章:

将对象设置为 null 时的 JavaScript(ES6) WeakMap 垃圾回收

java - 为什么调整 java 堆分配大小会导致 OOME?

go - 生产中的 FreeOSMemory()

java - Riemann Siegel Formula in Java的实现,好奇改进余数项

java - 优先队列,同时防止饥饿

java - UseConcMarkSweepGC 与 UseParallelGC

java - Concurrent Mark Sweep (CMS) 是停止世界事件吗?

java - jmap 实时堆转储如何包含无法访问的对象?

java - FIrebaseUI(版本 0.4.0): firebase recycler Adapter not fetching data from firebase database

java - 您可以放入 Eclipse java 文档中的代码行数是否有限制