Performance, under the hood · Start here · 1 of 6

A photo or a diary

Android Studio offers to take snapshots of what your app is running, or to record every method. They answer different questions, and one of them changes the timings it measures. Here's what each actually records.

The problem

Scrolling your list feels slow. You open the profiler and it asks you to choose: take snapshots of what's running, or record every Java/Kotlin method. Both draw charts that look alike. Which one tells the truth about where the time went, and what does each cost your app while it runs?

See it

A thread's call stack changes over time; the kernel takes a snapshot thousands of times per second of CPU time and counts them; method tracing instead writes a timestamp at every call's start and end, which slows the thread; a trace section marks only one named span call stack over time onBind() parseJson() draw() a sample every N CPU cyclesparseJson ×4draw ×2missed a timestamp at every callexact, but slower trace("parse") { }only the span you name CPU
  1. A thread's call stack over time: parseJson() runs for a while, with a tiny call inside it, then draw().

    Step 1 of 4
  2. Sampling: thousands of times per second of CPU time, the kernel snapshots the stack. Count the snapshots: most land in parsing. The tiny call fell between two and was missed.

    Step 2 of 4
  3. Method tracing: instrumentation writes a timestamp at every call's start and end. Exact counts, but the thread now does extra work. Predict: what does that do to the timings?

    Step 3 of 4
  4. It inflates them, most where calls are many and small. A trace section records only the span you name.

    Step 4 of 4

The building block

Every thread has a call stack: the chain of calls it's inside right now, from the first caller down to the function that's running.

Sampling doesn't touch your code. The profiler asks the kernel, and the kernel asks the CPU's counters: each time a given number of CPU cycles pass (by default about 4,000 times per second of running), the kernel interrupts the thread and writes down its call stack. That snapshot is a sample. Thousands of them later, a function that shows up in 30% of the samples took about 30% of the time. It's cheap, and it sees native code and the runtime too. It only samples a thread while it's running on a CPU, so time spent waiting is invisible. But it's statistics: a call that starts and ends between two snapshots may never appear, and it can't tell you how many times something was called.

Method tracing works by instrumentation: ART injects a timestamp at the entry and exit of every method. Now you get exact calls and exact counts. The price: that extra code runs on every call, so a function made of many tiny calls looks slower than it is. Android's docs say to keep these recordings to five seconds or less, and warn that timings may differ from production.

The third tool is yours: a trace section. Wrap a span in trace("loadAndProcessData") { } and it appears, named, when you record a system trace (in the profiler, or with Perfetto). You pay only where you put one.

SamplingMethod tracingTrace section
Recordsa call stack every N eventsevery method's start and endspans you name
Exact countsnoyesyes, for your spans
Slows the appbarelya lotbarely

The analogy

Sampling is a photographer who snaps the kitchen every minute: from a day of photos you know the cook spent most of it at the stove, but not how many eggs were cracked. Method tracing is a cook who writes in a diary before and after every move: complete, but the cooking gets slower. A trace section is a timer the cook starts only for the soup.

Check yourself

A function runs for 50 µs, once, between two snapshots. Which tool can miss it entirely?

Not quite. A trace section records whatever span you wrap; the risk of missing it is sampling's.

Not quite. Instrumentation records every method's start and end, however short.

Right. A snapshot misses what started and ended between two snapshots.

Your parser makes a million tiny calls. Method tracing says it takes 900 ms; sampling says 300 ms. Which do you trust for the time, and why?

Not quite. They can; they disagree because one of them slowed the code down.

Not quite. Sampling estimates, but without slowing the code; the trace paid its own overhead a million times.

Right. Instrumentation costs most where calls are many and small.

What's the difference between a sample and a trace section?

Right. Who decides what's recorded: the kernel's clock, or you.

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

Sources

  • simpleperf: How simpleperf works ("The kernel enables counters while the profiled processes are running. Each time a given number of events happen, the kernel dumps a sample to the mapped buffers.")
  • Android Studio: Record Java/Kotlin methods ("the Android Studio Profiler uses runtime instrumentation to inject timestamps at the entry and exit points of each method call"; "We recommend limiting recordings to five seconds or less to avoid performance overhead from runtime instrumentation"; "The timing information from tracing might deviate from production due to the overhead introduced by the instrumentation itself.")
  • Android Studio: Sample the callstack ("the callstack usually includes not just the Java/Kotlin code but also the native code required to run the program itself"; "Internally, Android Studio uses simpleperf")
  • Android Developers: Define custom events ("Jetpack provides a tracing API that you can use to label a particular section of code"; "The trace function automatically ends the trace when the lambda completes.")

What you now know

Sampling asks the kernel to snapshot a thread's call stack every so many CPU events; counting the snapshots shows where time goes, cheaply, but short calls can be missed. Method tracing injects a timestamp at every method's start and end: exact counts, but the instrumentation slows the app and skews the timings. Trace sections mark only the spans you choose.