☕[ JVM technical guide] (4) G1 garbage collector SATB of garbage collection system


☕[ JVM technical guide] (4) G1 garbage collector SATB of garbage collection system

Object omission

In the concurrent marking stage of garbage collection, GC threads and application threads execute concurrently, so after an object is marked, the application thread may tamper with the reference relationship of the object,As a result, the missing mark and wrong mark of the object are caused.

  • Mislabeling doesn’t matter. At most, it will cause floating garbage, which can still be recycled in the next GC.

  • The consequence of missing the target is fatal. The object that should have survived is recycled, which affects the correctness of the program.

In order to solve the problem of missing labels of living objects in the process of concurrent marking, GC Handbook divides the objects into three colors:

  1. Black: itself and reachable objects have been marked
  2. Gray: it is marked, but the reachable object is not marked
  3. White: not yet marked

Therefore, missing labels only occur in white objects and meet any of the following conditions:

  1. During concurrency marking, the application thread assigns the white object to the reference type field of a black object
  2. When marking concurrency, the application thread deletes all references from gray objects to the white object

For the first case, use the post write barrier to record all new reference relationships, and then rescan the root according to these reference relationships

For the second case, use the pre write barrier to record all the old references of the reference relationship to be deleted, and finally scan again with these old references as the root


The full name of SATB [snapshot at the beginning], an algorithm developed by Taiichi Yuasa for incremental mark removal garbage collector, is mainly applied to the concurrent marking stage of garbage collection, and solves the potential risk of long-time STW in the re marking stage of CMS garbage collector.

Region contains five pointers: bottom, previous TAMs, next TAMs, top and end

☕[ JVM technical guide] (4) G1 garbage collector SATB of garbage collection system

Among them, previous TAMs and next TAMs are the full names of the positions when concurrent tags occur before and aftertop-at-mark-start

  1. Assuming that the nth round of concurrent marking starts, assign the current top pointer of the region to next TAMs. During the concurrent marking period, the allocated objects are between [next TAMs, top]. SATB can ensure that these objects will be marked and are alive by default

  2. When the concurrency marking ends, assign the address of the next TAMs to the previous TAMs, and the SATB creates a snapshot bitmap for the objects between [bottom, previous TAMs], and all garbage objects can be identified through the snapshot

  3. The N + 1 round of concurrency marks the beginning, and the process is the same as that of the N round

SATB ensures that the newly allocated objects will not miss the mark during the concurrent marking process

  • However, if a white object W is referenced by a gray object G before TAMs, and is assigned null before the concurrent tag scans this field, cutting off the reference relationship between object W and object G, object W may be omitted. This is the second case where a white object is omitted?

How to solve it in G1?

Insert a layer of pre write barrier before the reference relationship is modified

☕[ JVM technical guide] (4) G1 garbage collector SATB of garbage collection system

Pre write barrier final execution logic:

☕[ JVM technical guide] (4) G1 garbage collector SATB of garbage collection system

adoptG1SATBCardTableModRefBS::enqueue(oop pre_val)Save the original reference to the SATB mark queue. Similar to the implementation of RSET, each application thread brings its own SATB mark queue

In the next concurrent marking phase, the objects in the SATB mark queue will be processed in turn to ensure that these objects survive this round of GC.

This work adoptsCC agreement, reprint must indicate the author and the link to this article