Sub topic 10
Measuring startup with --trace-startup and DevTools
This chapter assumes you can run a Flutter app from the command line, you know the difference between debug, profile and release builds, and you are comfortable with async/await. You will learn how to take a repeatable startup measurement, read the result, and cross-check it in DevTools.
Why startup needs a measurement
"The app feels slow to open" is not something you can fix. A number you can compare before and after a change is. Startup time is also noisy. It varies with device temperature, background processes, whether the app was recently run, and whether the operating system has the app's files in cache.
So the goal of this chapter is a method, not a single number. You pick a build mode, a device, and a procedure, and you keep all three fixed while you change the code.
Pick the right build mode
Debug builds use a just-in-time (JIT) compiler and include assertions and debugging hooks. They start much slower than a shipped app and their timings do not reflect what users see. Release builds are what users get, but they strip out the timeline data that --trace-startup and DevTools rely on.
Profile mode sits between them. It is compiled ahead of time like release, so performance is close to release, but it keeps the tracing hooks. That is why startup measurement uses profile mode.
| Mode | Compilation | Tracing available | Use for startup numbers |
|---|---|---|---|
| Debug | JIT | Yes | No, timings are inflated |
| Profile | AOT | Yes | Yes |
| Release | AOT | No | Real-world feel only, no tracing |
Always measure on a physical device. Emulators and simulators run on your computer's CPU and storage, so their startup numbers say little about a mid-range phone.
Running --trace-startup
The command launches the app in profile mode, waits for the first frame, records timeline events, and then exits:
flutter run --profile --trace-startupWhen the run finishes, the tool writes the results to build/start_up_info.json. A typical file looks like this:
{
"engineEnterTimestampMicros": 96026,
"timeToFrameworkInitMicros": 3600,
"timeToFirstFrameRasterizedMicros": 33899,
"timeToFirstFrameMicros": 25415,
"timeAfterFrameworkInitMicros": 21815
}The values are in microseconds, so divide by 1000 for milliseconds. Your numbers will differ from the example, and the exact set of keys can vary between Flutter versions. Open your own file to see which keys it contains.
Reading the fields
Each field marks a point on the startup path.
| Field | What it measures |
|---|---|
| `engineEnterTimestampMicros` | When the Flutter engine started, as a timestamp. |
| `timeToFrameworkInitMicros` | Time from engine start until the Dart framework finished initializing. |
| `timeAfterFrameworkInitMicros` | Time from framework initialization to the first frame being built. Your `main()` and first screen build live here. |
| `timeToFirstFrameMicros` | Total time from engine start to the first frame being built. |
| `timeToFirstFrameRasterizedMicros` | Total time until that first frame was rasterized, meaning actually drawn. |
The field you can influence most is timeAfterFrameworkInitMicros, because it covers your own code: everything main() does before runApp, and the first build of your widget tree. Engine start is mostly outside your control.
Note what the numbers do not include. The timer starts when the engine starts, not when the user taps the icon. Time spent by the operating system loading your process and creating the activity or view comes before it. The numbers are therefore a lower bound on what the user experiences.
Making the measurement repeatable
A single run is not a measurement. Follow a fixed procedure:
- Use the same device, plugged in or at the same battery level, with the screen on.
- Close other apps and let the device sit idle for a minute so it is not hot.
- Run the command at least five times.
- Record
timeToFirstFrameRasterizedMicrosfrom each run. - Compare the median, not the best or the average.
You can automate the collection. This script runs the command several times and prints the median of one field. It assumes jq is installed and one device is connected.
#!/usr/bin/env bash
set -euo pipefail
RUNS=5
values=()
for i in $(seq 1 "$RUNS"); do
flutter run --profile --trace-startup > /dev/null
v=$(jq '.timeToFirstFrameRasterizedMicros' build/start_up_info.json)
values+=("$v")
echo "run $i: $v us"
done
printf '%s\n' "${values[@]}" | sort -n | awk '{a[NR]=$1} END {print "median:", a[int((NR+1)/2)], "us"}'Cold, warm and hot starts
The numbers from --trace-startup describe a cold start: the process did not exist, and the engine and framework start from nothing. Users also experience other cases.
- Cold start: the process is created from scratch. This is the slowest and the one you can improve most through your own code.
- Warm start: the process may still exist but the UI was destroyed, so the app recreates its view.
- Hot start: the app returns from the background with the view intact. There is little work to do.
Optimize and measure the cold start. If it is fast, the others are fast too.
Cross-checking in DevTools
--trace-startup gives you totals. It does not tell you why a total is large. For that, open DevTools and look at the timeline.
- Run the app in profile mode on a physical device:
flutter run --profile. - Open DevTools from the link the tool prints, or from your IDE.
- Select the Performance page.
- Look at the frame chart and the timeline events for the first frames.
Things to look for:
- A long gap between the framework start and the first frame, which usually means slow work in
main()or a long firstbuild. - A very long first frame in the UI thread row, which points to heavy widget construction or synchronous work.
- A long first frame in the raster thread row, which can come from expensive graphics such as large images or shader work.
- Frames after the first that are also slow, which means you are measuring jank, not startup. Use the performance tools for that instead.
You can also add your own markers so your code shows up in the timeline. The dart:developer library provides Timeline.startSync and Timeline.finishSync:
import 'dart:developer';
import 'package:flutter/widgets.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Timeline.startSync('init_storage');
await initStorage();
Timeline.finishSync();
runApp(const MyApp());
}
Future<void> initStorage() async {
// Placeholder for opening a local database or reading preferences.
await Future<void>.delayed(const Duration(milliseconds: 50));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Directionality(
textDirection: TextDirection.ltr,
child: Center(child: Text('Hello')),
);
}
}A caveat: Timeline.startSync and finishSync are meant for synchronous sections. The await inside the block above means the interval spans an asynchronous gap, so the event can be misleading. For async work, use TimelineTask, or time the call with a Stopwatch and print the result in profile mode.
import 'package:flutter/foundation.dart';
Future<T> timed<T>(String label, Future<T> Function() work) async {
final watch = Stopwatch()..start();
final result = await work();
watch.stop();
debugPrint('$label took ${watch.elapsedMilliseconds} ms');
return result;
}You can then wrap each startup step, for example await timed('storage', initStorage);, and see which one dominates.
Common errors
- Measuring in debug mode. Numbers are several times larger than a shipped app. Always use
--profile. - Measuring in release mode. The trace file is not produced as expected, because release builds do not expose the timeline.
- Comparing runs across devices. A number from your laptop emulator and one from a phone cannot be compared.
- Reading one run. Noise from caching and temperature can exceed the change you are testing. Use the median of several runs.
- Blaming the first frame for later jank. If frames after the first are slow, startup is not the problem.
- Forgetting the pre-engine time. The OS start and any native splash screen happen before the engine timestamp, so the user's wait is longer than the trace says.
Using the result
Once you have a baseline, you know which part to attack. If timeAfterFrameworkInitMicros dominates, the next chapter's focus applies: keep main() and the first build lean. If the raster time dominates, look at the first screen's images and effects. Record the baseline in your repository notes so that later changes can be compared against it.
Exercise
You run flutter run --profile --trace-startup on your phone and get timeToFirstFrameRasterizedMicros of 1,200,000 on one run. A colleague changes main() and gets 1,050,000 on a single run. Can you conclude the change helped, and what would you do instead?
Show answer
No. One run per build is not enough, because caching, device temperature and background load can easily move the result by 150 ms. Use the same physical device, run each build at least five times, and compare the medians of timeToFirstFrameRasterizedMicros. Then look at timeAfterFrameworkInitMicros, since that is the field covering main() and the first build.
Exercise
Why is --trace-startup run in profile mode instead of debug or release mode?
Show answer
Debug mode uses JIT compilation and extra checks, so its timings are much larger than what users see. Release mode is accurate for performance but does not keep the tracing hooks the tool needs. Profile mode is compiled ahead of time like release and still exposes the timeline, so it gives realistic numbers that can be recorded.