Garbage collectionhard5-8 years

G1's young collections only scan eden and survivor spaces, not the whole heap — that's how they stay fast. But an object in eden can be kept alive by a reference sitting in the old generation. How does a 'partial' collection find that reference without scanning the old generation on every single pause?

It doesn't search for the reference at collection time at all — it keeps a running record of it as the reference is created, so the young collection only has to consult a small, pre-built index rather than searching anything. Each old-generation region maintains a remembered set (RSet): a summary of every reference from outside that region pointing into it. Whenever application code writes a reference that crosses a region boundary — an old-generation object's field starting to point at something in eden — a write barrier catches that store and records it, cheaply, as a 'dirty card' in a card table rather than updating the RSet synchronously and expensively on every write. A background thread later drains those dirty cards into the actual RSets. So a young collection's job is: scan eden and survivor, plus consult each region's already-built RSet — never search the old generation itself.

The lesson behind it →
More on Garbage collection