Why should you NEVER profile performance in debug mode?
Debug mode includes extra checks, assertions, and disables optimizations. This makes everything slower than release and produces inaccurate performance data. Always use --profile mode.
Profile and fix jank, slow builds, and memory issues with DevTools
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
A smooth Flutter app targets 60 frames per second (or 120 on high-refresh displays). When your app drops frames, users experience jank, which is the visible stutter or lag during animations and scrolling. Performance debugging involves identifying which frames are slow, understanding whether the bottleneck is in the UI thread or the raster thread, and applying targeted optimizations. Flutter DevTools provides a comprehensive Performance view that makes this diagnosis systematic.
1// ══════════════════════════════════════2// PERFORMANCE DEBUGGING WITH DEVTOOLS3// ══════════════════════════════════════45// ── Step 1: Run in Profile Mode ──6// NEVER profile in debug mode (it is too slow)7// flutter run --profile89// ── Step 2: Open DevTools Performance Tab ──10// In terminal press 'v' to open DevTools11// Or: VS Code → Run → Open DevTools → Performance1213// ── Step 3: Record a Timeline ──14// 1. Click "Record" in Performance view15// 2. Perform the slow action (scroll, navigate, animate)16// 3. Click "Stop"17// 4. Analyze the frame chart:18// - Green bar = frame rendered under 16ms (good)19// - Red bar = frame took too long (jank!)20// - UI thread = Dart code execution21// - Raster thread = GPU rendering2223// ── UNDERSTANDING THE FRAME BUDGET ──24// 60 FPS = 16.67ms per frame25// If UI thread > 16ms → your Dart code is too slow26// If Raster thread > 16ms → too many pixels to paint2728// ── Step 4: Find the Bottleneck ──29// Click on a red (slow) frame to see:30// - Which widgets were rebuilt31// - How long each build() took32// - Which painting operations were expensive
1// ══════════════════════════════════════2// COMMON PERFORMANCE PROBLEMS AND FIXES3// ══════════════════════════════════════45// ── PROBLEM 1: Unnecessary Rebuilds ──6// Symptom: Too many widgets rebuilding on every frame78// BAD: Entire tree rebuilds when counter changes9class MyPage extends StatefulWidget { ... }10class _MyPageState extends State<MyPage> {11int _counter = 0;1213@override14Widget build(BuildContext context) {15return Column(16children: [17ExpensiveHeader(), // rebuilds unnecessarily!18Text('$_counter'),19ExpensiveFooter(), // rebuilds unnecessarily!20],21);22}23}2425// FIX: Extract widgets, use const, or use Consumer/Selector26class _MyPageState extends State<MyPage> {27int _counter = 0;2829@override30Widget build(BuildContext context) {31return Column(32children: [33const ExpensiveHeader(), // const prevents rebuild34Text('$_counter'),35const ExpensiveFooter(), // const prevents rebuild36],37);38}39}4041// ── PROBLEM 2: Heavy Work on UI Thread ──42// Symptom: UI freezes during computation4344// BAD:45void processData() {46final result = heavyComputation(bigDataSet); // blocks UI!47setState(() => _result = result);48}4950// FIX: Use compute() for isolate-based processing51import 'package:flutter/foundation.dart';5253void processData() async {54final result = await compute(heavyComputation, bigDataSet);55setState(() => _result = result);56}5758// ── PROBLEM 3: Expensive List Rendering ──59// BAD: Creates ALL items at once60ListView(61children: List.generate(10000, (i) => ListTile(title: Text('Item $i'))),62)6364// FIX: Use .builder for lazy rendering65ListView.builder(66itemCount: 10000,67itemBuilder: (_, i) => ListTile(title: Text('Item $i')),68)
1// ── PROBLEM 4: Memory Leaks ──2// Symptom: App gets slower over time, eventual crash34// Common causes:5// a) Stream subscriptions not cancelled6class _MyState extends State<MyWidget> {7late StreamSubscription _sub;89@override10void initState() {11super.initState();12_sub = myStream.listen((data) => setState(() => _data = data));13}1415@override16void dispose() {17_sub.cancel(); // ALWAYS cancel subscriptions18super.dispose();19}20}2122// b) AnimationController not disposed23// c) TextEditingController not disposed24// d) ScrollController not disposed25// Rule: If you create it in initState, dispose it in dispose()2627// ── PERFORMANCE CHECKLIST ──28// 1. Use const constructors wherever possible29// 2. Use ListView.builder instead of ListView with children30// 3. Avoid calling setState on the entire page31// 4. Move heavy computation to isolates with compute()32// 5. Cache network images with CachedNetworkImage33// 6. Use RepaintBoundary around expensive custom painters34// 7. Profile in --profile mode, NEVER in debug mode35// 8. Dispose ALL controllers and subscriptions36// 9. Use the Performance Overlay to spot jank visually:37// MaterialApp(showPerformanceOverlay: true)3839// ── MEMORY DEBUGGING ──40// Open DevTools → Memory tab41// 1. Take a heap snapshot42// 2. Perform actions in the app43// 3. Take another snapshot44// 4. Compare to find objects that should have been freed45// 5. Look for growing collections, unreleased streams
Add showPerformanceOverlay set to true in your MaterialApp to display a real-time performance graph directly on the screen. The top bar shows raster thread timing and the bottom shows UI thread timing. Any bar that exceeds the green line is a dropped frame. This is the fastest way to spot jank without opening DevTools.
Assign a performance budget for each critical flow:
Feature-level budgets turn performance from guesswork into an enforceable quality target.
Profile and fix jank, slow builds, and memory issues with DevTools. Forms collect user input, and validation ensures the input is safe and useful.
Think in architecture, reliability, and team-level maintainability.
Show validation messages early and clearly, near the field that needs correction.
Most app trust problems start when invalid or incomplete input reaches backend systems.
Validated input at UI and domain layers protects business logic from invalid assumptions.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
Debug mode includes extra checks, assertions, and disables optimizations. This makes everything slower than release and produces inaccurate performance data. Always use --profile mode.
60 frames per second means each frame has approximately 16.67 milliseconds to complete both the UI thread (build/layout) and the raster thread (paint/composite).
async/await still runs on the UI thread. compute() spawns a separate Dart isolate for CPU-intensive work, keeping the UI thread free for smooth rendering.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue