Sub topic 5
Reading the Flutter frames chart
This chapter covers the chart at the top of the DevTools Performance view. It assumes you can run an app with flutter run --profile, can open the Performance view, and know the frame budget and the split between the UI thread and the raster thread from earlier chapters. Here you learn what each element of the chart means and what to check before you inspect a frame in detail.
What the chart shows
The Flutter frames chart is a bar chart. Each bar is one frame that your app produced. Time runs left to right, so new frames appear on the right.
Every frame has two parts:
- UI: the time the UI thread spent running your Dart code for that frame. This covers build, layout, and paint, and it ends with the layer tree handed to the engine.
- Raster: the time the raster thread spent turning that layer tree into GPU commands and pixels.
The two parts are drawn together for each frame. The bar is as tall as the slower of the two. Both parts are measured against the same budget, so one slow thread is enough to make the frame miss its deadline.
The chart also shows a horizontal line that marks the frame budget. At 60 Hz the budget is about 16.7 ms. On a 120 Hz display it is about 8.3 ms. DevTools uses the display's refresh rate, so the same app can look fine on one device and janky on another.
A frame to look at
You need an app that draws frames, and ideally one that sometimes draws slow ones. This app rebuilds when you tap a button. Its build() method does deliberately expensive work, so every tap produces a slow UI-thread frame.
import 'package:flutter/material.dart';
int slowSum() {
var total = 0;
for (var i = 0; i < 50000000; i++) {
total += i % 7;
}
return total;
}
void main() => runApp(const MaterialApp(home: Counter()));
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _taps = 0;
@override
Widget build(BuildContext context) {
// Expensive work inside build() runs on the UI thread on every rebuild.
final result = slowSum();
return Scaffold(
body: Center(child: Text('Taps: $_taps, result: $result')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _taps++),
child: const Icon(Icons.add),
),
);
}
}Run it in profile mode on a real device, as described in the previous chapter:
flutter devices
flutter run --profile -d <device-id>Open the Performance view from DevTools, then tap the button several times.
Reading the bars
Once frames are flowing, work through the chart in this order.
- Check the budget line. Bars that stay under it are healthy. Bars that cross it are the ones to investigate.
- Look at the highlight color. DevTools marks frames that miss the budget in a warning color, so you can spot them without measuring heights. A separate color is used for frames in which shader compilation was detected. The legend beside the chart lists the current colors, so read it instead of relying on memory. The colors have changed between DevTools releases.
- Compare the UI and raster parts of a bad bar. The taller part shows which thread is the bottleneck. In the example above, the UI part towers over the raster part.
- Hover over a frame. The tooltip gives the frame number and the UI and raster times in milliseconds. Use the numbers, not your reading of the bar height.
- Look at the pattern, not one bar. One slow frame at startup is normal. A slow frame on every tap, or a run of slow frames during a scroll, is a real finding.
The signals below map what you see to what it usually means.
| What you see | What it usually means | Next step |
|---|---|---|
| UI part over the line, raster part small | Too much Dart work on the UI thread, often in `build()` or layout | Select the frame and open Timeline Events |
| Raster part over the line, UI part small | The scene is expensive to draw, for example with heavy clipping, `saveLayer`, or large images | Select the frame and check the raster side of the trace |
| Both parts over the line | Heavy work on both threads, or the device is throttled | Fix the UI side first, then re-measure |
| A single over-budget frame the first time a screen or animation appears | Possible shader compilation or first-use setup cost | Check for the shader jank color, then see the chapter on shaders |
| Many bars near the line, none clearly over | The app has no headroom and will drop frames on slower devices | Treat it as a warning, not as noise |
Selecting a frame
Click a bar to select it. The selected frame stays highlighted, and the tabs below the chart, such as Frame Analysis and Timeline Events, show data for that frame only. This is how the chart connects to the other tools. The chart tells you which frame to look at, and the tabs tell you what happened inside it.
Selection matters for another reason. The chart keeps only a limited number of recent frames, and new frames push old ones out. If you want to study a bad frame, select it before it scrolls away. If you cannot catch it in time, pause the recording and then select it. The chart does not need to keep moving for the tabs to work.
Edge cases that change how you read the chart
An empty or frozen chart is often correct. Flutter draws a frame only when something changes, such as an animation tick, a setState, or a scroll. If your app is idle, no new bars appear. This does not mean DevTools is broken. Interact with the app to generate frames.
The first frames are slow. Startup builds the whole widget tree for the first time and loads resources. Do not judge the app by those frames. Navigate to the screen you care about, wait for it to settle, then perform the action you want to measure.
Short bars do not guarantee smooth motion. The chart reports how long each frame took. It does not show frames that were never produced, for example when the UI thread was blocked for a long time and no frame started at all. A long gap between bars during an animation is also a sign of trouble.
The numbers depend on the device. An emulator, or the same phone in debug mode, gives timings that do not match what users see. Debug mode adds checks and runs unoptimized code, so the bars look worse than reality. Profile mode on a real mid-range device is the baseline to trust.
Other work can distort a frame. A frame can look slow because of a garbage collection pause, a background app, or thermal throttling after a few minutes of testing. If a result surprises you, repeat the action a few times. A cause that shows up consistently is the one to fix.
Turning on extra tracing slows frames. The enhance-tracing options add per-widget events to the timeline. They help with diagnosis, but they add overhead, so frame times with them on are slightly worse than without. Use them to find the cause. Then turn them off before you compare timings.
A short worked reading
Suppose you tap the button in the example app five times. The chart shows five tall bars. The tooltip for one of them says UI 78 ms and raster 3 ms, and the budget is 16.7 ms.
- The frame is far over budget, so it is jank.
- The UI part accounts for almost all of the time, so the problem is Dart code on the UI thread. Changing images or clips would not help.
- It happens on every tap, so it is tied to the rebuild and not to a one-off cost.
That reading tells you where to look next: select a frame and open Timeline Events to confirm that build() is the long event. The chart cannot tell you which function is slow. It tells you which frame and which thread to examine.
Summary of what the chart can and cannot tell you
The chart can tell you which frames missed the budget, which thread was responsible, how often it happens, and whether shader compilation was detected. It cannot tell you which widget or function is responsible. For that you use Frame Analysis and Timeline Events on the frame you selected.
Exercise
You run an app on a 120 Hz phone in profile mode. A frame has UI 6 ms and raster 11 ms. Is it janky, and which thread is the likely cause?
Show answer
Yes. The budget at 120 Hz is about 8.3 ms, and the raster part at 11 ms exceeds it, even though the UI part is within budget. The raster thread is the bottleneck, so look for expensive drawing, such as heavy clipping, saveLayer, or large images, not for slow Dart code. The same frame would pass at 60 Hz, where the budget is about 16.7 ms.
Exercise
You open the Performance view, and no new bars appear while you watch the app. Name two things to check before concluding that DevTools is broken.
Show answer
First, check whether the app is idle. Flutter draws frames only when something changes, so interact with the app or start an animation. Second, check that the chart is not paused, and that you are connected to the running app in profile mode on the device you expect.