Quick revision
The short answer
A young collection treats old-to-young references as extra roots, and the JVM finds them without scanning the old generation by recording where they might be. A write barrier marks a small piece of old memory, a card, as dirty whenever a reference store might create such a pointer, and the young collection scans only the dirty cards. G1 goes further and keeps a remembered set per region, so it can collect any set of regions, and it pays for this with barrier work on every reference store, memory for the sets, and pause time when too many cards are dirty.
What to remember
In this answer, we saw why a young collection needs every old-to-young pointer as a root and why scanning the old generation to find them would defeat the purpose. The post-write barrier dirties a 512-byte card when a reference is stored, and the young collection scans only dirty cards. G1 adds per-region remembered sets so it can evacuate any chosen set of regions, and concurrent refinement threads keep the work out of the pause. The price is barrier work on every store, memory for the sets, refinement CPU, and longer pauses when cards are dirtied faster than they can be refined.
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.
A young collection treats old-to-young references as extra roots, and the JVM finds them without scanning the old generation by recording where they might be. A write barrier marks a small piece of old memory, a card, as dirty whenever a reference store might create such a pointer, and the young collection scans only the dirty cards. G1 goes further and keeps a remembered set per region, so it can collect any set of regions, and it pays for this with barrier work on every reference store, memory for the sets, and pause time when too many cards are dirty.
Why this question is asked
Generational collection only works if collecting the young generation is cheap, and it is only cheap if the collector can ignore most of the heap. But old objects do point at young ones: a cache entry updated with a fresh value, a session that gets a new list. If the collector missed those pointers, it would free objects that are still in use. Interviewers ask this question to see whether we know how the JVM keeps young pauses small, and why some workloads still have slow young pauses. In this answer, we will recap the problem, see how a card table and its write barrier work, look at how G1 extends the idea with remembered sets and refinement threads, and then count the costs.
1A Quick Recap: Why Old-to-Young Pointers Matter
A young collection finds the live objects in the young generation and copies them somewhere safe. It starts from the roots, such as local variables on thread stacks and static fields; anything reachable from a root is live.
There is a catch. A young object can be reachable only through an old object. If we trace from the roots, we would have to walk through the old generation to find it, and the old generation can be ten or a hundred times larger than the young one. Walking all of it at every minor collection would make generational collection pointless.
So the collector needs another source of roots: every reference stored in an old object that points into the young generation. The trick is to track where those references might be, at the moment they are written, instead of searching for them later.
2How a Card Table and a Write Barrier Work
HotSpot divides the old generation into cards, small fixed ranges of memory, 512 bytes each by default. The card table is a byte array with one entry per card. A card is either clean or dirty.
Whenever the application stores a reference into a field of an object, the JIT compiler adds a few extra instructions right after the store. This is the post-write barrier, a small piece of code that runs after every reference write. In its simplest form, used by the Serial and Parallel collectors, it just marks the card that contains the written field as dirty.
Notice that the barrier does not check whether the new value is young. It marks the card unconditionally, because one byte store is cheaper than a test. So a dirty card means 'something here may point to a young object', not 'something here does'.
At the next young collection, the collector walks the card table, and for each dirty card it scans the objects in that 512-byte range. Every reference that points into the young generation is treated as an extra root. Then the card is cleaned. So it scans a few cards instead of gigabytes of old objects.
3What G1 Adds: Remembered Sets Per Region
G1 splits the heap into equal regions, typically 1 to 32 MB each, and during mixed collections it collects a chosen set of old regions too, not just the young ones. To evacuate a region, which means copying its live objects elsewhere, G1 must find and update every reference into that region from outside it.
So each region has a remembered set: a record of which cards in other regions contain references into it. Young regions always have one, because they are collected every time. Old regions get one when they become candidates for collection, which recent JDKs build during concurrent marking only for the regions worth collecting.
G1's post-write barrier is smarter than the simple one. It filters out stores that cannot matter: a null value, or a reference that stays within the same region. For the rest, it dirties the card and puts it into a dirty card queue. It does not update the remembered set directly, because that would make every store slow.
- Application stores a reference
cache.latest= newValue() - Cross-region and not null?YesDirty the card, enqueue itNoNothing else to do
- Refinement thread takes the cardruns concurrently, outside the pause
- Scan the card's referencesadd the card to the remembered sets of target regions
- Young pause startsmerge remembered sets of the collection set
- Scan remaining dirty cardscards not yet refined are scanned in the pause
- Evacuate live objectsand update the pointers that were found
7 steps in order. Play them, or step through them one at a time.
Refinement moves most card work out of the pause; whatever is left is paid for inside it.
4Concurrent Refinement Threads
Processing a dirty card means reading its references, finding which regions they point into, and adding the card to those regions' remembered sets. G1 does this with concurrent refinement threads, background threads that drain the dirty card queues while the application runs.
The reason is pause time. Any card that has not been refined when a young pause starts must be scanned inside the pause. G1 sets a target for how many unrefined cards it will tolerate, based on how long scanning them took in recent pauses and on the pause time goal, and it activates more refinement threads when the queue grows past that target. In logs with -Xlog:gc+phases=debug, this work shows up under phases such as Merge Heap Roots and Scan Heap Roots; the names change between JDK versions, so check your version.
5What It All Costs
The first cost is on the application threads. Every reference store runs a barrier. For Serial and Parallel it is one extra byte store. For G1 it is a filter, a check of the card, and sometimes an enqueue, plus a pre-write barrier used for marking.
The second cost is memory. Remembered sets can take a noticeable fraction of the heap, a few percent in typical services and more when many regions point at many others. The third cost is CPU for the refinement threads, which compete with the application.
The last cost is pause time. If the application dirties cards faster than refinement can process them, the young pause scans many cards itself and the Scan Heap Roots phase grows, even though eden and the surviving data are the same size.
| Cost | When it is paid | Where we see it |
|---|---|---|
| Write barrier on reference stores | every store, in application threads | lower throughput than Parallel GC |
| Remembered set memory | all the time | native memory tracking, GC part |
| Refinement CPU | concurrently, between pauses | busy G1 concurrent threads |
| Scanning unrefined dirty cards | inside the young pause | Scan Heap Roots phase in gc+phases logs |
6Follow-up questions
Why can a workload that constantly updates old objects to point at new ones have long young pauses?
Every such update dirties a card in an old region. If the application does it millions of times per second, for example by replacing values in a large long-lived map, the refinement threads fall behind and many cards are still dirty when the pause starts, so the pause scans them itself. There is a second effect: each of those references keeps a young object alive, so more objects survive and must be copied, and copying survivors is the main cost of a young pause. The fix is usually in the data structure, for example storing primitive values instead of new objects, or replacing whole immutable snapshots less often.
Does the card table make the young collection exact?
No, it is conservative. A dirty card says a reference may point into the young generation. The collector scans the card and keeps only the references that really point into the collected regions. A dead old object on a dirty card can even keep a young object alive until the old object is collected; this floating garbage is accepted in exchange for a cheap young pause.
7Conclusion
In this answer, we saw why a young collection needs every old-to-young pointer as a root and why scanning the old generation to find them would defeat the purpose. The post-write barrier dirties a 512-byte card when a reference is stored, and the young collection scans only dirty cards. G1 adds per-region remembered sets so it can evacuate any chosen set of regions, and concurrent refinement threads keep the work out of the pause. The price is barrier work on every store, memory for the sets, refinement CPU, and longer pauses when cards are dirtied faster than they can be refined.
Open with
A young collection has to treat any old object that points to a young one as a root, but scanning the whole old generation would make the pause as slow as a full collection.
Then, if they want more
So HotSpot divides the old generation into 512-byte cards with a byte per card in a card table. The JIT adds a post-write barrier after every reference store, and in Serial or Parallel that barrier simply marks the card dirty.
At the next young collection the collector scans only dirty cards and treats the young references it finds there as extra roots. G1 needs more because it collects arbitrary sets of regions, so every region has a remembered set listing the cards elsewhere that point into it.
G1's barrier filters out null and same-region stores and queues the dirty card, and concurrent refinement threads move those cards into remembered sets in the background, so the pause only scans what is left.
The costs are barrier work on every reference store, which is part of why Parallel has better raw throughput, memory for the remembered sets, refinement CPU, and longer Scan Heap Roots time when the application dirties cards faster than refinement can keep up.
A workload that keeps pointing old objects at new ones is the classic case: lots of dirty cards and lots of survivors to copy.
About this answer
- Last updated
- 12 November 2026
- 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 "A young collection must find young objects referenced from the old generation without...": https://scrivio-interview-library.netlify.app/java/garbage-collection/c9d26a5858/