Quick revision
The short answer
Concurrent collectors track marking with three colours: white for objects not seen yet, grey for objects seen whose fields are not scanned, and black for objects fully scanned. The danger, the lost object problem, is that the application stores a reference to a white object into a black one and removes the last grey path to it, so the collector would free a live object. G1 and Shenandoah prevent this with snapshot-at-the-beginning write barriers that record overwritten references, while ZGC uses load barriers with coloured pointers, and Shenandoah uses load reference barriers for concurrent relocation.
What to remember
In this answer, we saw that concurrent marking uses three colours and is correct as long as a black object never points to a white one that has no other grey path. The application can break that by copying a reference into a black object and deleting the last grey path, the lost object problem. G1 and Shenandoah stop it with SATB write barriers that record overwritten references, ZGC uses load barriers on coloured pointers, and Shenandoah uses load reference barriers for relocation. These barriers run in our threads, which is part of why concurrent collectors use more CPU.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Concurrent collectors track marking with three colours: white for objects not seen yet, grey for objects seen whose fields are not scanned, and black for objects fully scanned. The danger, the lost object problem, is that the application stores a reference to a white object into a black one and removes the last grey path to it, so the collector would free a live object. G1 and Shenandoah prevent this with snapshot-at-the-beginning write barriers that record overwritten references, while ZGC uses load barriers with coloured pointers, and Shenandoah uses load reference barriers for concurrent relocation.
Why this question is asked
Short pauses on big heaps are only possible because marking runs while our threads keep changing the object graph. That sounds unsafe, and it is, unless the collector and the application cooperate through barriers. Interviewers ask this to see whether we understand where the extra CPU of concurrent collectors goes and why a concurrent collector can never be as cheap as a stop-the-world one. In this answer, we will recap tri-color marking, reproduce the lost object problem step by step, see how SATB write barriers fix it in G1 and Shenandoah, see how ZGC's load barriers work, and look at what barriers cost.
1A Quick Recap of Tri-Color Marking
Marking finds every object reachable from the roots. To describe its progress, we give each object a colour. White means the collector has not reached it yet. Grey means it has been reached, but its reference fields have not been scanned. Black means it has been reached and all its fields have been scanned, so everything it points to is at least grey.
Marking starts with the roots' direct targets grey and everything else white. The collector repeatedly takes a grey object, colours every white object it references grey, and turns the object black. When no grey objects remain, marking is done, and every object still white is unreachable and can be freed. In a stop-the-world collector this is all safe, because the graph does not change during the traversal.
- Colour root targets greyeverything else is white
- Any grey object left?Yestake oneNomarking done: white objects are garbage
- Scan its fieldsturn each white target grey
- Turn the object blackall its references are now at least grey
4 steps in order. Play them, or step through them one at a time.
2Reproducing the Lost Object Problem
Now let the application run during marking. The invariant that keeps marking correct is that a black object never points directly to a white object, because the collector will not look at a black object again. Here is how the application can break it.
5 messages in order. Play them, or step through them one at a time.
A barrier has to stop one of the two application steps from going unnoticed.
Notice that both steps are needed. If the application only stored C into A, the collector would still reach C through B. If it only removed B's reference, C really would be unreachable. The lost object appears when a reference to a white object is copied into a black object and the last grey path to it is destroyed while marking is in progress. A barrier must break one of these two conditions.
3SATB Write Barriers in G1 and Shenandoah
G1 and Shenandoah break the second condition with snapshot-at-the-beginning marking, SATB for short. The promise is that every object that was reachable when marking started will be kept alive in this cycle, even if it becomes unreachable during it. To keep that promise, the JIT puts a write barrier, a small piece of code, before every store of a reference field. While marking is active, the barrier records the old value being overwritten into a buffer, and the collector later marks everything in that buffer.
So in our example, B.field = null logs C before it is removed, and C is marked even though B no longer points to it. Objects that became garbage during the cycle survive until the next one, which is called floating garbage, a small price for correctness. G1 also uses a post-write barrier for a different purpose: to keep its remembered sets up to date, recording references between regions.
4Load Barriers in ZGC and Shenandoah
ZGC works differently. It uses coloured pointers, references that carry a few metadata bits about the state of the object they point to. Every time the application loads a reference from the heap, a load barrier checks these bits. If the reference is not in the expected state for the current phase, the barrier takes a slow path: it marks the object, or, if the object has been relocated, finds its new address and fixes the reference in place, so later loads take the fast path. This is how ZGC can mark and move objects while the application runs, because the application never sees a stale reference.
Generational ZGC, the only mode since JDK 24, also adds store barriers to track references between generations and support its marking, so it uses both kinds. Shenandoah uses SATB write barriers for marking and a load reference barrier for evacuation: when the application loads a reference to an object in a region being evacuated, the barrier ensures it gets the new copy, copying the object itself if the collector has not done it yet.
5What Barriers Cost
Barriers run in our own threads on very frequent operations. Most of the time they take the fast path, a test and a branch, but even that adds instructions to hot code, takes space in the instruction cache, and limits some JIT optimisations. Together with the collector's background threads, this is why concurrent collectors typically use more total CPU than Parallel GC, whose stop-the-world design needs almost no barriers beyond card marking. A throughput difference of a few percent to more than ten percent is common, depending on the workload and the JDK version, so check with your own benchmark.
6Follow-up questions
What is the other way to break the lost object condition?
An incremental update barrier, used by CMS, breaks the first condition instead. When the application stores a reference to a white object into a black object, the barrier records the black object, or greys the target, so the collector rescans it. It keeps less floating garbage than SATB, but it needs a final rescan at the end of marking, which can make the remark pause longer.
Why does SATB need a remark pause at all?
At the end of concurrent marking, some threads may still have partly filled SATB buffers, and new objects allocated during marking must be handled. G1's remark pause drains the remaining buffers and finishes marking. It is short, because most of the work was done concurrently.
Are objects allocated during marking white?
No. Collectors treat objects allocated during the marking cycle as live for that cycle, for example G1 considers everything above the top-at-mark-start pointer of a region as implicitly marked. Otherwise a brand-new object stored only into a black object would be lost.
7Conclusion
In this answer, we saw that concurrent marking uses three colours and is correct as long as a black object never points to a white one that has no other grey path. The application can break that by copying a reference into a black object and deleting the last grey path, the lost object problem. G1 and Shenandoah stop it with SATB write barriers that record overwritten references, ZGC uses load barriers on coloured pointers, and Shenandoah uses load reference barriers for relocation. These barriers run in our threads, which is part of why concurrent collectors use more CPU.
Open with
Concurrent marking uses three colours. White objects have not been reached, grey ones have been reached but their fields are not scanned, and black ones are fully scanned.
Then, if they want more
The collector keeps taking grey objects, greying their white children and blackening them; when nothing is grey, white objects are garbage. The problem is that the application keeps running.
If it stores a reference to a white object into a black object, and then removes the last grey path to that white object, the collector never scans the black object again, so it frees a live object. That's the lost object problem, and barriers prevent it.
G1 and Shenandoah use snapshot-at-the-beginning: a pre-write barrier on every reference store records the old value while marking is active, so every object reachable at the start of marking survives the cycle, at the cost of some floating garbage.
ZGC uses coloured pointers and a load barrier on every reference load, which marks or remaps the reference if its colour is stale, and that is also how it relocates objects concurrently; generational ZGC adds store barriers too. Shenandoah uses a load reference barrier for evacuation.
All these barriers add instructions to hot code, which together with the background GC threads is why concurrent collectors cost more CPU than Parallel GC.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- HotSpot Garbage Collection Tuning Guide, JDK 21
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do concurrent collectors like G1, ZGC and Shenandoah mark objects while the...": https://scrivio-interview-library.netlify.app/java/garbage-collection/aab0001705/