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

What the collector costs

"Freeing memory freezes my app" was true on old Android. Modern ART collectors barely stop it. 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. Puzzle 7 showed that these become garbage once nothing reaches them. Someone on your team says the garbage collector "freezes the app when you allocate, and freezes cause jank". Is that still true? What does the collector actually do while you scroll?

See it

Each thread allocates from its own TLAB by moving a pointer; the collector copies live objects to fresh space while threads run; young collections look only at new objects; when allocation outpaces collection the main thread waits main thread TLAB: bump a pointer Concurrent Copyinglive objects copied young GC: new objects only memory fills faster than freedmain thread waits
  1. Each thread allocates from its own TLAB: a new object moves a pointer forward. No lock, no search.

    Step 1 of 4
  2. Concurrent Copying copies the objects still alive into fresh space while your threads keep running. One small pause per collection: a moment when your threads stop.

    Step 2 of 4
  3. Most 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 4
  4. With 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 piece 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 (puzzle 7): find the garbage, and free its 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, a moment when the collector stops your threads, 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 also sorts objects by age, young and old. 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 4). 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

Why is creating an object in ART cheap?

Not quite. It doesn't; allocation is fast because the space is the thread's own and contiguous.

Right. Own space, one pointer bump.

Not quite. No search, no shared list: the thread moves its own marker in its own TLAB.

Concurrent Copying runs while you scroll. What does its pause depend on?

Not quite. It has one small pause per collection.

Not quite. Copying happens concurrently, while your threads run; the pause stays small.

Right. Small and constant, whatever the heap.

What's the difference between a young GC and a full-heap GC?

Right. Recent objects only vs everything.

Not quite. That's the two swapped.

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.