Sub topic 3
Why profiling needs profile mode
This chapter explains why timings from a debug build mislead you, what profile mode changes, and how to pick the right device. It assumes you can build and run a Flutter app with flutter run, you know what a build method is, and you have DevTools available. It does not cover reading the frames chart. That comes in later chapters.
The three build modes
Flutter compiles and runs your app in one of three modes. Each mode makes a different trade between development speed, runtime speed, and diagnostic information.
| Mode | Command | Dart compilation | Assertions and debug checks | Performance tooling | Intended use |
|---|---|---|---|---|---|
| Debug | `flutter run` | JIT (just-in-time) | On | Full debugging, hot reload | Writing and fixing code |
| Profile | `flutter run --profile` | AOT (ahead-of-time) on devices | Mostly off | DevTools performance tracing stays available | Measuring performance |
| Release | `flutter run --release` | AOT | Off | None | Shipping to users |
JIT means the Dart virtual machine compiles code while the app runs. This is what makes hot reload possible. AOT means the code is compiled to native machine code before the app starts. This is what users get from the store.
Profile mode is close to release mode, with a small amount of instrumentation left in so that DevTools can still connect and record data.
Why debug timings mislead
Debug mode adds cost that your users never pay. Several effects combine, and each one pushes the numbers in the same direction: slower than reality.
- JIT compilation. Code that has not run yet must be compiled first. Early frames include that compile time, and the code is also less optimized than AOT output.
- Assertions. Flutter's framework contains many
assertstatements. They run only in debug mode. They check things such as widget constraints and keys, and they cost time on every build, layout, and paint. - Debug-only services. The inspector, hot reload support, and extra bookkeeping all run in debug mode.
- Unoptimized framework code. The same Flutter framework code runs slower under JIT than after AOT compilation.
The result is that a screen can look janky in debug mode and run smoothly in release mode. The reverse mistake is rarer but possible: some costs, such as shader compilation or real GPU limits, depend on the real hardware and are only visible when you measure the real build.
What profile mode keeps and removes
Profile mode is designed to answer one question: how does this app behave on a real build, while still letting me look inside?
- It uses AOT compilation on physical devices, so Dart code runs at close to release speed.
- It turns off the assertions and most debug checks.
- It keeps the service extensions and tracing hooks that DevTools needs. This is why the Performance view works.
- It does not support hot reload. You edit, then restart.
Tracing has a small cost. In practice the overhead is low enough that frame times match release mode closely, but it is not zero. Treat profile numbers as representative, not exact.
The diagram below shows how the three modes relate on two axes you care about: how close the timings are to what users see, and how much DevTools can show you.
Choosing the device
Profile mode on the wrong device still gives you the wrong answer. Follow these rules.
- Use a physical device. AOT profile builds are not supported on the iOS Simulator or Android emulator in the same way, and emulators use your computer's CPU and GPU, which are not like a phone's. On Android emulators and iOS simulators, Flutter does not offer profile mode for accurate profiling. Use a real phone or tablet.
- Use a device that is slow on purpose. Test on the weakest device your users are likely to have. A new flagship phone can hide problems that a four-year-old mid-range phone shows clearly.
- Profile the web and desktop separately. Profile mode exists on those targets too, but the timings and the rendering path differ. Do not carry numbers over from a phone.
- Keep the device in a steady state. Unplug thermal stress sources, close other apps, and avoid low-battery mode. A hot device lowers its clock speed and your timings drift.
Running in profile mode
First list the devices Flutter can see, then launch on the one you chose.
flutter devices
flutter run --profile -d <device-id>Replace <device-id> with the ID shown in the second column of the flutter devices output. When the app starts, the terminal prints a line with the address of the Dart VM service and a link to open DevTools. You will use that link in the next chapter.
If you run from an IDE, choose the profile run configuration instead of the default one. In Android Studio and IntelliJ this is "Run > Profile", and in VS Code it is the "Run > Start Without Debugging" option combined with a launch configuration that sets flutterMode to profile. A launch configuration for VS Code looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter (profile)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
}
]
}Seeing the difference yourself
The following widget does heavy work inside build(). This is a deliberate mistake that you will diagnose and repair in later chapters. For now, use it to see how much the mode matters.
import 'package:flutter/material.dart';
int slowSum() {
var total = 0;
for (var i = 0; i < 50000000; i++) {
total += i % 7;
}
return total;
}
class JankyText extends StatelessWidget {
const JankyText({super.key});
@override
Widget build(BuildContext context) {
// Expensive work inside build() runs on the UI thread on every rebuild.
return Text('Result: ${slowSum()}');
}
}Put JankyText on a screen and run it in debug mode. Then run it in profile mode on the same device. The loop in slowSum is plain Dart arithmetic, so the JIT-compiled debug build takes noticeably longer than the AOT-compiled profile build. The exact ratio depends on the device. What matters is that the two builds give different answers about how bad the problem is, and only the profile run tells you what your users would see.
Write down the numbers you get from each mode. A habit of recording the mode, the device, and the frame time next to every measurement makes later comparisons honest.
Common errors
- Profiling in debug mode by accident. This happens when you launch with the green "Run" button out of habit. Check the terminal for the
--profileflag, or look for the DEBUG banner on the screen. - Looking for hot reload. Profile mode does not support it. After you change code, stop and run again. Restart is the correct workflow here.
- Comparing numbers across devices or modes. A change from 14 ms to 9 ms means something only if both runs used the same mode on the same device in the same state.
- Trusting the first frames. The first frames after launch include startup work such as loading assets and warming caches. Navigate to the screen, let it settle, then record.
- Profiling on an emulator and assuming it carries over. It does not. See the device rules above.
Summary of the working model
Debug mode answers "is my code correct?" Release mode answers "what do users get?" Profile mode answers "why is it that way?" by giving release-like speed with tracing still attached. Every performance measurement in the rest of this tutorial assumes profile mode on a physical device.
Exercise
A teammate reports that a list screen drops frames, with 40 ms frames in the DevTools Performance view. They collected the data with flutter run on an emulator. Give two reasons the numbers cannot be trusted, and say what they should do instead.
Show answer
First, flutter run starts a debug build, which uses JIT compilation and runs assertions, so frames are slower than in a shipped app. Second, an emulator runs on the development computer's CPU and GPU, which do not behave like a phone's hardware. They should connect a physical device, ideally a lower-end one, run flutter run --profile -d <device-id>, and measure again.
Exercise
You change a widget, and you want to check whether the change lowered frame times. Name the mode you should use, the main feature you give up by using it, and the step you take after each code edit.
Show answer
Use profile mode. You give up hot reload. After each edit you stop the app and run it again with flutter run --profile -d <device-id>, then record the new frame times on the same device in the same state.