Sub topic 4
Running in Profile Mode and Opening the Performance View
This chapter takes you from a working Flutter project to a live recording in the DevTools Performance view. You will pick a device, launch the app in profile mode, connect DevTools, and check that the connection works before you analyze anything.
What this chapter assumes
You should already be able to:
- Build and run a basic Flutter app with
flutter run. - Read a
build()method and tell aStatelessWidgetfrom aStatefulWidget. - Use a working Flutter SDK, which includes DevTools, with either a physical device or an emulator.
You do not need to know how to read the frames chart yet. The next chapters cover that. Here the goal is a trustworthy setup. A wrong setup produces numbers that look like data but mislead you.
Why profile mode matters here
Flutter has three build modes. Debug mode favors fast iteration, and release mode favors speed and size. Profile mode sits between them: it is compiled like release, but it keeps the hooks that tools need.
| Aspect | Debug | Profile | Release |
|---|---|---|---|
| Compilation | JIT (just in time) on mobile | AOT (ahead of time) | AOT |
| Assertions and extra checks | On | Off | Off |
| Timings representative of users | No | Yes | Yes |
| DevTools Performance view | Works, but numbers are inflated | Full support | Not available |
| Hot reload | Yes | No | No |
Debug-mode timings can be many times slower than what users see. A frame that looks like jank in debug mode may be fine in profile mode. The reverse is rare. So profile mode is the right place to measure, and release mode gives you no way to attach DevTools.
Step 1: Find a target device
List the devices Flutter can see, then launch on the one you pick:
flutter devices
flutter run --profile -d <device-id>The first command prints one line per device, including its ID. Typical output looks like this:
Found 2 connected devices:
Pixel 7 (mobile) • 2A101FDH2000XX • android-arm64 • Android 14 (API 34)
Chrome (web) • chrome • web-javascript • Google ChromePass the ID from the second column to -d.
Choose the device with care:
- Use a physical device when you can. Ideally it is a mid-range or older phone, because users on slower hardware hit the frame budget first.
- Emulators and simulators are poor timing sources. They run on your computer's CPU and GPU, so their numbers do not match a phone. The iOS simulator cannot run profile mode at all, and Flutter will tell you to use a physical device.
- Desktop targets work (macOS, Windows, Linux), and the numbers reflect your own computer.
- Web builds do not give you the native frames chart in the same way. This tutorial covers native targets.
Step 2: Give yourself something to measure
An idle app produces no interesting frames. The app below rebuilds a deliberately expensive widget each time you tap the button. Put it in lib/main.dart:
import 'package:flutter/material.dart';
int slowSum() {
var total = 0;
for (var i = 0; i < 50000000; i++) {
total += i % 7;
}
return total;
}
void main() => runApp(const MaterialApp(home: JankDemo()));
class JankDemo extends StatefulWidget {
const JankDemo({super.key});
@override
State<JankDemo> createState() => _JankDemoState();
}
class _JankDemoState extends State<JankDemo> {
int _taps = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile me')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Taps: $_taps'),
const SizedBox(height: 16),
JankyText(rebuild: _taps),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _taps++),
child: const Icon(Icons.add),
),
);
}
}
class JankyText extends StatelessWidget {
const JankyText({super.key, required this.rebuild});
final int rebuild;
@override
Widget build(BuildContext context) {
// Expensive work inside build() runs on the UI thread on every rebuild.
return Text('Result: ${slowSum()} (rebuild $rebuild)');
}
}The rebuild field matters. Without it, a parent rebuild could reuse a const instance and skip build(), and you would see no slow frames.
Step 3: Launch in profile mode
Run the app with the profile flag and your device ID:
flutter run --profile -d <device-id>The first profile build is slower than a debug build, because the tool compiles the whole app ahead of time. When the app starts, the terminal prints a line like this:
A Dart VM Service on Pixel 7 is available at: http://127.0.0.1:50123/AbCdEf=/
The Flutter DevTools debugger and profiler on Pixel 7 is available at:
http://127.0.0.1:9100?uri=http://127.0.0.1:50123/AbCdEf=/The first URL is the VM service, the channel DevTools uses to talk to your app. The second URL opens DevTools already connected to it. The port numbers and the token differ on every run.
In the terminal session, r (hot reload) does nothing useful in profile mode. To change code, stop with q and run again.
Step 4: Open DevTools
You have several routes. All of them end in the same DevTools UI.
| Route | How |
|---|---|
| Terminal | Open the DevTools URL that `flutter run` printed. |
| VS Code | With the app running, open the Command Palette and run **Open DevTools**, then choose the Performance page. |
| Android Studio or IntelliJ | Use the **Open DevTools** button in the Flutter Inspector or Flutter Performance tool window. |
| Standalone | Run `dart devtools`, then paste the VM service URL into the connect box. |
The standalone route is useful when you started the app somewhere else, for example from a CI script or a remote machine. Paste the VM service URL, not the DevTools URL.
Step 5: Orient yourself in the Performance view
Select the Performance tab. The view has a few regions you will use in later chapters.
- Flutter frames chart. This sits at the top and shows one pair of bars per frame, one for UI-thread work and one for raster-thread work. It starts recording as soon as DevTools connects.
- Frame Analysis tab. After you select a frame, this tab gives a summary and hints about likely causes.
- Timeline Events tab. This shows the detailed trace for the selected frame.
- Options. A menu or toolbar section holds toggles such as the performance overlay and the enhance-tracing options. Labels shift slightly between DevTools versions, so look for the nearest match.
Now check that the connection works:
- Leave the Performance view open and switch to the app on the device.
- Tap the floating action button three or four times.
- Look at the frames chart. New bars should appear for each tap, and some should stand out as taller than the rest.
If bars appear, your setup is working. If the chart stays empty, see the error table below.
Common setup errors
| Symptom | Likely cause | What to do |
|---|---|---|
| "Profile mode is not supported" on a simulator or emulator | Profile mode needs a physical device on some platforms, notably iOS | Switch to a physical device or a desktop target. |
| Frames chart is empty | DevTools is connected to an old session, or the app is idle | Check that the VM service URL matches the current run, then interact with the app. |
| Many frames look slow and tall | The app was launched in debug mode | Look for a "DEBUG" banner or a debug banner in the DevTools header, and relaunch with `--profile`. |
| Performance tab is missing or limited | Release build, or a web target | Relaunch on a native target in profile mode. |
| Connection drops after the app restarts | The VM service URL changed | Reconnect with the new URL from the terminal. |
Making the first recording reliable
A few habits keep the early data clean:
- Warm up first. The first frames after launch include one-time work, such as loading assets and building caches. Interact with the app for a few seconds, then start your measured action.
- Change one thing at a time. Close other heavy apps on the device and keep its power mode steady. Throttling during a run can create fake jank.
- Repeat the same action. Tap the button the same number of times in each run so you can compare runs.
- Clear old frames before each test. The Performance view has a clear control. Use it so that earlier frames do not mix into the new run.
You now have a live, profile-mode recording of an app with a known problem. The next chapter reads the frames chart bar by bar.
Exercise
Why is flutter run --profile on a physical device preferred over a debug run on an emulator when you want to judge whether a screen is fast enough?
Show answer
Profile mode compiles the app ahead of time and turns off assertions, so frame times are close to what users see in a release build, while DevTools stays connected. A debug run uses JIT compilation and extra checks, which inflate timings. An emulator also runs on your computer's CPU and GPU, so its timings do not match a real phone.
Exercise
You run flutter run --profile, open the DevTools URL, and select the Performance tab, but the frames chart stays empty after you tap the button. Name two things to check.
Show answer
First, check that the DevTools session points at the VM service URL of the current run, because a stale URL from an earlier run connects to nothing. Second, check that the app on the device is actually rebuilding or animating. An idle app produces no frames. Also confirm the target is a native platform in profile mode, since a release or web target limits the Performance view.
Exercise
After flutter run --profile is running, you edit slowSum() and press r. What happens, and how do you test the change?
Show answer
Hot reload is not supported in profile mode, so the edit does not take effect. Stop the session with q, run flutter run --profile -d <device-id> again, reconnect DevTools with the new URL, and repeat the same action.