Sub topic 6
Telling UI-Thread Jank from Raster-Thread Jank
The two kinds of frame work
This chapter assumes that you can run a Flutter app in profile mode and use the DevTools Performance view. You should also know the purpose of build() and basic widget layout.
Flutter produces each frame in stages. The UI thread runs Dart code. It builds widgets, calculates layout, paints widgets into a scene, and sends that scene to the raster thread. The raster thread turns the scene into pixels using the GPU.
A frame can be slow on either thread:
- UI-thread jank happens when Dart or framework work takes too long. Examples include expensive calculations in
build(), rebuilding too many widgets, or complicated layout. - Raster-thread jank happens when drawing the scene takes too long. Examples include large images, expensive clipping, shadows, opacity layers, or shader compilation.
The display cannot show the next frame until the required work finishes. At 60 Hz, a frame has about 16.7 milliseconds. At 120 Hz, it has about 8.3 milliseconds. Missing that deadline makes motion look uneven. This is called jank.
Reading the frames chart
Open DevTools while the app runs in profile mode. In the Performance view, interact with the app until you reproduce the slow motion. The Flutter frames chart shows individual frame bars and separate timing information for UI and raster work.
Select a slow frame. Compare the UI and raster durations:
| Chart result | Likely bottleneck | First place to investigate |
|---|---|---|
| UI duration exceeds the frame budget | UI-thread jank | Dart callbacks, builds, layout, and parsing |
| Raster duration exceeds the frame budget | Raster-thread jank | Images, layers, effects, shaders, and GPU drawing |
| Both durations are high | Multiple bottlenecks or cascading work | The selected frame's timeline and Frame Analysis |
A tall UI bar does not necessarily mean the GPU is slow. It means the UI thread did not prepare the scene quickly enough. Likewise, a tall raster bar does not prove that a widget rebuilt too often. It means the prepared scene was expensive to draw.
A practical diagnosis
- Reproduce the problem in profile mode on a representative device.
- Select a consistently slow frame instead of an isolated startup frame.
- Check whether the UI or raster portion crosses the frame budget.
- Open Frame Analysis for suggestions about the selected frame.
- Inspect Timeline Events to find the longest event.
- Change only the suspected cause, then measure the same interaction again.
For UI-thread jank, look for long Dart callbacks, repeated builds, synchronous JSON parsing, sorting, or work inside build(). Move one-time or expensive computation out of build(). Split large widgets so that unrelated parts do not rebuild. Move CPU-heavy independent work to an isolate when appropriate.
For raster-thread jank, inspect large images and effects. Resize images before displaying them, reduce unnecessary opacity and clipping, and simplify shadows or complex custom painting. Test on the slowest device you support because a desktop GPU can hide raster problems.
Recognizing misleading evidence
A slow first frame can include initialization, shader compilation, image decoding, or asset loading. It may not represent normal scrolling. Capture several frames during the real interaction.
A high CPU reading also does not identify the bottleneck by itself. The frames chart tells you which frame stage missed its deadline. Timeline Events then shows what consumed that stage's time.
Exercise
The selected frame has a 4 ms UI duration and a 24 ms raster duration on a 60 Hz device. What kind of jank is most likely, and what should you inspect first?
Show answer
It is most likely raster-thread jank. Inspect images, compositing layers, clipping, shadows, custom painting, and shader activity in the frame analysis and timeline.