Generational GC
Generational GC splits the heap by object age. Most objects die young, so the collector spends most of its time on a small young generation and promotes only survivors to the old generation.
Detailed Explanation
The weak generational hypothesis says the majority of objects become unreachable quickly. Scanning the whole heap for every collection would waste work on long-lived objects. HotSpot therefore allocates into Eden. A minor collection copies live Eden objects into a survivor space. Objects that survive several minor collections are promoted to the old generation. Major or mixed collections then reclaim the old generation.
G1 (the default) still uses generations: young regions and old regions on a heap divided into equal-sized regions. It collects young regions often and incrementally evacuates old regions to meet a pause goal. ZGC became generational in later JDKs (generational ZGC) for the same hypothesis with much shorter pauses.
Why you care: a cache that promotes everything to old gen turns a cheap minor GC world into an expensive old-gen problem. A burst of short-lived objects stays in Eden and is cheap if the live set at each minor GC is small. The live set at collection time, not the allocation rate alone, sets pause work for copying collectors.
You reach for this model when reading GC logs (Pause Young, Mixed, Full). You size young gen (-Xmn, G1 region counts) only after you know allocation rate and survivor rate. You do not set every -XX flag you found on a blog.
The cost of a too-small young gen is frequent minor GCs and premature promotion. The cost of a too-large young gen is longer minor pauses (more to copy). The cost of a huge tenuring threshold can be copying the same object many times. Defaults are reasonable; change one variable and measure.
Remember: promotion is not a leak. Promotion plus never becoming unreachable is a leak.
Code Examples
public class YoungJunk {
public static void main(String[] args) {
long sum = 0;
for (int i = 0; i < 100_000; i++) {
byte[] tmp = new byte[256]; // dies at the end of the iteration
sum += tmp.length;
}
System.out.println(sum); // prints: 25600000
// These arrays are typical young-gen garbage.
}
}import java.util.ArrayList;
import java.util.List;
public class Promote {
static final List<byte[]> LIVE = new ArrayList<>();
public static void main(String[] args) {
LIVE.add(new byte[1024]); // static list: survives and will promote
System.out.println(LIVE.size()); // prints: 1
}
}Common Gotchas
Caches in static maps promote and stay
that is old-gen pressure, not a young-gen issue.
Full GC is the expensive fallback
frequent full GCs mean the heap or the live set is wrong for the collector.
Treating promotion as a leak
long-lived objects belong in old gen.