Sub topic 2
Choosing debug, profile, or release mode
This chapter assumes you can run a Flutter app on a device or emulator, read basic Dart, and know that DevTools connects to a running app through the Dart VM service. Chapter 1 covered launching DevTools. Here you will learn which build mode to run before you start measuring anything.
Why the mode matters
Flutter can compile and run the same app in three ways. Each way trades debugging power against speed. If you pick the wrong one, you can chase a problem that does not exist, or miss one that does.
A common mistake is to open the Performance view while running in debug mode, see dropped frames, and start "optimizing" code that is fast in a real build. The reverse mistake is to run in release mode and then wonder why the Widget Inspector and the debugger are unavailable.
The three modes
| Aspect | Debug | Profile | Release |
|---|---|---|---|
| Command | `flutter run` | `flutter run --profile` | `flutter run --release` |
| Dart compilation | JIT (just-in-time) | AOT (ahead-of-time) | AOT (ahead-of-time) |
| Hot reload | Yes | No | No |
| Assertions and `debugPrint` checks | Enabled | Disabled | Disabled |
| Performance | Slow, not representative | Close to release | Fastest |
| DevTools connection | Full | Most tools, limited debugging | None |
| Emulator or simulator | Supported | Physical devices only for mobile | Physical devices only for mobile |
| Main use | Correctness and layout | Performance and memory | Shipping, final checks |
Debug mode
Debug mode compiles Dart with a JIT compiler. This is what makes hot reload possible, because the VM can swap in new code while the app runs. It also turns on assert statements, extra checks in the framework, and service extensions that the Widget Inspector uses.
The cost is speed. Code is less optimized, and the framework does additional work to validate your widgets. Frame times in debug mode are usually much worse than what your users see. Treat them as unreliable for any performance conclusion.
Use debug mode for:
- Inspecting the widget tree and layout constraints.
- Setting breakpoints and stepping through code.
- Reading framework error messages, such as
RenderFlex overflowed. - Logging and checking application logic.
Profile mode
Profile mode compiles with AOT, as release does, so performance is close to what users get. It keeps just enough instrumentation for DevTools to connect and record timelines, CPU samples, and memory data. Assertions are off, and hot reload is not available.
Use profile mode for:
- The Performance view and frame analysis.
- The CPU Profiler.
- The Memory view.
- Any claim of the form "this screen is slow" or "this is faster after my change."
Profile mode on an emulator or simulator is not supported for mobile targets, because the numbers would reflect your computer rather than a phone. Use a physical device. On desktop and web, profile mode runs on the host you are already using.
Release mode
Release mode is what you ship. It strips debugging information, removes service extensions, and applies the strongest optimizations. DevTools cannot connect to it.
Use release mode to confirm behavior that only shows up in a real build, such as obfuscation effects, startup time, or a crash that does not happen in profile mode. You check results with logs, crash reports, and platform tools instead of DevTools.
Running profile mode and connecting DevTools
Start the app in profile mode on a connected device. Then start DevTools separately if you prefer a standalone window.
# Run the app in profile mode on a connected device
flutter run --profile
# Start DevTools on its own, then connect using the VM service URI printed by flutter run
dart devtoolsWhen flutter run starts, it prints a line with the VM service URI and a DevTools link. Open the link, or paste the URI into the connect box of a DevTools instance started with dart devtools. If you have more than one device attached, add -d <device-id>. Run flutter devices to list the IDs.
flutter devices
flutter run --profile -d <device-id>Replace <device-id> with an ID from the list.
What changes between modes in practice
The differences are easiest to see in code that behaves differently depending on the mode. Flutter exposes constants for this in package:flutter/foundation.dart.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const ModeBanner());
}
class ModeBanner extends StatelessWidget {
const ModeBanner({super.key});
String get modeName {
if (kReleaseMode) return 'release';
if (kProfileMode) return 'profile';
return 'debug';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(child: Text('Running in $modeName mode')),
),
);
}
}Run this with each of the three flags to confirm which mode you are in. The constants kReleaseMode and kProfileMode are compile-time values. The compiler can remove code behind them, so a branch guarded by kReleaseMode has no cost in a debug build and the reverse.
Assertions
Assertions run only in debug mode. A line such as the one below checks the condition in debug and does nothing in profile and release.
void setQuantity(int quantity) {
assert(quantity >= 0, 'Quantity must not be negative');
// In profile and release, a negative value passes through silently.
}This means that a bug can disappear in release because the check that caught it is gone, while the bad value still flows through your code. If a profile or release build behaves differently from debug, look at your assertions first.
Debug-only overlays and banners
In debug mode the framework shows a "DEBUG" banner on MaterialApp. It also enables performance overlays and inspector tools you can toggle. None of these appear in profile or release unless you turn them on in code.
Performance overlay as a quick check
Profile mode supports the performance overlay, which draws two graphs on screen: one for the UI thread and one for the raster thread. Turn it on through MaterialApp.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
showPerformanceOverlay: true,
home: const Scaffold(body: Center(child: Text('Overlay on'))),
),
);
}Run it in profile mode on a device. A bar that goes above the line in either graph is a frame that missed its deadline. The overlay is a quick on-device signal. For causes, move to the Performance view in DevTools.
Choosing a mode: decision criteria
Ask what you need to learn, then pick the cheapest mode that gives a trustworthy answer.
- Do you need to see widgets, constraints, or step through code? Use debug.
- Do you need to measure time or memory? Use profile on a physical device.
- Do you need to check what users receive? Use release, and verify with logs or crash reports.
Gotchas and failure modes
Trusting debug-mode performance
Debug builds can drop frames in code that runs smoothly in profile. If the Performance view shows jank in debug, reproduce it in profile before you change anything. If the jank disappears, there is nothing to fix.
Measuring on an emulator
An emulator uses your computer's CPU and often a software or desktop graphics path. Its frame times say little about a real phone. Mobile profile mode is blocked on emulators for this reason. Measure on the lowest-end physical device you support, because that is where problems show first.
Expecting hot reload in profile
Profile and release builds have no hot reload. After each code change, stop the app and run it again. Plan your measurements around this. Make one change, rerun, and record the result.
Shader compilation jank on first run
Some jank appears only the first time an animation runs, because the graphics engine compiles shaders on demand. This can show up in profile mode on a device and can be mistaken for a code problem. Run the animation a second time. If the first run is slow and later runs are smooth, the cause is one-time compilation rather than your build method. Newer rendering engines reduce this problem, but you should still compare first and repeated runs.
Forgetting that service extensions are gone in release
If you need the Widget Inspector or the debugger, release mode cannot give them to you. Switch to debug, reproduce the problem, and then return to release to confirm the fix.
Different behavior between modes
When a bug appears in only one mode, the usual causes are:
- An
assertthat guards a value in debug only. - Code behind
kDebugMode,kProfileMode, orkReleaseMode. - Timing differences, since debug is slower and can hide or reveal race conditions.
- Minification or obfuscation in release, which changes names in stack traces.
Check each in turn before suspecting the framework.
Which DevTools features need which mode
| DevTools tool | Debug | Profile | Release |
|---|---|---|---|
| Widget Inspector and Layout Explorer | Yes | Limited | No |
| Debugger | Yes | No | No |
| Logging view | Yes | Yes | No |
| Performance view | Connects, unreliable numbers | Yes, use this | No |
| CPU Profiler | Connects, unreliable numbers | Yes, use this | No |
| Memory view | Connects, inflated numbers | Yes, use this | No |
| Network view | Yes | Yes | No |
Profile mode keeps the inspector available in a reduced form, but you will get the best experience for tree and layout work in debug. Treat this table as a guide. Tool availability can change between Flutter versions, so check the DevTools screen when a tab is greyed out.
A short workflow for choosing
- Reproduce the problem in debug mode first. Most correctness and layout bugs show up there, and you get full tooling.
- If the symptom is speed, memory, or smoothness, move to profile mode on a physical device and reproduce it again.
- If the symptom disappears in profile, it was a debug artifact. Stop.
- Fix the issue and measure again in the same mode and on the same device.
- Do a final check in release mode before shipping, using logs or crash reports.
Exercise
A teammate opens the Performance view on an emulator running flutter run and reports that the product list drops many frames. What should you tell them to do before changing any code, and why?
Show answer
Rerun the app in profile mode on a physical device with flutter run --profile -d <device-id> and reproduce the scroll there. Debug mode uses JIT compilation and extra framework checks, and an emulator uses the computer's hardware, so neither gives representative frame times. Only if jank remains in profile mode on a real device is there a problem worth investigating.
Exercise
A negative quantity causes a crash in debug mode but silently produces wrong totals in release mode. Which language feature most likely explains the difference, and how can you confirm it?
Show answer
An assert that checks the quantity. Assertions run only in debug mode, so release lets the bad value through. Search the code for assert near the quantity handling, and confirm by running with flutter run --release and logging the value, or by adding explicit validation that runs in every mode.
Exercise
Which of the three modes can DevTools not connect to, and what do you use instead to investigate problems there?
Show answer
Release mode. Use logs, crash reports, and platform tools. To use DevTools, reproduce the problem in debug mode for correctness issues or profile mode for performance and memory issues.