Sub topic 10
Measuring startup with `--trace-startup` and DevTools
Assume you can run a Flutter app from the command line, understand Dart async code, and know the difference between debug, profile, and release modes. This chapter uses profile mode because it provides performance data that is closer to release mode while keeping debugging and timeline tools available.
What startup time includes
Startup is the path from launching the process to displaying the first useful frame. Several phases contribute to that time:
- The operating system starts the app process.
- The Flutter engine loads native libraries and creates the rendering surface.
- The Dart runtime starts the root isolate.
main()performs synchronous work and waits for required asynchronous work.- Flutter builds the widget tree, lays out widgets, and paints the first frame.
- The operating system displays the rendered frame.
A slow phase can hide behind a single large “startup” number. Timeline events help you locate the phase that needs attention.
Capture a startup trace
Use a physical device when possible. Emulators are useful for repeatable development tests, but their startup behavior may differ from real hardware.
First stop any existing instance. Then run the app in profile mode:
flutter run --profile --trace-startupThe command launches the app and records startup timeline events. After startup completes, Flutter writes the trace summary to:
build/start_up_info.jsonThe file contains timing information for startup tasks and frame rendering. Keep the same device, operating-system state, app data, and test action when comparing two commits. Otherwise, environmental differences can look like code changes.
Do not use debug mode for performance conclusions. Debug mode performs extra checks and uses a different compilation strategy. Release mode is the best final target, but profile mode is usually more practical for investigation because it exposes profiling tools.
Inspect the trace in DevTools
Start DevTools for the profile session and open the Performance view. The timeline shows events arranged by thread and time. Look for long synchronous sections on the UI thread, expensive first-frame work, and repeated layout or build activity.
The UI thread must finish Dart work, widget building, layout, and painting before it can submit a frame. A long event there delays the first frame. Work on the raster thread can also delay display if painting or GPU operations are expensive.
| Observation | Likely cause | Investigation |
|---|---|---|
| Long gap before Dart events | Engine, process, or plugin startup | Compare devices and native plugins |
| Long synchronous section in `main()` | File, database, or service initialization | Move non-essential work after the first frame |
| Expensive first build | Large widget tree or heavy synchronous computation | Profile builds and simplify the initial screen |
| Raster thread is busy | Large images, shaders, or complex painting | Inspect images and custom painting |
Treat the first trace as a baseline, not as a diagnosis. Repeat the run several times and record the median time. One run can be affected by device state, background processes, or storage activity.
Build a repeatable check
Record a small set of measurements for each important build:
- Time to the first rendered frame.
- Time until the first screen is interactive.
- The slowest startup event.
- Device model, operating-system version, and build mode.
Set a startup budget, such as 1.5 seconds to the first useful screen on a specified test device. Run the same launch procedure in CI or on a scheduled performance device. Investigate a regression instead of silently accepting a larger number.
Exercise
Why should you use profile mode with --trace-startup instead of debug mode when measuring startup?
Show answer
Debug mode adds checks and uses different compilation behavior, so its timings do not represent normal application performance. Profile mode provides more realistic timings while retaining profiling support.