skip to content

Why is a mature-area collection so much more expensive per byte reclaimed than a young-area collection of the same size?

level: middleimportance: must knowfreq 55%

answer

  1. work per byte reclaimed
  2. the dead are never visited
  3. garbage density decides where to collect
  4. the mature area is mostly live
  5. the expensive collection is deferred, not removed

basics

~20 s

Survival rate decides it. A tracing collector pays for live objects, not for garbage, so a young area that is a few percent live reclaims almost all of itself for very little work, while a mature area that is mostly live traces a great deal to reclaim little.

solid answer

~40 s

The useful unit is **work per byte reclaimed**. A tracing collector visits reachable objects and never visits the dead individually, so its cost tracks the live set while its payoff tracks the garbage. The young area is, by the generational hypothesis, mostly garbage when it is collected: a few percent survives, that few percent is traced and relocated, and the rest of the space comes back wholesale. The mature area is the inverse — it holds what has already proved long-lived, so most of it is live, and the collector traces nearly all of it to recover a small remainder. Same region size, very different bills. That asymmetry, not object size or memory hardware, is why one area is collected constantly and the other as seldom as possible.

go deeper

for a junior

Remember the direction: the young area is cheap to collect because almost everything in it is garbage, and the older area is expensive because almost everything in it is still in use.

for a middle

Explain the mechanism in terms of work per byte reclaimed, and be able to say why dead objects cost a tracing collector nothing individually while live ones cost it a visit and often a copy.

for a senior

Bring numbers. Estimate survival rates for a real service, show how they set the ratio between the two collections, and say what promotion does to the mature area's growth over a day.

for a principal

Treat the asymmetry as the budget line it is. Decide how much footprint to buy so mature collections stay rare, and be explicit that the work is deferred rather than avoided.

## The unit that matters Compare collections by **work per byte reclaimed**, not by region size or by pause length alone. That unit exposes the whole argument for generations in one number. A tracing collector establishes liveness by reachability: it starts at roots, follows references, and touches each reachable object once. Dead objects are **never enumerated**. Their space is recovered in bulk once the survivors have been accounted for. So: - **cost** is driven by the bytes and references the collector must trace and, where survivors are relocated, copy; - **payoff** is driven by the bytes that turn out to be dead, which cost nothing individually. The ratio between the two is therefore governed almost entirely by the region's **survival rate**. ## A worked comparison Take a young area of 512 MB that is 2% live when collected, and a mature area of 4 GB that is 90% live. | | young area | mature area | |---|---|---| | region size | 512 MB | 4 GB | | live when collected | ~10 MB | ~3.6 GB | | reclaimed | ~502 MB | ~400 MB | | traced per byte reclaimed | ~0.02 bytes | ~9 bytes | The mature collection does roughly 450 times as much work for each byte it returns — more than two orders of magnitude — even though the two collections recover comparable amounts of space. Halve the sizes or change the survival percentages and the numbers move, but the shape does not: the ratio is set by how much of the region is live. ## Why the mature area is mostly live This is not an accident of layout; it is what promotion selects for: - an object only reaches the mature area by surviving several young collections, which is evidence that it is long-lived; - long-lived objects are, by the hypothesis, the small population that does not die quickly; - so the mature area accumulates exactly the objects least likely to be garbage at any moment. The area therefore has low garbage density by construction. It is also the area that grows: the live set a long-running program retains lives there, and the cost of tracing it grows with that live set. ## Three explanations that are not the reason - **Object size.** Mature objects are not systematically bigger, and a mature area full of small objects is just as expensive when most of them are live. - **Where the memory sits.** Both areas are ordinary heap memory. Locality differences exist, but they are second-order beside a survival rate of 2% against 90%. - **Algorithm choice.** The area's algorithm is chosen *because* of the survival rate, not the other way round: an approach that pays per survivor suits a region with few of them, and a region that is mostly live wants one that does not copy everything. ## What follows for scheduling 1. **Collect the young area whenever it fills.** Each such collection is cheap, and waiting longer only lets the area grow. 2. **Collect the mature area as seldom as the footprint budget allows.** Each one is expensive in both work and pause, and running it more often does not make it cheaper — the live set is the same. 3. **Treat promotion as a cost, not a cleanup.** Every promoted object moves out of the cheap area and into the expensive one, where its eventual death can only be noticed by a mature collection. ## The limits of the argument The asymmetry is a consequence of the hypothesis, so it fades exactly when the hypothesis does. If a workload's young area routinely survives at, say, 40%, the young collection's work per byte reclaimed climbs towards the mature area's, the promotion rate rises, and the split's advantage narrows to the difference in region sizes. It is also worth being precise about what is deferred. Deferring mature-area collection does not reduce the total work a program's garbage eventually requires; it reduces how often that work is paid and keeps the frequent collections small. The bill still arrives, which is why footprint has to be sized for the live set that accumulates between mature collections. ## What an interviewer is listening for That you reach for survival rate rather than size or hardware; that you can state the rule that a tracing collector pays for the live and not the dead; and that you say plainly that promoted objects are traced again whenever the mature area is collected.

  • If the young area is collected far more often, why does total collection work not simply rise with the frequency?
    Because each young collection pays only for its survivors, and the fraction that survives stays small as long as the collection interval comfortably exceeds ordinary object lifetimes. Shrink the interval far below those lifetimes and the fraction does climb, and total work with it — frequency is cheap only while survival stays low.
  • Does the cost of a mature-area collection grow with the heap size or with the live set?
    Tracing cost grows with the live set, since that is what the collector visits. Depending on the reclamation step, some work can also scale with the size of the region being swept or compacted. Sizing a service therefore means watching the retained live set, not the configured maximum.

The bill for moving house is set by what you carry to the van, not by the junk you leave at the kerb for collection. A young-area collection is charged for the few survivors it carries out; the dead cost nothing to leave behind.

saying these in an interview costs you the question

  • Says a collection's cost depends on how much garbage it reclaims
  • Explains the mature area's cost by claiming its objects are larger
  • Thinks a promoted object is never traced again
  • Believes a young-area collection also reclaims dead objects in the mature area
  • Claims a whole-heap collection costs about the same as a young one of equal size