Sub topic 8
Inspecting a frame in Timeline Events
The frames chart shows which frames are slow. The Timeline Events tab shows what the engine and framework did during one of those frames. This chapter covers how to select a frame, read its event rows, follow the nesting from the top-level frame event down to the widget work, and recognize what a slow event is telling you.
Prerequisites
This chapter assumes you can:
- Run an app in profile mode and open the DevTools Performance view.
- Read the frames chart and tell UI-thread bars from raster-thread bars.
- Use the Frame Analysis tab to get a first guess at a slow frame's cause.
If any of these are unfamiliar, review the earlier chapters first. Event names and layout shift slightly between Flutter and DevTools versions, so treat the names below as typical rather than guaranteed.
What the tab shows
Timeline Events is a flame chart. Each rectangle is one timed event. Width is duration, and a rectangle sits on top of the events that ran inside it. If event B is drawn under event A, B started and finished while A was still running. Time runs left to right.
The chart is split into horizontal tracks, one per thread. For frame work you care about two:
| Track | What runs there | Matching bar in the frames chart |
|---|---|---|
| UI thread (often labeled `<n>.ui`) | Dart code: animations, `build()`, layout, paint recording, building the layer tree | UI bar |
| Raster thread (often labeled `<n>.raster`) | The engine turning the layer tree into GPU work and submitting it | Raster bar |
You may also see tracks for I/O and platform threads, and for any isolates you started. They are useful for checking whether work moved off the UI thread, but they do not count against the frame budget.
Selecting a frame
The Timeline Events tab can show a long stretch of time, so first narrow it to the frame you care about.
- In the frames chart, click the bar of the slow frame. Red bars mark frames that missed the budget.
- The tab scrolls and zooms to that frame's time range, and the frame's UI and raster events are highlighted.
- Click any event rectangle. The bottom panel shows its name, duration, and any arguments attached to it.
Zoom and pan with the mouse wheel with Ctrl or Cmd held, or use the keyboard: W and S zoom in and out, A and D pan left and right. Zooming matters because a 16 ms frame may be only a few pixels wide until you zoom in.
Selecting the frame first is better than scrolling through the whole recording. A full recording contains hundreds of frames, and only the one you picked is the one you can tie to a number in the frames chart.
Reading the UI-thread events
A typical UI-thread frame has this nesting. Names vary a little between versions.
Animator::BeginFrameorFrame: the root event for the whole UI-thread part of the frame.Animate: ticker and animation callbacks run here.Build: the framework rebuilds dirty widgets, so yourbuild()methods run here.Layout: render objects compute sizes and positions.Compositing BitsandPaint: render objects record drawing commands into layers.CompositingandFinalize Tree: the layer tree is assembled and handed to the raster thread.
The order follows the pipeline you saw earlier. When a frame is slow, one of these children is usually much wider than the others. That wide child tells you which phase to investigate.
| Wide event | Typical meaning |
|---|---|
| `Build` | Too many widgets rebuilt, or expensive code inside `build()` |
| `Layout` | Costly layout, such as deep intrinsic-size calculations or very large lists laid out at once |
| `Paint` | Heavy custom painting or many layers to record |
| `Animate` | Slow code in a ticker or animation listener |
Reading the raster-thread events
The raster thread has a shorter tree. You usually see a rasterizer draw event, such as GPURasterizer::Draw or Rasterizer::DoDraw, with the engine's drawing and submission work beneath it. With Impeller you may see entries for its render passes.
You rarely fix raster work by changing a specific event. The events show that the raster thread was busy, and the cause is nearly always in what the UI thread sent it. Common causes are large saveLayer operations (from Opacity, ClipRRect with anti-aliasing, or ShaderMask), big images, and complex clip or blur effects. A later chapter covers fixes. For now, remember that a wide raster event means "the scene was expensive to draw", not "this event name is the bug".
A worked example: a wide Build with nothing inside
Use the deliberately slow widget from the topic plan. This version is complete, with a button that triggers rebuilds:
import 'package:flutter/material.dart';
int slowSum() {
var total = 0;
for (var i = 0; i < 50000000; i++) {
total += i % 7;
}
return total;
}
class JankyText extends StatelessWidget {
const JankyText({super.key});
@override
Widget build(BuildContext context) {
// Expensive work inside build() runs on the UI thread on every rebuild.
return Text('Result: ${slowSum()}');
}
}
void main() => runApp(const MaterialApp(home: CounterPage()));
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _taps = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Taps: $_taps'),
const JankyText(),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _taps++),
child: const Icon(Icons.add),
),
);
}
}Run it on a device in profile mode, as in the earlier chapter:
flutter devices
flutter run --profile -d <device-id>JankyText is const, but slowSum() is inside its build(), and Flutter calls that method again whenever the element is rebuilt. Tap the button a few times, then:
- Open the Performance view and find the tall red UI bar in the frames chart.
- Click it. Timeline Events zooms to that frame.
- On the UI track, locate the root frame event and look at its children.
- You should see a
Buildevent that takes nearly the whole frame, whileLayoutandPaintare thin slivers. - Click
Buildand read its duration in the bottom panel. It should be well above the frame budget.
The Build event also has almost no children. That is the useful clue. Normally Build expands into many small events as widgets rebuild. A wide event with nothing beneath it means the time is spent in code that is not itself a traced framework operation, such as a loop or a synchronous parse running directly in your build() method. Per-widget build events exist only when tracing is enhanced, which a later chapter explains. Either way, an empty wide event points at plain Dart code.
Checking the other thread
After finding a wide event on the UI track, check the raster track for the same frame. In the example, the raster event should be short. That confirms this is UI-thread jank. If both tracks were wide, you would have two separate problems, and fixing the UI one would not remove the raster one.
Also check the time between frames. A frame can be fast and still appear late if the UI thread was blocked before it began. A long event sitting before the frame's root event is work the frames chart attributes to nothing, such as a timer callback or a message handler running synchronously. Zoom out a little to see what ran just before the slow frame.
Following the workflow
Common mistakes
- Profiling in debug mode. Debug builds add checks and run unoptimized code, so every event looks slower than it will in production. Always capture in profile mode.
- Reading the whole recording at once. Select one frame first. Averages over many frames hide the spike you are looking for.
- Blaming a child event for a parent's time. A parent's duration includes its children. Compare a parent's width with the sum of its children's widths. The gap is time spent in the parent itself.
- Trusting one frame. The first frame after a screen opens often includes one-time setup. Capture several frames and check that the pattern repeats.
- Fixing the first thing you see. Choose the widest event on the slow frame, not the one at the top of the screen.
- Ignoring other tracks. If work you moved to an isolate now shows up on another track, that is expected and fine. If the UI track is still wide, the move did not take effect.
What to carry forward
Timeline Events turns the question "why is this frame slow?" into "which event is wide, and does it have children?" The name of the widest event on the UI track tells you which phase to investigate. A wide event with no children points at plain Dart code. A wide raster event points back at what the UI thread sent. The next chapters use this reading skill to apply fixes, and then add finer-grained events to the trace.
Exercise
In Timeline Events, the UI track for a red frame shows a Build event covering about 90% of the frame, with Layout and Paint both very thin and no child events under Build. The raster track for the same frame is short. What does this tell you, and where do you look next?
Show answer
This is UI-thread jank caused by work in the build phase. The raster thread is not the problem. The lack of child events suggests the time is spent in plain Dart code inside a build() method, such as a long loop or synchronous parsing, rather than in many widget rebuilds. Look through the build() methods of the widgets that rebuilt in that frame for expensive synchronous work, and move it out of build() (for example into an isolate started once). Then re-measure.
Exercise
Why should you click a specific frame in the frames chart before reading Timeline Events, instead of scrolling the whole recording?
Show answer
Clicking a frame zooms the flame chart to that frame's time range and ties the events to a frame whose duration you can see in the frames chart. A full recording contains many frames, so a single slow one is easy to miss, and its events are too narrow to read without zooming. Working on one selected frame lets you compare its UI and raster tracks directly.
Exercise
A parent event is 12 ms wide, and its children add up to 3 ms. What does the remaining time mean, and how would you investigate it?
Show answer
About 9 ms was spent in the parent event itself, outside any traced child. This usually means untraced Dart code ran directly inside that phase, for example synchronous work in a build() method. Investigate by looking at the code that runs in that phase, and consider wrapping suspect blocks in named timeline events or turning on the enhanced tracing options, both covered in later chapters, so the time is attributed to named events.