Sub topic 1
Measure the right build: debug, profile and release
Every number in this tutorial, such as download size, startup time and frame time, depends on the build mode you measured. A figure from the wrong mode can be off by a factor of two or more, and it can send you to fix things that don't matter. This chapter explains what each mode does and which one to use for each measurement. It also covers the mistakes that make results unreliable.
Prerequisites
This chapter assumes you can:
- Run a Flutter app and edit
pubspec.yaml. - Use the
fluttercommand line tool. - Read basic
async/awaitDart code. - Connect a physical Android or iOS device to your computer.
You don't need any knowledge of DevTools yet. Later chapters introduce it.
The three build modes
Flutter compiles your Dart code in one of three modes. The mode controls how the code runs and which developer tools are included in the app.
- Debug mode is for development. Dart code runs through a just-in-time (JIT) compiler. A JIT compiler turns code into machine instructions while the app runs, which makes hot reload possible. Assertions are on, and the app carries debugging and service extension support.
- Profile mode is for performance analysis. Dart code is compiled ahead of time (AOT), meaning it is turned into machine code before the app is installed. A small set of tracing and profiling hooks stays on, so DevTools can connect and record a timeline.
- Release mode is for shipping. Code is AOT compiled and optimized. Assertions, debugging hooks and service extensions are removed. This is the build that users download.
| Property | Debug | Profile | Release |
|---|---|---|---|
| Compilation | JIT | AOT | AOT |
| Hot reload | Yes | No | No |
| Assertions | On | Off | Off |
| DevTools profiling | Limited, distorted | Yes | No |
| Tracing and timeline | Yes | Yes | No |
| Runs on emulators and simulators | Yes | No | Android emulator: yes. iOS simulator: no |
| Representative of user speed | No | Close | Yes |
| Representative of download size | No | Close, with extra hooks | Yes |
Some details vary by platform and Flutter version. On the web, for example, profile and release both use an optimizing compiler. Check the Flutter documentation for your target if the table doesn't match what you see.
Why debug numbers mislead
A debug build is much slower and much larger than a release build. There are several reasons.
- The JIT compiler needs time to compile code on first use. That time adds to startup and can cause dropped frames the first time a code path runs.
- Assertions run on every check you wrote, and on many checks inside the framework.
- The app bundles the Dart runtime pieces that support hot reload and debugging.
- Debug builds don't tree-shake or optimize the way an AOT release does.
Because of this, a debug app can look slow while the same release app is fast. It is also possible for a debug build to hide a real problem, so never use it as a baseline. Treat debug mode as a tool for correctness, not for measurement.
Which mode to use for each measurement
Use this rule for everything that follows in this tutorial.
- App size: measure release builds. The
--analyze-sizeflag only works on release builds. - Startup time and frame time: measure profile builds on a real device. They behave like release builds and still let you collect a trace.
- Final confirmation: check the numbers again on a release build, because profile mode adds a little overhead for tracing.
Building in each mode
The flutter run command accepts a mode flag. Run it with a device connected.
flutter run --debug
flutter run --profile
flutter run --releaseWith no flag, flutter run uses debug mode. The flutter build commands work differently, because they build release mode by default. You can ask for another mode explicitly.
# Release is the default for build commands
flutter build apk
# Equivalent, but explicit
flutter build apk --release
# A profile build, for example to install and trace by hand
flutter build apk --profile
# A debug build (large and slow, only for debugging)
flutter build apk --debugThe same idea applies to flutter build appbundle and flutter build ios. Always write the mode flag in scripts and notes, so no one has to remember which default applies.
If you run flutter run --profile with no device attached, or only an emulator, the tool will refuse or pick an unsuitable target. Connect a physical device first.
Detecting the mode in code
Sometimes you want to confirm which mode a running app is in. The foundation library exposes three constants for this: kDebugMode, kProfileMode and kReleaseMode. Exactly one of them is true.
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
String buildModeName() {
if (kReleaseMode) return 'release';
if (kProfileMode) return 'profile';
return 'debug';
}
void main() {
runApp(
Directionality(
textDirection: TextDirection.ltr,
child: Center(child: Text('Running in ${buildModeName()} mode')),
),
);
}Run this with each of the three flags and the text changes. Debug-only code guarded by kDebugMode is removed from release builds by the compiler, because these constants are known at compile time. This is a handy way to keep logging or test hooks out of what you ship. It also means debug-only code never affects your size and startup numbers, as long as you measure in release or profile mode.
Measure on a real device
Profile mode is meant for real hardware. Emulators and simulators run on your computer's CPU and GPU, so their speed has no relation to a phone's. Mid-range and older phones are usually much slower than a developer laptop. If you only test on a fast machine, you will miss problems that most of your users see.
Pick one test device and keep it. A low or mid-range phone is a better choice than a flagship, because problems show up more clearly. Use the same device for all comparisons so differences come from your changes, not from hardware.
Making measurements repeatable
Even in the right mode, a single run can mislead. Follow these habits.
- Close other apps and keep the screen on. Background work and low battery change timing.
- Let the device cool down. A hot phone slows itself down to protect the hardware, which inflates timings.
- Separate cold and warm starts. A cold start launches the app from a fully stopped state. A warm start brings back a process already in memory. Force-stop the app before each cold start measurement.
- Run several times. Take at least five runs and compare the median, not the best or the worst.
- Don't attach a debugger or profile at the same time unless you are collecting a trace on purpose, since a connected tool changes timing a little.
- Keep the build inputs the same. Use the same Flutter version, the same dependencies and the same flags between the "before" and "after" builds.
Size is steadier than timing, but the same discipline applies. A release APK for one CPU type is not the same size as a universal APK that holds several. Later chapters cover that. For now, note the exact command you used so you can repeat it.
Common mistakes
- Measuring in debug mode. This gives a larger size and a slower start than users see. The fix is to switch to release for size and profile for timing.
- Profiling on an emulator. The tool may refuse profile mode there. Even when a run works, the numbers don't match a phone.
- Judging the first run after install. The first launch after installing can do extra work, such as setting up caches. Launch once, close the app, then measure.
- Comparing different devices or OS versions. A change of device can look like a gain or a regression.
- Reading the size of a debug APK as the "app size". A debug APK is far bigger than the release one that users download, so it only causes needless worry.
- Forgetting that profile has overhead. Profile mode keeps tracing hooks, so it reads slightly slower than release. Use profile to find causes and release to confirm the final number.
A first check to run now
Do this once to see the gap on your own project. The exact numbers will depend on your app.
- Run
flutter build apk --debugand note the size of the file inbuild/app/outputs/flutter-apk/. - Run
flutter build apk --releaseand note the size of the file in the same folder. - Compare the two. The release file is much smaller.
- Install the release APK on your test device and launch it from a stopped state. Repeat the same with
flutter run --profileto feel how profile mode behaves.
Keep these notes. In the next chapters you will use release builds as your size baseline and profile builds as your startup baseline.
Exercise
A teammate says "our app starts in 3 seconds" after running flutter run on an emulator with no extra flags. Name the two problems with this measurement and say what you would do instead.
Show answer
First, flutter run with no flag builds in debug mode, which uses JIT compilation and assertions, so it is slower than what users get. Second, an emulator runs on the computer's hardware, so its speed doesn't match a real phone. Instead, connect a physical device and run flutter run --profile (or install a release build), force-stop the app before each cold start, repeat at least five times, and report the median along with the mode and device.
Exercise
Which build command and mode do you use to measure the download size of an Android app, and why not a debug build?
Show answer
Use a release build, for example flutter build apk --release or flutter build appbundle. The --analyze-size flag only works on release builds. A debug build includes JIT and debugging support and skips optimizations, so its size is much larger than what users download and tells you nothing useful about it.