跪拜 Guibai
← All articles
Frontend · Android · Flutter

Dart Heap Sampling Enters Perfetto: Flutter's Native Memory Profiling Gap Closes

By 恋猫de小郭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— PR #197182 adds `--enable-heapprofd` to the Flutter Engine, enabling Dart heap sampling in Profile and Release builds.
— The Dart VM's existing C API (via `dart_api.h`) remains the sampling source; the engine handles all Perfetto communication.
— Default sampling interval is 512 KiB; sampling is statistical and finds allocation hotspots, not exact heap sizes.
— The prototype only initializes on Android 13 (API 33) or higher because it relies on Bionic `backtrace()`.
— Binary size trade-offs: ~145 KiB with full Perfetto DataSource, ~80 KiB with IPC-only, ~45 KiB with self-implemented IPC.
— The engine registers `android.heapprofd`, `android.java_hprof`, and `android.java_hprof.oom` data sources via `/dev/socket/traced_producer`.
— Call stacks are captured via `backtrace()` and symbolized using ELF maps, Build IDs, and DWARF info; AOT symbolization quality is not yet guaranteed.
— Surviving sampled objects can be encoded into Perfetto's `HeapGraph`, aligning with Android 17's OOM profiling direction.
Conclusions

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.

Concepts & terms
heapprofd
Android's native heap profiler daemon that samples malloc/free and can emit data into Perfetto traces. Flutter's integration uses its standalone client to report Dart heap samples.
Perfetto
Android's system-wide performance tracing tool that collects CPU, memory, and scheduling data from the kernel and user-space processes into unified traces.
HeapGraph
A Perfetto data structure that encodes a snapshot of live heap objects, enabling graph-based analysis of object references and retention.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗