Dart Heap Sampling Enters Perfetto: Flutter's Native Memory Profiling Gap Closes
This unifies Dart heap analysis with the rest of the Android performance pipeline, so a single Perfetto trace can correlate Dart allocation behavior with native threads, GC, and system scheduling. It also gives Google Play Console a way to see Dart-level memory footprints, which was previously a gap for Flutter apps.
Flutter PR #197182 adds `--enable-heapprofd` to the engine, letting Profile and Release builds feed Dart object allocations into Android Perfetto traces. Instead of pushing Perfetto communication into the Dart VM, the PR keeps the VM as a pure provider of heap sampling data via existing C APIs, and puts the Android-specific producer logic in the engine. Binary size trade-offs drove the choice: embedding full Perfetto DataSource would cost ~145 KiB, while reusing only the IPC layer costs ~80 KiB and a hand-rolled Proto-over-IPC approach ~45 KiB.
Sampling is statistical, not per-object. With a default interval of 512 KiB, it finds allocation hotspots, not exact heap sizes. The engine bridges Dart's sampling callbacks into a `TracedHeapProducer`, uses Bionic `backtrace()` on Android 13+ to capture native/Dart AOT stack PCs, and tries to symbolize them via ELF maps, Build IDs, and DWARF. The current prototype only runs on API 33+, and the quality of symbolization for AOT builds remains a question.
When it works, one Perfetto trace can show a `_GrowableList` allocation, its call stack, and the GC release that follows, next to system scheduling. That makes it possible to spot allocation churn and leak patterns without shuttling between Perfetto and DevTools. The PR also encodes surviving samples into Perfetto's `HeapGraph`, pointing toward Android 17's OOM profiling.
Keeping the Dart VM Android-agnostic is a sound architectural call; it avoids coupling the VM's runtime to one platform and isolates the platform-specific producer logic in the engine.
The sampling design is a trade-off between trace size and accuracy: it's a hotspot detector, not a heap profiler, so developers need to read the numbers as trends rather than exact counts.
The success of this integration hinges on symbolization of AOT Dart stacks; without readable method names, the call stacks in Perfetto will be PC addresses, which limits practical debugging on Release builds.