TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About · Labs · Cheat sheets · Learning paths

All concepts · java · jvm · memory

Garbage collection

How the JVM frees memory for you. It finds objects that can no longer be reached from any variable and reclaims them, collecting short-lived objects often and long-lived ones rarely.

10 min3 animations

🖨️ Cheat sheet

Toys on strings

In this playroom, every toy in use is tied by a string to the toy box rope, either directly or through another toy. The teddy is tied to the rope, and its hat is tied to the teddy.

The cleaner follows every string from the rope and puts a sticker on each toy it reaches. Anything without a sticker can't be reached by anyone, so it's thrown away. Nobody has to say "I'm done with this".

Reachability

Java has no free(). The garbage collector reclaims objects that are unreachable: not referenced, directly or through other objects, from any GC root. Roots include local variables on every thread's stack, static fields, and JNI references.

Collection starts by marking everything reachable from the roots; unmarked memory is reclaimed. Cycles aren't a problem: two objects pointing at each other but unreachable from a root are both collected.

A memory leak in Java is an object you no longer need but that is still reachable, like entries in a static map that nobody removes.

What can still be reached?5 steps
🪢Toy box rope🧸Teddy🎩Teddy's hat🎈Old balloon🧹Cleaner
Press play and watch it happen!

The just-used box

Most toys are played with once and forgotten. So new toys go into a just-used box, which the cleaner tidies often, and quickly, because almost everything there is already forgotten. Toys that survive several tidies move to the favourites shelf, which is tidied rarely.

Generations

The generational hypothesis: most objects die young. So the heap is split:

  • Young generation: new objects are allocated in eden. Minor GCs collect it often; survivors are copied to survivor spaces, and after enough GCs, promoted.
  • Old generation: long-lived objects, collected less often by major (or G1's mixed) collections.

Copying the few survivors is cheaper than scanning all the garbage, so minor GCs are fast.

New toys and old favourites4 steps
Just-used box01234Favourites shelf0123🏠Playroom🧹Cleaner
Press play and watch it happen!

Freezing while tidying

The easiest way to tidy is "everybody freeze!", but nobody can play while that happens. Modern cleaners tidy while everyone keeps playing, and only ask for a freeze of a blink.

Pauses and collectors

Stop-the-world pauses halt every application thread, which shows up as latency spikes.

CollectorAims for
G1 (default)A balance, with a pause-time target (-XX:MaxGCPauseMillis=200)
ZGC / ShenandoahPauses under a millisecond, even on huge heaps
ParallelMaximum throughput, for batch jobs

Watch GC with -Xlog:gc or JDK Flight Recorder, and size the heap with -Xmx.

Stop and tidy, or tidy while playing3 steps
🧒Player 1👧Player 2🧹Cleaner
Press play and watch it happen!

🧩 Quick quiz

Quick quiz! 3 questions about Garbage collection, told the same way as the story.
  1. Which toys does the cleaner throw away?
  2. Why does the cleaner tidy the just-used box often?
  3. What do modern cleaners do differently?
Answer every question to check.Passing marks Garbage collection as done.
Got the hang of Garbage collection? Mark it as done, and your paths show how far you’ve got.

Saved in this browser.

☕ Java concurrencyStep 5 of 5

← Previous on the path🤵 Virtual threadsLast step 🎉Back to the path

⚙️ Java under the hoodStep 3 of 5

← Previous on the path🍪 Java StreamsNext on the path →🛠️ JVM memory: heap, stack and metaspace

👉 Read next

  • 🤵Virtual threadsA few waiters serving hundreds of tables, pinning each order to the pass instead of waiting beside one table.
  • 🛠️JVM memory: heap, stack and metaspaceA workshop: each worker stacks their job at their own bench, and finished pieces go to one shared storeroom.