Generational Memory: An Intuition
Imagine you're cleaning your room. You have a pile of things you use every day — phone, keys, water bottle — and a pile of things you haven't touched in months — old notebooks, a broken charger, a souvenir from a trip. When you decide to throw things away, which pile do you look at first? The old one, obviously. The things you just used are probably still useful.
That's the core idea behind generational memory. It's a strategy for managing memory in computer programs (especially garbage collectors) that exploits a simple observation: most objects die young. A newly created object is very likely to become garbage quickly. An object that has survived for a while is likely to keep surviving.
So instead of treating all memory as one big pile, you split it into generations — typically a young generation and an old generation. New objects are born in the young generation. You clean (collect) the young generation frequently, because that's where most garbage is. You clean the old generation rarely, because objects there tend to stick around.
This is not a law of physics — it's a heuristic called the weak generational hypothesis. It holds true for most programs, which is why nearly every modern garbage collector (Java's G1, .NET's GC, Python's generational GC) uses it.
The Precise Statement
Generational memory is a memory management scheme that divides the heap into two or more regions (generations) based on object age. The collector applies different collection policies to each generation:
- Young generation: Collected frequently using a fast, stop-the-world algorithm (often a copying collector). Objects that survive a few collections are promoted (moved) to the old generation.
- Old generation: Collected infrequently, using a slower, more space-efficient algorithm (often mark-sweep or mark-compact). Only objects that have survived multiple young collections end up here.
The key insight is that by focusing effort on the young generation, the collector reclaims most garbage with minimal work. The old generation, which contains few dead objects, is scanned rarely — saving time.
Total GC work≈(frequency×young size)+(rare×old size)
Since most garbage is in the young generation, the first term dominates — but it's cheap because the young generation is small.
Why It Works (and When It Doesn't)
The weak generational hypothesis is an empirical observation, not a theorem. It works because:
- Temporary objects (loop variables, intermediate results, short-lived data structures) are created and discarded rapidly.
- Long-lived objects (caches, singletons, configuration data) tend to stay alive for the entire program. …