Sub topic 2
The frame budget and what jank means
This chapter assumes you can run a Flutter app and write StatelessWidget and StatefulWidget classes. It also assumes you know that Flutter draws the UI in frames. By the end you should be able to compute the time budget for a given display. You should also be able to say why a frame that misses the budget is visible to the user, and why an average frame rate hides the problem.
What a frame budget is
A display refreshes at a fixed rate. A 60 Hz screen shows a new image 60 times per second, so a new image is due about every 16.67 ms. Flutter has to finish all of the work for a frame before the display asks for the next image. The time available for that work is the frame budget.
The budget comes from the refresh rate:
budget (ms) = 1000 / refresh rate (Hz)| Refresh rate | Budget per frame |
|---|---|
| 60 Hz | 16.67 ms |
| 90 Hz | 11.11 ms |
| 120 Hz | 8.33 ms |
| 144 Hz | 6.94 ms |
Two points follow from this table:
- The budget is a property of the device, not of your app. The same code that fits comfortably on a 60 Hz phone can miss the budget on a 120 Hz phone.
- The budget is a ceiling, not a target. Finishing in 4 ms instead of 12 ms does not make the screen look better. It leaves headroom for slower devices and background work.
Why there are two clocks to watch
The previous chapter described how Flutter splits a frame between the UI thread and the raster thread. The UI thread runs your Dart code: build, layout, and the creation of a layer tree. The raster thread takes that layer tree and turns it into GPU commands.
These two threads work as a pipeline. While the raster thread draws frame N, the UI thread can already start building frame N+1. For this reason, the budget applies to each thread separately. A frame is on time when the UI thread finishes its part within the budget and the raster thread finishes its part within the budget.
The pipeline means that a frame's total latency from start to screen can be longer than one budget. What matters for smoothness is that neither thread becomes the bottleneck. If one thread takes longer than the budget, the pipeline backs up and the next frame cannot start on time.
What jank is
Jank is the visible stutter that happens when a frame is not ready for the vsync signal it was meant for. The display has no new image, so it shows the previous one again. To the user, motion freezes for one refresh interval and then jumps.
A few details are worth being exact about:
- One missed frame is a dropped frame. Jank is the user-visible effect of dropped frames, and it is most noticeable during animation and scrolling, where the eye expects smooth change.
- A single 20 ms frame in a 60 Hz app delays the update by one refresh interval. A 100 ms frame holds the display for about six refresh intervals, which is clearly a freeze.
- Jank is about timing, not about how much work your app does overall. An app that uses 10% of the CPU can still be janky if that 10% arrives in one long block on the UI thread.
Consider a list scrolling at 60 Hz, where most frames take 6 ms and one frame in every 40 takes 30 ms. Each slow frame misses its vsync by about 13 ms. The 30 ms frame overruns the 16.67 ms budget and holds the screen for about two refresh intervals, and the user sees the scroll hitch.
Why average frame rate is the wrong number
The first number people reach for is frames per second (FPS). An average of 58 FPS sounds close to perfect, but it can hide a long frame. Take 59 frames of 8 ms and one frame of 200 ms. Over a second, the average is still about 1 frame per 8 ms for most of the window, but the user saw a 200 ms freeze.
For this reason, performance work looks at individual frames and at the slowest ones. Useful questions are:
- How many frames exceeded the budget during the interaction I care about?
- How far over the budget were the worst ones?
- Which thread was over: UI, raster, or both?
Percentile figures such as the 95th or 99th percentile frame time describe a run better than the mean does. The DevTools frames chart, which a later chapter covers, answers the first two questions visually. It shows one bar per frame and marks the frames that missed the budget.
How DevTools decides a frame is janky
DevTools compares each frame's UI time and raster time against the budget for the device's refresh rate. A frame that goes over on either thread is flagged as janky. DevTools also uses a separate marker for frames that are much slower, called shader compilation or very slow frames, but the basic rule is the same: compare the thread time with the budget.
This has two practical consequences:
- If your device runs at 120 Hz, DevTools flags frames over about 8.33 ms. A trace from a 60 Hz device and a trace from a 120 Hz device are therefore not directly comparable by the number of red frames.
- A frame that is flagged on the UI thread and a frame flagged on the raster thread need different fixes. Telling these apart is the subject of a later chapter. For now, remember that "over budget" always comes with a thread name.
Measuring the budget from code
DevTools is the main tool, but you can also observe frame timings from inside the app. The engine reports a FrameTiming object for each frame. It has a buildDuration for the UI thread work and a rasterDuration for the raster thread work. The next program logs any frame that goes over the 60 Hz budget.
import 'dart:ui' show FrameTiming;
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Budget for a 60 Hz display, in microseconds.
const budget = Duration(microseconds: 16667);
WidgetsBinding.instance.addTimingsCallback((List<FrameTiming> timings) {
for (final timing in timings) {
final uiOver = timing.buildDuration > budget;
final rasterOver = timing.rasterDuration > budget;
if (uiOver || rasterOver) {
debugPrint(
'Slow frame: '
'ui=${timing.buildDuration.inMicroseconds / 1000} ms, '
'raster=${timing.rasterDuration.inMicroseconds / 1000} ms',
);
}
}
});
runApp(
MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: 1000,
itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
),
),
),
);
}Run this in profile mode and scroll the list. On a fast device you will probably see few or no lines. If you add expensive work to itemBuilder, lines start to appear.
The timings arrive in batches. The engine delivers them after a short delay, so the callback is for after-the-fact logging, not for reacting within the same frame.
Edge cases and common misunderstandings
- Debug mode inflates timings. In debug mode, Flutter runs unoptimized code and extra assertions, so frames that look slow may be fine in a real build. The next chapter in the plan explains why profiling needs profile mode. Do not draw conclusions about the budget from a debug run.
- Variable refresh rate. Some devices change their refresh rate depending on content, for example dropping to 60 Hz or lower when idle. The budget can differ between two moments in the same session.
- An emulator or simulator is not a device. Timings on a desktop-hosted emulator reflect the host machine, so they say little about the budget on a phone.
- The first frame is special. The first frames of a screen can include one-time costs, such as loading assets or compiling shaders. A slow first frame does not always mean the steady state is slow, but it can still be a real user-facing problem on app start or navigation.
- Under budget is not always smooth. If work finishes at 15 ms on a 16.67 ms budget, any extra load, such as a garbage collection pause or a busy background process, can push it over. Look for frames that sit consistently near the limit.
- Missing the budget once is not always a bug. A one-off slow frame during an expensive screen transition may be acceptable. Frames that are slow during continuous scrolling or animation deserve attention first.
A working rule
Use this rule as the starting point for the later chapters:
- Find the refresh rate of the device you are testing on and compute its budget.
- Reproduce the interaction that feels slow, in profile mode.
- Count the frames that exceed the budget and note which thread each one exceeded it on.
- Treat the slowest frames, not the average, as the ones to investigate.
Exercise
A device runs at 90 Hz. Your app has a UI-thread time of 7 ms and a raster-thread time of 13 ms for one frame. Does this frame meet the budget? Which thread is the problem, and would the answer change on a 60 Hz device?
Show answer
The budget at 90 Hz is 1000 / 90 = 11.11 ms. The UI thread (7 ms) is under budget, but the raster thread (13 ms) is over it, so the frame is janky and the raster thread is the one to investigate. On a 60 Hz device the budget is 16.67 ms. Both threads would then be under budget, and the same frame would not count as janky.
Exercise
An app shows an average of 59 FPS over a 10-second scroll, but users report a noticeable stutter. Give one reason the average can be misleading, and name two figures you would look at instead.
Show answer
The average spreads a few very long frames across many fast ones, so a single 150 ms freeze barely changes the mean. Look at the number of frames that exceeded the budget during the scroll and the worst frame times, such as the slowest frame or a 95th or 99th percentile frame time, ideally split by UI thread and raster thread.