Performance, under the hood · Puzzle 4 · 5 of 6
What the collector costs
"The GC paused my app" was true on old Android. Modern ART collectors barely pause. Here's how ART allocates and collects, and the way it can still cost you frames.
The problem
Your list allocates a few new objects per row: a StringBuilder for the title, a formatted date, a list for the tags. Someone on your team says "allocations cause GC pauses, and GC pauses cause jank". Is that still true? What does the collector actually do while you scroll?
See it
Each thread allocates from its own TLAB: a new object moves a pointer forward. No lock, no search.
Step 1 of 4Concurrent Copying copies the objects still alive into fresh space while your threads keep running. One small pause per collection.
Step 2 of 4Most objects die young, so a young GC looks only at new ones: cheap and frequent. Predict: when does this still cost a frame?
Step 3 of 4With allocation churn: allocate faster than the collector frees, and your main thread must wait for it.
Step 4 of 4
The building block
Start with allocating. With ART's default collector, each thread gets its own TLAB, a thread-local slice of the heap. Allocating an object there is a pointer bump: move a marker forward by the object's size. No lock, no searching for space. Creating an object is cheap.
Collecting is the garbage collector's job: find the objects nothing can reach any more, and free their memory. Since Android 8, ART's default has been Concurrent Copying (newer devices may run its successor, Concurrent Mark-Compact, with the same low-pause idea): it copies the objects that are still alive into fresh space while your threads keep running, so live objects end up packed together, with no gaps, without stopping your app. There's one small pause per collection, and it doesn't grow with the size of the heap: ART's own docs show an average of about 2 ms.
Since Android 10 it's also generational. Most objects die young, like that StringBuilder, so ART runs a young GC that looks only at recently created objects: little work, run often. It switches to a full-heap collection only when young ones stop freeing enough.
So where does it hurt? Not mostly in the pause. Allocation churn makes collections frequent, and the collector's threads need CPUs too; your main thread may find them taken (Runnable, puzzle 2). And if your app allocates faster than the collector can free, your thread must wait for it, and that wait can cost a frame.
The analogy
A café where every waiter has their own stack of clean cups (the TLAB): taking one is instant. A dishwasher collects dirty cups while service goes on (concurrent), mostly from the tables just cleared (young). Service only slows when cups are dirtied faster than he can wash them, and then a waiter stands at the counter, waiting.
Check yourself
Sources
- Android Open Source Project: Debug ART garbage collection ("Starting with Android 8 (Oreo), the default plan is Concurrent Copying (CC)"; "This allocates a thread-local allocation buffer (TLAB) to each app thread, which can then allocate objects out of its TLAB by bumping the 'top' pointer, without any synchronization"; "CC performs heap defragmentation by concurrently copying objects without pausing app threads"; "GC only has one small pause, which is constant in time with regards to the heap size"; "CC extends to be a generational GC in Android 10 and higher. It enables collecting young objects, which often become unreachable fairly quickly"; "bad mutator behavior, such as excessive allocation, can still cause performance problems"; "young concurrent copying paused: … Avg: 1.830ms")
- Android Studio: Capture a heap dump ("if your app allocates memory faster than the system can collect it, your app might be delayed while the collector frees enough memory to satisfy your allocations. The delay could cause your app to skip frames")
What you now know
Each thread allocates from its own TLAB by bumping a pointer, with no lock. ART's Concurrent Copying collector moves live objects while your threads keep running, with one small pause that doesn't grow with the heap, and since Android 10 it mostly runs cheap young GCs. What still costs frames is allocation churn: the collector's threads compete for CPUs, and an app that allocates faster than they free must wait.
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.