Performance, under the hood · Puzzle 5 · 6 of 6

What a heap dump proves

Puzzle 4's collector frees everything it can't reach. So how does memory get stuck in a garbage-collected app? Here's what a snapshot of memory records, the two sizes it shows, and the chain that proves memory is stuck.

The problem

After rotating the phone ten times, your app uses 80 MB more than at start. The garbage collector frees everything that's unreachable, so whatever is left must still be reachable. From where? And which object, among millions, is the one to fix?

See it

A GC root, a static field, refers to a listener, which refers to an old destroyed Activity, which retains its screen and bitmaps; shallow size is the Activity alone, retained size is the whole group; cutting the listener's reference frees all of it GC rootlistener old Activity: 400 Bscreenbitmapsretained: 12 MB reachable, so never freed cut one link12 MB freed
  1. A GC root, a static field, refers to a listener. Everything reachable from a root stays in memory.

    Step 1 of 4
  2. The old Activity, destroyed on rotation: shallow size 400 bytes. With its screen and bitmaps, its retained size is 12 MB.

    Step 2 of 4
  3. The listener still refers to it, so it's reachable, and the collector must keep it: a leak. Predict: what frees the 12 MB?

    Step 3 of 4
  4. Cutting one link: unregister the listener when the Activity is destroyed. The heap dump shows which link.

    Step 4 of 4

The building block

A heap dump is a snapshot of every object in your app's heap, with every reference between them. Android Studio lists them by class with two sizes:

  • Shallow size: the memory one object takes by itself, its own fields. An Activity is a few hundred bytes.
  • Retained size: the memory that would be freed if this object were freed: itself, plus everything only it keeps alive. That same Activity can retain megabytes: its screen and its Compose state. (Bitmap pixels live in native memory, shown separately as Native Size.)

The collector (puzzle 4) starts from GC roots, the references it always keeps: local variables of running threads, static fields, and the like. Whatever it reaches stays; the rest is freed. So a leak in a garbage-collected app is an object you're finished with that's still reachable from a root.

The classic case: on rotation, your Activity is destroyed and a new one created (the Android course, module 4). If a long-lived object, such as a listener registered in a companion object or a singleton, still refers to the old Activity, the old Activity stays reachable, and everything it retains stays too. Ten rotations, ten old Activities.

The heap dump proves it. Android Studio's "Show activity/fragment leaks" filter finds destroyed Activities still in memory. For each instance it shows its depth, the shortest number of references from a GC root, and the reference chain. Cut one link in that chain, usually by unregistering the listener when the Activity is destroyed, and the whole retained size is freed.

The analogy

A hotel cleans every room whose guest has checked out (the collector). A guest who left but whose name is still on a booking (a reference from a root) keeps the room, and everything in it, forever. The booking list is the chain: cancel that one booking, and the whole room is freed.

Check yourself

An old Activity has a shallow size of 400 bytes and a retained size of 12 MB. What does freeing it save?

Right. Retained size is what you really get back.

Not quite. A destroyed Activity is collected like any object, once unreachable.

Not quite. That's shallow size; retained size counts everything that goes with it.

The GC frees whatever it can't reach. How can an app leak?

Right. Reachable but unneeded.

Not quite. The thread that created an object doesn't matter; only whether it's reachable.

Not quite. Size doesn't matter to the GC; reachability does.

What's the difference between shallow size and retained size?

Right. One object vs what goes with it.

Not quite. That's the two swapped. Re-read that part ↑

Sources

  • Android Studio: Capture a heap dump ("Shallow Size: Size of this instance in Java memory. Retained Size: Size of memory that this instance dominates"; "Depth: The shortest number of hops from any GC root to the selected instance"; "Show activity/fragment leaks: Shows classes that are causing memory leaks"; "leaking the host Activity will retain the entire Compose hierarchy and all active state trees in memory"; "Long-lived references to Activity or Context")
  • Android Developers: Overview of memory management ("Garbage collection has two goals: find data objects in a program that cannot be accessed in the future; and reclaim the resources used by those objects.")

What you now know

A heap dump is a snapshot of every object and who refers to whom. Shallow size is one object alone; retained size is everything that would be freed with it. A leak is an object you're done with that's still reachable from a GC root, often a destroyed Activity held by something long-lived. The path from the root shows exactly which reference to cut.

That's everything published so far

You've reached the last puzzle of Performance, under the hood. Next: Module 2: Measuring for real, about macrobenchmark and why one run proves nothing; baseline profiles and what ART compiles ahead of time; startup traced end to end; reading a Perfetto trace with SQL.

New puzzles are announced on LinkedIn and in the learning log feed.

All courses →