Does Java Garbage Collect always has to “Stop-the-World”?

前端 未结 4 1363
一个人的身影
一个人的身影 2021-02-04 08:36

I am trying to understand Java\'s garbage collection more deeply.

In HotSpot JVM generational collection, in the heap, there are three areas (Young generation, Old gener

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 09:13

    All freely available GCs in openjdk have some stop the world events. And not just the GCs, other things such as deoptimizations can trigger safepoints too.

    But not all pauses are equal. CMS and G1 do not need to scale their pause times with the live data set in the old generation because they only scan a subset of the objects during the pauses and do a major fraction of their work concurrently, unlike the Serial and Throughput collectors.

    ZGC (available since OpenJDK11) and Shenandoah (since 12) are collectors that further decouple pause times from the live data set size and scale their pauses with only the root set size instead.

    Additionally other GC implementations exist which avoid global pauses - they may still experience per-thread pauses - or make the pause durations O(1), i.e. independent of live data set size. A commonly cited example is azul's C4 collector.

    So the second question comes to why the compaction needs a STW pause?

    Compacting means moving objects. Moving objects means pointers need to be updated. This is very difficult or costly to achieve safely when applications threads are still running.

    Concurrent algorithms generally pay some cost in throughput and complexity in exchange for their lower pause times. Not doing compactation makes CMS relatively(!) simple for a concurrent collector.

提交回复
热议问题