In the JVM Hotspot, we know the JVM needs to stop the mutator threads(STW) during some marking phrases, such as in young generation and old generation initial marking phrases for GC type CMS and G1 when marking the roots.
May I know why must need the STW during these phrases? To avoid the race condition between the mutator and mark threads?
Cann't we use the same technologies as the concurrent marking phrases to avoid the STW?
When the CMS collector needs to run it needs to mark the root set. This is the set of objects directly accessible from the current stack frames plus static references. If you allow mutator threads to run while this is happening you may well miss references as stack frames are popped and new ones pushed.
After the root set has been marked, the GC has an accurate starting point from which to follow all other referencable objects from application code. This part can happen concurrently with mutator threads.
Once the full graph of reachable objects has been traced (concurrently) a second STW remarking phase is required so that changes made while the concurrent marking phase was in progress can be integrated into the list of valid object references.
At this point, a concurrent sweeping phase can be used (since all valid objects are now known) to update free lists of areas of memory available between valid objects. No compaction is done unless fragmentation becomes too great or free space becomes too small, at which point a full compacting collection is initiated.