Performance, under the hood · Puzzle 7 · 8 of 10

What garbage is

In Kotlin you create objects and never free them. Something else decides when an object's memory can be reused. Here's the rule it follows, which is the rule behind both memory puzzles that come next.

The problem

In onBind() you write val title = StringBuilder(), use it, and return. You never free it, and Kotlin has no way to. Yet your app doesn't run out of memory after a million rows. Who decides that the StringBuilder's memory can be reused, and how can they be sure nobody needs it any more?

See it

Two GC roots, a local variable and a singleton, point at objects in the heap; objects point at other objects; the collector follows every chain from the roots; objects no chain reaches are garbage and their memory is freed heapStringBuilderListActivityold User local: titlesingletonGC roots reachable: kept garbageno chain leads here
  1. Objects live in the heap. Variables don't hold them; they hold references, arrows pointing at them.

    Step 1 of 4
  2. Some references are always in use: a running function's local variables, static fields like a singleton. These are the GC roots.

    Step 2 of 4
  3. Follow the arrows from the roots: every object you reach is reachable, so some code could still use it. Predict: what about the old User?

    Step 3 of 4
  4. No chain from any root leads to it. It's garbage, and the garbage collector frees its memory.

    Step 4 of 4

The building block

Every object you create lives in the heap, the part of your app's memory where objects are kept. A variable doesn't hold the object itself; it holds a reference to it, an arrow pointing at where the object is. Objects can hold references too: a User can refer to its Address.

Some references are always in use: the local variables of functions that threads are running right now, and static fields, like a property of a companion object. These are the GC roots.

An object is reachable if you can start at a root and follow references, one arrow after another, to it. Reachable means some code could still use it. If no chain from any root leads to an object, no code can ever get to it again. That object is garbage: its memory can be reused, safely.

The garbage collector (GC for short) is the part of ART that does this: it follows every chain from the roots, keeps whatever it reaches, and frees the rest. You don't call it; ART runs it when memory is needed.

Back to onBind(): while it runs, title is a local variable, a root, so the StringBuilder is reachable. When onBind() returns, that variable is gone. Unless you stored the reference somewhere else, nothing leads to the StringBuilder any more: it's garbage. But if you'd added it to a list held by a singleton, it would stay reachable, and stay in memory, for as long as that list does.

The analogy

A museum. The entrances are the roots; corridors between rooms are references. A room you can walk to from an entrance stays open. A room no corridor leads to any more can never get a visitor again, so the museum can clear it and reuse the space. The garbage collector is the guard who walks every corridor from every entrance and marks which rooms to clear.

Check yourself

A function creates a User and keeps it only in a local variable. After the function returns, what is the User?

Not quite. Who created it doesn't matter; what matters is whether a chain from a root still leads to it.

Right. No chain from a root means garbage.

Not quite. It becomes garbage then; the collector frees it later, when it runs.

A singleton holds a list, and the list holds your Activity. Can the collector free the Activity?

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

Not quite. Being off screen doesn't matter; the chain from the root keeps it reachable.

Right. Reachable from a root means kept.

What's the difference between a reference and an object?

Not quite. That's the two swapped.

Right. The thing vs the arrow to it.

Sources

What you now know

Objects live in the heap; variables and fields hold references to them, not the objects themselves. GC roots are the references that are always in use: running threads' local variables and static fields. An object is reachable if a chain of references leads to it from a root; if none does, nothing can ever use it again, and it's garbage. The garbage collector finds the garbage and frees its memory.