Java / Java 17 Garbage Collection Interview Questions
What is a card table and how does it help garbage collection?
A card table divides the heap into small, fixed-size chunks called cards (512 bytes each). Whenever application code writes a reference field, a JIT-inserted write barrier marks the corresponding card as "dirty" in a compact byte array.
During a young collection, the GC needs to find old-to-young references so it doesn't miss objects only reachable from the old generation - without a card table it would have to scan the entire old generation for such references. Instead, it only needs to re-scan the small number of dirty cards, since any potentially new cross-generational reference must have gone through a write barrier that dirtied a card.
This is the underlying mechanism that remembered sets are typically built on top of in G1 and other generational collectors.
More Related questions...