Sub topic 7
Using Frame Analysis to find the likely cause
This chapter assumes you can run an app in profile mode and open the Performance view in DevTools. It also assumes you can pick a frame in the Flutter frames chart and tell UI-thread work from raster-thread work. If not, review the earlier chapters on those topics first. Here you will learn what the Frame Analysis tab reports, how to read it, and where its limits are.
What Frame Analysis is
The frames chart tells you that a frame was slow and which thread was slow. It does not tell you why. You can answer that by reading the Timeline Events tab, which shows every traced event in the frame. That is accurate but slow, because you have to know what to look for.
The Frame Analysis tab is the shortcut. When you select a frame, it does three things:
- It splits the frame time into the phases the engine measures: build, layout, paint, and raster. It also reports time spent compiling shaders when that applies.
- It checks the frame's trace against a set of known problems and shows a hint for each match.
- It tells you which tracing options are on, because some hints only appear when more detail is recorded.
The result is a starting hypothesis. It is not a verdict. Treat it as the answer to "where should I look first?" and confirm it in Timeline Events before changing code.
Opening the tab
- Run the app in profile mode on a real device:
flutter run --profile -d <device-id>. - Open DevTools and go to the Performance view.
- Use the app until a frame shows in red in the frames chart. Red means the UI thread, the raster thread, or both exceeded the frame budget.
- Click that frame bar. The Frame Analysis tab, next to Timeline Events, now describes the selected frame.
Frame Analysis always describes one selected frame. If you click a different frame, the tab updates. If you have not selected a frame, the tab has nothing to analyze.
The frame time breakdown
At the top of the analysis is a breakdown of where the frame's time went. Read it before the hints, because it tells you which hints deserve attention.
| Segment | Thread | What it covers |
|---|---|---|
| Build | UI | Running `build()` methods for widgets that were marked dirty |
| Layout | UI | Computing sizes and positions of render objects |
| Paint | UI | Recording drawing commands into layers |
| Raster | Raster | Turning the layer tree into pixels on the GPU |
| Shader compilation | Raster | Compiling GPU programs the first time they are needed |
Use the breakdown to answer two questions:
- Which phase dominates? One phase that takes most of the frame is a better lead than several small ones.
- Is the frame over budget on one thread or both? A frame that is slow on the UI thread and cheap on the raster thread points to Dart code. The reverse points to what you are asking the GPU to draw.
At 60 Hz the budget is about 16.7 ms per frame. At 120 Hz it is about 8.3 ms. The UI and raster threads each work against that budget, since they run as a pipeline. A frame with 20 ms of build time is janky even if raster takes 2 ms.
The hints
Below the breakdown, Frame Analysis lists hints. Each one names a pattern it detected in the selected frame's trace and says what usually fixes it. The common categories are:
- Slow UI-thread work. The frame's UI portion is over budget. The hint suggests looking at Timeline Events to find the expensive event. This is the generic hint, and it usually means Frame Analysis has no more specific explanation.
- Shader compilation. The raster thread spent time compiling shaders during the frame. This typically happens the first time an effect appears, which is why it often looks like a one-off hitch.
- Intrinsic passes. Layout ran extra passes to ask children for their natural size. Widgets such as
IntrinsicHeightandIntrinsicWidthcause this, and their cost grows quickly with nesting depth. - Expensive raster operations. Operations such as
saveLayerforce the GPU to draw into an offscreen buffer and then composite it.Opacity, clipping with anti-aliasing, and shadows are common triggers. - Images that could be cached. An image or a stable, costly subtree is re-rasterized on every frame, and a raster cache could avoid that.
- Tracing options. A note that enhanced tracing is off, which limits how specific the analysis can be.
Some hints are tied to a detected pattern. Others only show that a threshold was crossed. In both cases the hint is a pointer to a place in the trace, not proof that this is the only cause.
A frame that triggers several hints
The following app is complete and runnable. It builds a list in which every row uses IntrinsicHeight and an Opacity, which are two of the patterns Frame Analysis recognizes.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HintDemo()));
}
class HintDemo extends StatelessWidget {
const HintDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Frame Analysis demo')),
body: ListView.builder(
itemCount: 200,
itemBuilder: (context, index) {
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(width: 8, color: Colors.indigo),
Expanded(
child: Opacity(
opacity: 0.85,
child: ListTile(
title: Text('Row $index'),
subtitle: const Text('Scroll quickly to build frames'),
),
),
),
],
),
);
},
),
);
}
}Run it in profile mode, scroll fast, and select the slowest red frame. Compare the breakdown with the hints. You should see layout time that is larger than it needs to be, and possibly raster time from the opacity layers. Whether a particular frame crosses the budget depends on the device, so on a fast phone you may need to scroll harder or test on a slower device.
Working through an analysis
Use the same sequence each time.
- Pick the right frame. Choose a frame that is clearly over budget, not one that barely crossed the line. Large outliers have clearer signals. If many frames are slightly over, pick a typical one and then check another.
- Read the breakdown first. Note the dominant phase and which thread is over budget.
- Read the hints that match. If the breakdown says layout dominates and a hint mentions intrinsic passes, those two agree and you have a strong lead. If a hint conflicts with the breakdown, trust the breakdown.
- Confirm in Timeline Events. Open the same frame there and find the event the hint refers to. Check that its duration accounts for a meaningful share of the frame.
- Change one thing, then re-measure. The final chapter covers this loop in detail.
Why enhanced tracing changes the analysis
By default the trace records the framework's top-level work for a frame but not each widget. Frame Analysis can only report what the trace contains. If build time is high but the trace has no per-widget events, the tab can say that build is slow without saying which widgets were rebuilt.
The enhance-tracing toggles add per-widget events for builds, layouts, and paints. With them on, Frame Analysis and Timeline Events can name specific widgets. The cost is that tracing itself adds overhead, so frames get a little slower and the numbers shift. Use this pattern:
- Profile with tracing off to see whether the problem exists at realistic timings.
- Turn tracing on only to find which widget is responsible.
- Turn it off again before you judge whether a fix worked.
The toggles can also be set from code, which is useful when you want them on from startup:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
// Adds timeline events for each widget build, layout, and paint.
// Use in profile runs only, and remove before shipping.
debugProfileBuildsEnabled = true;
debugProfileLayoutsEnabled = true;
debugProfilePaintsEnabled = true;
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Hello')))));
}Limits and common mistakes
- Analyzing a debug build. Debug mode compiles Dart differently and runs extra assertions. Every frame looks slow, and the hints describe problems that do not exist in a release build. Always use profile mode on a physical device.
- Treating the first hint as the only cause. Hints are independent checks. A frame can have an intrinsic-pass problem and an expensive
saveLayer. Fix the one that accounts for the most time first. - Acting on a hint with no time behind it. A hint for a pattern that takes 0.3 ms of a 20 ms frame is not worth changing. Compare every hint with the breakdown.
- Reading one frame as the whole story. A single frame can be an outlier from garbage collection or from the system. Look at several red frames and see which hints repeat.
- Missing the work that is not in the frame. A frame can be janky because a background isolate or the platform is competing for CPU. If the breakdown shows small build, layout, paint, and raster segments but the frame is still long, look at the raw timeline for gaps.
- First-run shader hitches. A shader compilation hint on a frame that happens only once, when an animation first appears, is a different problem from steady jank. Check whether the same frame is cheap the second time.
When Frame Analysis has nothing useful to say
Sometimes the tab shows only the generic slow-UI hint. That usually means the expensive work is your own Dart code, such as parsing, sorting, or a long loop inside build(). Frame Analysis cannot recognize a pattern in code it knows nothing about. Switch to Timeline Events, find the longest event in the UI thread, and read its name. If the name is not informative, wrap the suspect code in a custom timeline event so it shows up by name on the next capture. A later chapter covers that technique.
Exercise
In profile mode you select a red frame. The breakdown shows 3 ms of build, 2 ms of layout, 1 ms of paint, and 24 ms of raster. The only hint says the UI thread is slow. Which should you trust, and where do you look next?
Show answer
Trust the breakdown. The UI thread work totals about 6 ms, which fits the budget, so the UI-thread hint does not explain the jank. The raster thread is the problem at 24 ms. Look at the raster side of the frame in Timeline Events, and check for expensive operations such as saveLayer, Opacity, clipping, shadows, or shader compilation. Also check whether the frame is a first-time effect that is fast on later runs.
Exercise
Why should you turn off the enhance-tracing options before deciding whether a fix worked?
Show answer
Per-widget tracing adds overhead to every frame, so frame times are higher than they would be for real users. You use tracing to find which widget is slow, and then you compare before and after times with tracing off so the numbers reflect realistic performance.