Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Inside the JVM

    Generations: Most Objects Die Young

    The weak generational hypothesis: most objects die young — temporary strings, iterators, request objects, boxed numbers. A few live a long time — caches,

    Chapter 6 of 105 min readOften asked in interviews

    The Plain Picture#

    A hospital sees that most patients are in and out within an hour (a sprained wrist), while a few stay for months. So it has a fast-turnover casualty ward, checked every few minutes, and a separate long-stay ward, checked rarely. Checking the whole hospital every few minutes would be wasteful.

    The Real Names#

    • The weak generational hypothesis: most objects die young — temporary strings, iterators, request objects, boxed numbers. A few live a long time — caches, configuration, connection pools.

    • So the heap is split into generations:

      ┌──────────────────────── YOUNG GENERATION ───────────────────────┬────────── OLD GENERATION ──────────┐
      │   EDEN                        │  SURVIVOR S0  │  SURVIVOR S1    │   long-lived objects               │
      │   (new objects are born here) │  (one of these is always empty)  │   (promoted after surviving N GCs) │
      └───────────────────────────────┴───────────────┴─────────────────┴────────────────────────────────────┘
    • A minor GC (young collection) cleans only the young generation: it copies the few live objects from Eden and the occupied survivor space into the empty survivor space, adds 1 to each object's age, and then treats Eden and the old survivor space as empty in one step. Objects that survive enough minor GCs (the tenuring threshold, up to 15) are promoted to the old generation.

    • A major GC collects the old generation; a full GC collects everything. These are rarer and — depending on the collector — much more expensive.

    Under the Hood — measured#

    A program that creates 3 million Note objects, keeping only 1 in every 1,000, with a 256 MB heap and GC logging on (-Xlog:gc):

    Output
    [0.004s][info][gc] Using G1
    [0.043s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 24M->1M(256M) 0.730ms
    [0.062s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(256M) 0.646ms
    [0.098s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 153M->1M(256M) 0.977ms
    [0.126s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 153M->1M(256M) 0.664ms
    done, kept 3000 sum 34888890

    Read one line: Pause Young … 153M->1M(256M) 0.977ms = a young collection that found 153 MB of heap in use, left 1 MB afterwards, in a heap of 256 MB, pausing the application for under 1 millisecond. Almost everything was garbage, and copying only the survivors is cheap because there are so few — the cost of a young GC depends on the live objects, not the garbage. That's the whole reason generations work.

    (This is also Part 0's sawtooth: memory climbs as Eden fills, then drops at each young GC.)

    Gotchas

    • Premature promotion: if survivor spaces are too small or objects live just long enough (e.g. a request that takes 2 seconds while young GCs happen every 0.5 s), short-lived objects get promoted to the old generation, fill it, and cause expensive old-generation collections.

    • Allocation is cheap in Java — a new object in Eden is usually just a pointer bump in a per-thread chunk of Eden (a TLAB, thread-local allocation buffer). The cost comes from objects that survive.

    Check yourself

    5 questions
    1

    What's the weak generational hypothesis?

    Show answer

    Most objects die young; few live long.

    2

    What happens to Eden's live objects in a minor GC?

    Show answer

    They're copied to the empty survivor space (their age increases); old enough ones are promoted to the old generation. Eden is then reused as empty.

    3

    Why is a young GC fast even when Eden is full of garbage?

    Show answer

    Its cost is proportional to the live objects it copies, and almost everything in Eden is dead.

    4

    Read the line Pause Young (Normal) 76M->1M(256M) 0.646ms.

    Show answer

    A young collection: 76 MB in use before, 1 MB after, heap size 256 MB, the application paused for 0.646 ms.

    5

    What is promotion, and what's premature promotion?

    Show answer

    Moving objects that survived enough young GCs into the old generation; premature promotion is short-lived objects being promoted because they didn't die quickly enough, bloating the old generation.