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
Objects live in the heap. Variables don't hold them; they hold references, arrows pointing at them.
Step 1 of 4Some references are always in use: a running function's local variables, static fields like a singleton. These are the GC roots.
Step 2 of 4Follow 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 4No 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
Sources
- 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.")
- Android Studio: Capture a heap dump ("Depth: The shortest number of hops from any GC root to the selected instance.")
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.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.