跪拜 Guibai
← All articles
Android · Android Jetpack · Kotlin

Android Jank: Which Thread Actually Cost You the Frame?

By 稀有猿诉 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Misdiagnosing jank wastes hours: the fix for a UI-thread bottleneck (move code off main) is useless for a GPU-limited frame, and simplifying draw commands does nothing for a main-thread stall. This breakdown tells you which stage to profile and which change actually reduces dropped frames.

Summary

Android frames pass through three handoffs: the UI thread records display lists, RenderThread replays them and submits commands, and the GPU rasterizes before SurfaceFlinger composes. The same dropped-frame symptom can come from any one of these. A heavy computation in onDraw() starves RenderThread because no commands are recorded; a large Path or clipPath slows DrawFrame even though recording was cheap; and a saturated GPU delays rendering after both CPU threads are done.

Moving work to a background thread only helps the first case. The trace markers differ: Record View#draw() runs on the main thread, DrawFrame on RenderThread, and GPU completion is separate. Perfetto's FrameTimeline on Android 12+ compares expected vs actual display time to isolate the stage. For Compose, the pipeline stays the same, but state reads can be scoped inside a graphicsLayer lambda to avoid recomposition per frame.

The frame interval is 16.7 ms at 60Hz, 11.1 ms at 90Hz, and 8.3 ms at 120Hz. Because frames pipeline, that number is not a per-thread budget; measuring the whole frame lifecycle matters more than slicing thread segments.

Takeaways
— RenderThread is a native thread that replays display lists recorded by the UI thread; it does not run app UI code.
— In hardware-accelerated rendering, onDraw() only records commands; it does not rasterize.
— Large Path objects and clipPath can be rasterized on the CPU, making RenderThread slow even when recording was lightweight.
— Animating translationX skips layout, while animating margins forces layout every frame.
— syncFrameState blocks the main thread while RenderThread copies UI state; large bitmaps slow this synchronization.
— dequeueBuffer and eglSwapBuffers can block RenderThread waiting for the GPU or SurfaceFlinger.
— Compose state reads can be moved into a graphicsLayer lambda to avoid recomposition per frame.
— Frame intervals are 16.7 ms at 60Hz, 11.1 ms at 90Hz, and 8.3 ms at 120Hz.
— Perfetto's FrameTimeline on Android 12+ compares expected vs actual display time to identify late frames.
— Macrobenchmark's FrameTimingMetric and JankStats provide automatic frame timing without manual tracing.
Conclusions

Because onDraw() only records rather than rasterizes, moving heavy logic off the UI thread does nothing when the real cost is in RenderThread's expensive command replay or in GPU execution.

Thread waits like syncFrameState and buffer returns are intentional, so a long idle segment in a trace often means the thread is waiting on a later stage; a single isolated segment can mislead debugging.

Frame pipelining means a thread's processing time is not the same as the latency from touch to screen, so treating 16 ms as a per-thread budget is a category error.

Concepts & terms
RenderThread
A native Android thread that replays display lists recorded by the UI thread and submits rendering commands to the GPU. It does not run application UI code.
DisplayList (RenderNode)
A data structure that records Canvas drawing commands during onDraw(). In hardware-accelerated rendering, drawing is recorded into this list and later replayed, rather than executed immediately.
SurfaceFlinger
The system service that composites all app and system surfaces into the final frame presented to the display. Apps have no control over this last stage of frame processing.
FrameTimeline
A Perfetto feature on Android 12+ that compares the expected and actual display times of frames, helping identify whether a frame actually reached the screen on time.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗