2

There are 4 high level phases CMS works for full GC

  1. Initial mark :- Stop the world(STW)
  2. Concurrent marking :- Run concurrently
  3. Remark :- STW
  4. Concurrent sweeping:- Run concurrently

I have got high level understanding of CMS after reading

http://www.tikalk.com/java/garbage-collection-serial-vs-parallel-vs-concurrent-mark-sweep/ and https://plumbr.eu/handbook/garbage-collection-algorithms-implementations/concurrent-mark-and-sweep

My question is why initial mark stage is STW for Initial Mark phase ? Can't we have just Remark phase as STW as this is the final stage for reconciliation.

Similarly why Sweeping phase is not STW as it will require compaction which means change ofphsical location of object. So if object is referred by app and concurrent thread changes the phsical location, won't it be issue ?

I know i a missing something here but what's that ?

emilly
  • 10,060
  • 33
  • 97
  • 172

1 Answers1

1

I assume you are referring Concurrent Mark Sweep implementation in Oracle's HotSpot JVM.

Initial mark phase include scanning young space (any young to old reference is root for concurrent mark). Remark phase includes very same operation.

In theory, you could omit STW for initial mark as scan it concurrent with risk of missing few root (which will be recovered during final remark any way). Though there are draw backs here

  • Scanning of memory may be inaccurate as young collection is moving objects. Synchronization between old collector and young collector would become even more complicated.
  • If some root would be missed during inaccurate initial mark, remark may become significantly longer as it would involve traversing objects reachable from newly found roots.

Sweeping does not require STW because it does not compact. CMS does not compact old space in concurrent cycles.

Here is my article explaining nature and asymptotics of CMS GC pauses

Alexey Ragozin
  • 8,081
  • 1
  • 23
  • 23
  • The same question as OPs is bothering me (except the part with sweeping). I'm still confused after reading your answer. Why missing few roots is such a big problem, even if the initial mark is made with STW many new roots can also appear just after that phase. – ContentPenalty May 02 '19 at 21:24
  • Image fresh new object created. It is not marked as reachable yet. If GC would not be very consevative object would be considered garbage, but it is still reachable from object graph. – Alexey Ragozin May 04 '19 at 17:28
  • But isn't it corrected at the remark phase which processes all stuff that was modified from the point when marking has started? What does actually making initial mark STW change? – ContentPenalty May 04 '19 at 18:44
  • @ContentPenalty you are correct remark fixes this. Problem is majority of roots are local variables. GC have to scan stacks for application threads. It can be done only with STW. Without this concurrent mark would leave too much of objects unmarked dramatically increasing work for STW remark phase. – Alexey Ragozin May 05 '19 at 11:01
  • So if I understand correctly, initial mark is STW because scanning thread stacks can't be done concurrently. Do you know why, is it because of some physical limitations or just because it would be in some way problematic? – ContentPenalty May 05 '19 at 11:54