Sub topic 2
The Frame Budget and What Jank Means
Prerequisites
This chapter assumes that you can build and run a basic Flutter app, understand widgets and build() methods, and use flutter run. You should also have a physical device or emulator available. The examples use Flutter’s UI thread and raster thread, which are introduced in the frame pipeline.
What the frame budget means
A Flutter app displays its user interface as a sequence of frames. A frame is one complete image prepared for the screen. The display refreshes at a fixed rate, such as 60 or 120 times per second.
The frame budget is the time available to prepare one frame before the next screen refresh:
| Display refresh rate | Approximate frame budget |
|---|---|
| 60 Hz | 16.67 ms |
| 90 Hz | 11.11 ms |
| 120 Hz | 8.33 ms |
At 60 Hz, Flutter must finish the work for each frame in about 16.67 milliseconds. A 120 Hz display gives the app less than half that time. The same code can therefore feel smooth on one device and stutter on another.
The budget is a deadline, not a target for only build(). Flutter must perform several kinds of work, including building widgets, calculating layout, painting, converting drawing commands into pixels, and presenting the result.
How a frame misses its deadline
Flutter mainly divides frame production between two threads:
- The UI thread runs Dart code. It handles widget builds, layout, painting commands, input, and other application work.
- The raster thread converts those painting commands into pixels and prepares the frame for the GPU.
These threads can work on different frames at the same time. However, both must finish their assigned work before the frame can appear. A slow UI thread can delay the commands sent to the raster thread. A slow raster thread can receive commands on time but fail to draw them before the display deadline.
What jank looks like
Jank is visible unevenness caused by missed frame deadlines. Scrolling may pause briefly, an animation may jump, or a transition may update less often than expected.
Suppose a 60 Hz app spends 20 milliseconds preparing one frame. The display deadline arrives after 16.67 milliseconds, so that frame is late. The screen may keep showing the previous frame until the new one is ready. The next frame might also be delayed, producing a noticeable hitch.
A slow frame does not always mean that every part of the app is slow. One expensive operation can block the UI thread:
- Parsing a large response during
build() - Sorting or filtering a large list synchronously
- Rebuilding a large widget subtree
- Performing too much layout or painting
- Compiling a shader during the first use of an effect
- Decoding or resizing large images
Distinguishing workload from refresh rate
The frame budget depends on the device, but the work inside your app is usually the same. This makes high-refresh-rate devices stricter. Code that uses 10 milliseconds per frame fits within a 60 Hz budget, but it misses a 120 Hz budget of 8.33 milliseconds.
Do not judge performance only by whether an animation appears acceptable on your development machine. Profile on representative hardware, especially lower-powered devices and devices with high refresh rates.
A useful mental model
When you investigate jank, ask two questions:
- Did the UI thread miss its deadline?
- Did the raster thread miss its deadline?
The Performance view in DevTools answers these questions by showing frame timings. Later, you will use the frames chart, Frame Analysis, and Timeline Events to identify the operation that consumed the budget.
Exercise
An animation runs on a 60 Hz device. One frame takes 12 ms on the UI thread and 7 ms on the raster thread. Why might this frame still be late, and what should you check?
Show answer
The two stages have different work and can overlap, so adding their times is not always correct. Check the actual frame timeline to see whether either stage missed its deadline, and also check whether other work delayed the frame.