Sub topic 2
Reading the Flutter frames chart
This chapter assumes you can run a Flutter app with flutter run, that you know StatelessWidget and StatefulWidget, and that you have a device or emulator with DevTools available.
The frame budget
Flutter aims to draw a new frame every 16.7 ms on a 60 Hz display, or every 8.3 ms on a 120 Hz display. If a frame takes longer than its budget, the screen repeats the previous image and the animation stutters. Users see this as jank.
Reading the frames chart
Open the Performance view in DevTools while your app runs in profile mode. The frames chart shows one bar per frame. Bar height is the time that frame took. A bar above the budget line is over budget, and DevTools colors it red. Click a bar to select that frame and load its details.
Telling UI-thread jank from raster-thread jank
Each frame has two stages that run on separate threads:
- UI thread: runs your
build, layout, and paint code and produces a description of the frame. - Raster thread: turns that description into GPU work and draws it.
Check which thread is slow in the selected frame. If the UI thread bar is long, your Dart code is the likely cause, for example expensive work in build. If the raster thread bar is long, the cost is in drawing, for example large images, many layers, or shader compilation.
Using Frame Analysis to find the likely cause
With a frame selected, open the Frame Analysis tab. It breaks the frame into its UI and raster costs and lists the work that dominated the frame. Use it to narrow your search. It does not name the exact line of code, so use it to choose which part of the app to inspect next.
Inspecting a frame in Timeline Events
The Timeline Events tab lists the individual events that ran during the selected frame, in order, with their durations. Find the widest event that matches your app's code, such as a build of a specific widget. Compare that event with the frame budget. If one event takes most of the budget, you have found the cause.
Exercise
A selected frame takes 22 ms, and the UI thread bar is 18 ms long while the raster thread bar is 3 ms long. Which thread should you investigate first, and why?
Show answer
Investigate the UI thread first. It accounts for most of the frame's time, so the cost is in your Dart code, such as build, layout, or paint work. The raster thread bar is short, so drawing is not the main problem.