Fundamentals 4 min read

How Card Tables Optimize Cross‑Generational GCRoot Scanning in Java HotSpot

This article explains how Java HotSpot uses a card‑table memory set to efficiently detect and handle cross‑generational references during young‑generation GCRoot scans, avoiding full old‑generation scans and significantly improving garbage‑collection performance.

Lobster Programming
Lobster Programming
Lobster Programming
How Card Tables Optimize Cross‑Generational GCRoot Scanning in Java HotSpot

During young‑generation GCRoot scanning, objects that are only referenced from the old generation (e.g., object D) may be missed, leading to potential errors if they are incorrectly reclaimed.

To avoid scanning the entire old generation, a Remember Set (memory set) can be introduced in the young generation to record pointers from the old generation to the young generation.

The Remember Set is implemented in HotSpot as a "card table" (CardTable), a byte array where each entry corresponds to a fixed‑size memory region (a card page, typically 512 bytes). When a cross‑generational reference appears, the corresponding card entry is set to 1.

During young‑generation GCRoot scanning, the GC queries the card table to find cards marked with 1, indicating the presence of cross‑generational references, and includes the referenced objects in the GCRoot set.

By using the card table, the GC avoids scanning the entire old generation; even if the old generation holds thousands of live objects, only a small subset of cards need to be examined, greatly improving collection efficiency.

HotSpot maintains the card table via write barriers: when the old generation references a young‑generation object, the corresponding card entry is set to 1.

JavaGarbage CollectionHotSpotCard TableCross-Generational References
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.