Quick revision
The short answer
ZGC does marking, relocation and reference processing concurrently with the application, and its pauses only synchronise phases and scan a small set of roots, so they do not grow with the heap. It can move objects while the application runs because every reference carries a few metadata bits, its colour, and a load barrier on every reference load checks those bits and fixes a stale pointer, for example one to an object that has just been relocated. Generational ZGC, added in JDK 21, splits the heap into young and old generations so short-lived objects are collected cheaply; it became the default in JDK 23 and the only mode in JDK 24, and the costs are more CPU, barrier overhead, no compressed oops, and allocation stalls when the collector falls behind.
What to remember
In this answer, we saw that ZGC keeps pauses tiny by doing marking, relocation and reference processing concurrently, leaving the pauses only to switch phases and scan small root sets. Coloured pointers tell whether a reference is up to date, and the load barrier on every reference load fixes stale ones, relocating or forwarding the object and healing the field. Generational ZGC, added in JDK 21, collects young objects cheaply with the help of store barriers and remembered sets, became the default in JDK 23 and the only mode in JDK 24. The price is CPU, barrier overhead, no compressed oops and the risk of allocation stalls.
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.
ZGC does marking, relocation and reference processing concurrently with the application, and its pauses only synchronise phases and scan a small set of roots, so they do not grow with the heap. It can move objects while the application runs because every reference carries a few metadata bits, its colour, and a load barrier on every reference load checks those bits and fixes a stale pointer, for example one to an object that has just been relocated. Generational ZGC, added in JDK 21, splits the heap into young and old generations so short-lived objects are collected cheaply; it became the default in JDK 23 and the only mode in JDK 24, and the costs are more CPU, barrier overhead, no compressed oops, and allocation stalls when the collector falls behind.
Why this question is asked
Most collectors stop the application to move objects, because moving an object means every reference to it must be updated, and the application must not use a reference while it is being changed. ZGC breaks that rule, and the result is pauses under a millisecond on heaps of hundreds of gigabytes. Interviewers ask how because the answer shows whether we understand barriers. In this answer, we will recap why moving objects is the hard part, see how coloured pointers and load barriers solve it, follow one relocated object, see what the pauses still do, and then look at what generational ZGC changed and what all this costs.
1A Quick Recap: Why Moving Objects Is Hard
A compacting collector moves live objects together so free memory is contiguous. After an object moves, every reference to it, from other objects, from stacks and from registers, points to the old address. In a stop-the-world collector this is easy: the application is frozen, the collector updates all references, and then the application continues. The pause is proportional to the live data and the references, which is why G1 pauses grow with the amount of data copied.
To move objects while the application runs, the collector needs a way to make sure the application never uses an old address. ZGC does this lazily: it does not update every reference at once, but it guarantees that any reference the application loads from the heap is checked and corrected on the spot.
2Coloured Pointers and the Load Barrier
In ZGC, a 64-bit reference holds the object's address plus a few metadata bits. These bits are called the colour, and they say which phase of the collection the pointer is up to date with: whether it has been marked in the current cycle, and whether it is known to point to the object's current location. At any moment, the collector has one good colour, and a pointer with any other colour is stale.
Whenever the application loads a reference from a field of a heap object, the JIT inserts a load barrier, a short check right after the load.
Notice that the common case is one test and a branch that is not taken: most loads find a pointer that is already good. Only the first load of a stale pointer takes the slow path, and because the slow path writes the corrected pointer back into the field, the next load of that field is fast again. This self-healing means the fixing work is spread across the application threads and the GC threads, a little at a time, instead of being done in one long pause.
3Following One Relocated Object
Suppose ZGC decides to compact a page, its name for a region, holding an Order object. It records the page in a relocation set and starts moving its live objects to a new page, keeping a forwarding table that maps old addresses to new ones. Then it changes the good colour, so every existing pointer into that page is now stale.
Now an application thread loads cart.order. The barrier sees a stale colour and takes the slow path. If the Order has already been moved, the slow path looks up its new address in the forwarding table. If not, the application thread copies the object itself, racing safely with GC threads through an atomic update of the forwarding table. Either way, it writes the new, good pointer back into cart.order and continues with the new address.
9 messages in order. Play them, or step through them one at a time.
The application never uses the old address; whoever reaches the object first moves it.
4What the Pauses Still Do
ZGC still has short pauses, Pause Mark Start, Pause Mark End and Pause Relocate Start. They flip the good colour, so that all pointers become stale at a well-defined moment, process the thread stacks' top frames, and hand over between phases. In modern ZGC even thread stacks are mostly scanned concurrently, a frame at a time, so the work inside the pause is roughly constant. That is the reason pauses stay well under a millisecond, typically tens to a few hundred microseconds, whether the heap is 2 GB or 2 TB.
5What Generational ZGC Changed
The original ZGC marked the whole heap in every cycle. For applications where most objects die young, that is wasted effort, and it meant ZGC needed a lot of headroom to survive high allocation rates. Generational ZGC, JEP 439 in JDK 21, adds a young and an old generation. Young collections run often and only trace the young generation, which is cheap because most of it is garbage; the old generation is collected less often.
Generations need a way to find old-to-young references, so generational ZGC added store barriers that record them in remembered sets, and it reworked the colour bits; it also stopped relying on multi-mapping the heap in virtual memory, which the original design used. It became the default ZGC mode in JDK 23 and the non-generational mode was removed in JDK 24.
| Aspect | Non-generational | Generational (JDK 21+) |
|---|---|---|
| Each cycle traces | the whole heap | young often, old rarely |
| Barriers | load barriers | load barriers plus store barriers |
| Headroom needed | high under heavy allocation | lower for typical workloads |
| Status in JDK 25 | removed in JDK 24 | the only mode |
6What It Costs
Concurrent work uses CPU while the application runs, so ZGC usually spends more total CPU on GC than G1. Every reference load pays for the barrier test, and in generational mode reference stores pay too, which costs some application throughput. Because colour bits live in the 64-bit pointer, ZGC cannot use compressed oops, the 32-bit references HotSpot normally uses for heaps under 32 GB, so the same objects take more memory.
Finally, ZGC does not fall back to a long pause when it gets behind. If the application allocates faster than concurrent cycles can free memory, threads that need memory wait, an allocation stall, which hurts latency even though no GC pause is recorded.
7Follow-up questions
Why does ZGC usually need more heap headroom than G1 for the same live set?
Because the application keeps allocating while a cycle runs, and memory is only returned when the cycle finishes relocating. The heap must hold the live set, everything allocated during the cycle, and the floating garbage that died during it. G1 instead stops the application to collect, so allocation and collection never overlap. On top of that, ZGC's uncompressed references make the live set itself larger. Generational mode reduces the headroom needed, but it is still common to give ZGC 20 to 50 percent more heap than G1.
What is self-healing in ZGC?
When a load barrier takes the slow path and finds the correct pointer, it stores that pointer back into the field it came from. Later loads of the same field find a good pointer and skip the slow path. So each stale reference is fixed at most once, by whichever thread touches it first.
8Conclusion
In this answer, we saw that ZGC keeps pauses tiny by doing marking, relocation and reference processing concurrently, leaving the pauses only to switch phases and scan small root sets. Coloured pointers tell whether a reference is up to date, and the load barrier on every reference load fixes stale ones, relocating or forwarding the object and healing the field. Generational ZGC, added in JDK 21, collects young objects cheaply with the help of store barriers and remembered sets, became the default in JDK 23 and the only mode in JDK 24. The price is CPU, barrier overhead, no compressed oops and the risk of allocation stalls.
Open with
ZGC keeps pauses short by not doing the expensive work in them. Marking, relocating objects and processing references all run concurrently with the application; the pauses only switch phases, flip which pointer colour counts as good, and scan a small set of roots, so they stay under a millisecond regardless of heap size.
Then, if they want more
The key mechanism is coloured pointers and load barriers. Each reference carries metadata bits that say whether it is up to date with the current cycle. Every time the application loads a reference from the heap, the JIT-inserted barrier checks those bits.
If they are stale, it takes a slow path: it marks the object, or looks up the object's new address in the forwarding table, or relocates the object itself if no one has yet, and it writes the good pointer back into the field, so each stale reference is healed once.
Generational ZGC, from JDK 21, added young and old generations, with store barriers and remembered sets, so short-lived objects are collected cheaply and less headroom is needed. It became the default in JDK 23 and the only mode in 24.
The costs are more CPU for concurrent work, barrier overhead in application code, no compressed oops, and allocation stalls when allocation outruns the collector, instead of a long pause.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 23, 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 does ZGC keep application pauses well under a few milliseconds regardless of heap...": https://scrivio-interview-library.netlify.app/java/garbage-collection/a48a0c3bcf/