Sub topic 7
Finding Jank in the Performance View
This chapter assumes that you can build simple Flutter apps, understand the widget tree, and use basic Dart and asynchronous code. You should also know how to run an app on an emulator, simulator, or physical device.
A frame is one complete image that Flutter prepares for the screen. At 60 frames per second, each frame has about 16 milliseconds to finish. At 120 frames per second, it has about 8 milliseconds. If a frame takes longer than its available time, the screen appears to pause or stutter. This is called jank.
Run the app in the right mode
Debug mode adds checks and diagnostic work, so its timing does not represent real user performance. Use profile mode when measuring frame performance. Profile mode keeps performance characteristics close to release mode while allowing DevTools to connect.
# Run the app in profile mode on a connected device
flutter run --profile
# Start DevTools separately
dart devtoolsThe flutter run command prints a VM service address. Use that address to connect DevTools to the running application.
Read the Performance view
Open the Performance view in DevTools and interact with the slow part of the app. Scroll a long list, open the screen, or perform the gesture that exposes the problem. The chart records frame timing and activity over time.
The chart commonly separates work into two tracks:
- The UI thread builds widgets, runs Dart code, handles input, and produces layer information.
- The raster thread turns that layer information into pixels for the display.
A tall frame bar means that frame took longer than the available budget. A UI-thread spike suggests expensive Dart or widget-building work. A raster-thread spike suggests expensive painting, large images, effects, or complex scenes.
Select a slow frame to inspect its timeline events. Look for long synchronous functions, repeated builds, image decoding, layout work, or painting operations. A single slow frame can come from a tap handler. Repeated slow frames during scrolling often point to work performed on every build or paint.
Add labeled timeline events
A timeline event gives a name to a region of work. Timeline.timeSync records synchronous work in the Performance view, making a suspected operation easier to find.
import 'dart:developer';
int sumOfSquares(List<int> values) {
return Timeline.timeSync('sumOfSquares', () {
var total = 0;
for (final value in values) {
total += value * value;
}
return total;
});
}Use a timeline label around a narrow operation rather than around an entire screen update. Then repeat the action, select the frame, and check whether the labeled event overlaps the slow period. If it does, measure its input size and frequency before changing the code.
Find and verify the cause
Use this sequence:
- Reproduce the symptom in profile mode.
- Record the interaction in the Performance view.
- Select one or more slow frames.
- Identify whether the UI thread or raster thread is slow.
- Inspect the longest events and add a timeline label if the cause is unclear.
- Change one operation, then record the same interaction again.
- Confirm that frame times improved without creating another problem.
Common fixes include moving expensive computation away from the UI thread, reducing unnecessary rebuilds, resizing oversized images, simplifying paint effects, and avoiding repeated synchronous work during scrolling. The Performance view shows where time was spent, but it does not automatically prove which change is safe. Re-test the complete interaction after every targeted fix.
Performance view and CPU Profiler
The Performance view answers when a frame became slow and how that work relates to rendering. The CPU Profiler helps answer which Dart functions consumed CPU time. Use the Performance view first to locate the bad interaction. Use the CPU Profiler when the UI thread is busy but the responsible Dart function is not obvious.
Exercise
A scroll gesture produces tall frame bars, and the UI thread is slow while the raster thread remains short. Which tool should you inspect first, and what does this pattern suggest?
Show answer
Inspect the Performance view first. The pattern suggests that Dart code, widget building, input handling, or layout work is taking too long on the UI thread rather than painting being the main problem.