Sub topic 7
Finding jank in the Performance view
This chapter assumes you can run a Flutter app on a device, that you know how the widget tree and build method work, and that you have DevTools open and connected (covered in earlier chapters). You also need to know the difference between debug, profile, and release mode, because everything below depends on it.
What jank is
Jank is visible stutter: an animation or scroll that skips or hesitates because the app missed its frame deadline. At 60 Hz each frame has a budget of about 16.7 ms. On a 120 Hz display the budget is about 8.3 ms. A frame that takes longer than the budget is dropped or shown late, and the user sees it.
Flutter does the work for each frame on two threads, and either can cause jank:
- The UI thread runs your Dart code:
build, layout, and painting into a layer tree. It is also called the UI thread in DevTools. - The raster thread takes that layer tree and turns it into GPU commands, using Impeller or Skia.
A frame is only on time if both threads finish inside the budget. The first job in the Performance view is to find out which thread is slow, because the fixes are different.
Set up a trustworthy measurement
Performance numbers from debug mode are not reliable. Debug builds use a JIT compiler, run assertions, and skip optimizations, so frames that look slow in debug may be fine in release. Use profile mode on a real device. Emulators and simulators use the host CPU and GPU, so their timings do not match what users see.
# Run the app in profile mode on a connected device
flutter run --profile
# Start DevTools on its own, then connect using the VM service URI printed by flutter run
dart devtoolsOpen the Performance tab. If you launched from an IDE, choose profile mode in the run configuration first. DevTools shows a notice when you are connected to a debug build, and you should take it seriously.
Reading the Frame Analysis chart
The top of the Performance view is a bar chart of recent frames. Each frame has two bars, one for the UI thread and one for the raster thread. The colors and labels are how you spot trouble:
| What you see | Meaning |
|---|---|
| Bars under the budget line | The frame finished on time. |
| Red or highlighted frame | The frame missed its deadline. This is the jank you are looking for. |
| Tall UI bar, short raster bar | The slow part is your Dart code: `build`, layout, or paint. |
| Short UI bar, tall raster bar | The slow part is rendering: heavy layers or effects. |
| Both bars tall | Both threads are overloaded, or a very expensive frame. |
| Shader compilation marker | Shaders were compiled during the frame, a known cause of first-run stutter on Skia. |
Select a slow frame. The panel below it shows Frame Analysis, which summarizes the frame and gives hints, such as which phase took longest or whether expensive operations like saveLayer were used. Treat these hints as leads to check, not as final answers.
You can also turn on the Performance Overlay from the same view. It draws two graphs over your app, one per thread, so you can watch jank while you interact with the app on the device.
Reading the timeline events
Selecting a frame shows its Timeline Events as a flame chart. Each block is a unit of work. Blocks nested under a parent happened inside it. Wider blocks took more time.
On the UI thread you will typically see these phases from top to bottom:
Animate: tickers and animation callbacks run.Build: widgets rebuild. Look here for expensivebuildmethods and for widgets rebuilding more often than they should.Layout: render objects compute sizes and positions.Compositing bitsandPaint: render objects draw into layers.Finalize tree: unmounting of removed elements.
On the raster thread you will see the rasterization of the layer tree. Wide blocks there usually trace back to saveLayer, large blurs, clipping, or opacity applied to complex subtrees.
Look for the widest block first. Fixing a 12 ms block matters more than fixing five 0.5 ms blocks.
Adding your own events to the timeline
Flame charts show framework work well, but your own code appears only as the Dart functions inside it, which can be hard to identify. You can label your own work with Timeline.timeSync. The block shows up by name in the timeline.
import 'dart:developer';
int sumOfSquares(List<int> values) {
return Timeline.timeSync('sumOfSquares', () {
var total = 0;
for (final v in values) {
total += v * v;
}
return total;
});
}Use this sparingly and mostly for suspicious sections. Each event has a small cost, and a chart full of custom labels is harder to read. Remove labels once you have found the problem.
Turning on extra tracing
The Performance view has options that add detail to the timeline. They add overhead, so turn one on only when you need it, then turn it off.
- Track widget builds shows which widgets were built in each frame, so you can find ones that rebuild unnecessarily.
- Track layouts shows which render objects performed layout.
- Track paints shows which render objects painted.
- Render-layer options such as showing
saveLayercalls and offscreen layers help you find expensive raster work.
Tracing changes timing. If frames get slower with a tracking option enabled, that is expected. Compare relative widths of blocks rather than absolute times.
Common causes and what the timeline shows
| Symptom in the timeline | Likely cause | Typical fix |
|---|---|---|
| Wide `Build` block for one widget | Heavy work inside `build`, such as parsing, sorting, or building large lists eagerly | Move work out of `build`, cache results, or use `ListView.builder` |
| Same widget built on every frame | Animation or state change rebuilds a large subtree | Push state down, use `const` constructors, narrow rebuilds with `AnimatedBuilder` or `ValueListenableBuilder` |
| Wide `Layout` block | Deeply nested or intrinsic-size layouts such as `IntrinsicHeight` | Simplify the layout, avoid intrinsic widgets in lists |
| Wide raster block with `saveLayer` | `Opacity`, `ShaderMask`, `ClipPath`, or blurs over large areas | Use `FadeTransition` or a color with alpha, reduce clipping, use `RepaintBoundary` where it isolates frequent repaints |
| Long block that is not a framework phase | Synchronous Dart work on the UI thread, such as JSON decoding of a large payload | Move it to an isolate with `Isolate.run` or `compute` |
| First animation janky, later runs smooth | Shader compilation (Skia) or lazy asset loading | Use Impeller where available, or warm up assets and shaders |
Worked example: a janky list
Suppose a product list stutters while scrolling in profile mode. Each item formats a price and sorts its tags inside build.
- Run
flutter run --profileon a real device and open the Performance tab. - Scroll the list. Frames turn red, and the UI bar is tall while the raster bar stays short. The problem is in Dart code.
- Select a red frame. The
Buildblock is wide, and enabling Track widget builds showsProductTilebuilt many times in one frame. - Look at
ProductTile.build. It calls a sort on every build. Move that work to where the data is loaded, or compute it once per item. - Hot restart in profile mode is not available in the same way as in debug, so stop and rerun the app. Scroll again and compare the frame chart.
The key is that the chart told you which thread to look at, the flame chart told you which phase, and the tracking option told you which widget. Only then did you open the code.
The Performance view versus the CPU Profiler
The Performance view tells you when a frame was slow and in which phase. When a wide block is Dart code and you cannot tell which function inside it is expensive, switch to the CPU Profiler, which samples the call stack and attributes time to functions. The next chapter covers it. Use the Performance view to locate the slow frame, and the CPU Profiler to explain a slow function.
Common mistakes
- Measuring in debug mode and chasing problems that release builds do not have.
- Measuring on an emulator, where the GPU path differs from real hardware.
- Looking at a single slow frame. One red frame during app start or a route transition may be normal. Look for patterns across many frames.
- Leaving tracking options on and then comparing against a baseline taken with them off.
- Changing several things at once, so you cannot tell which change helped.
- Optimizing a block because it looks large in the flame chart when the frame is still under budget.
Exercises
Exercise
In the Performance view, a scrolling list shows red frames where the UI thread bar is about 28 ms and the raster thread bar is about 4 ms. Which thread is the problem, and where in the timeline should you look first?
Show answer
The UI thread is the problem, since it is far over the roughly 16.7 ms budget at 60 Hz while the raster thread is well under it. Select a red frame and look at the UI thread flame chart for the widest block, usually under Build or Layout. Enable Track widget builds to see which widgets rebuild. Raster-side causes such as saveLayer are not the lead here.
Exercise
A teammate says a screen is slow because frames take 40 ms when running with flutter run on an emulator. Give two reasons this measurement may be misleading and say how to get a better one.
Show answer
Debug mode uses JIT compilation and assertions, and emulators do not reproduce real device CPU and GPU behavior. Run flutter run --profile on a physical device, open the Performance view, repeat the same interaction, and compare frame times against the frame budget.
Exercise
A fade-in animation uses an Opacity widget around a large, complex subtree, and the raster thread bar is tall during the animation. What change might help, and why?
Show answer
Replace Opacity with FadeTransition driven by the animation, or apply alpha to a color where possible. Opacity over a complex subtree can force a saveLayer, an offscreen render that is expensive on the raster thread. FadeTransition avoids rebuilding the subtree each frame and is cheaper. Verify the change in profile mode by comparing raster bar heights before and after.