Dart Heap Sampling Enters Perfetto: Flutter's Native Memory Profiling Gap Closes
Wow, Flutter PR #197182: heapprofd integration finally entered the PR stage. Flutter has finally started to integrate Dart object heap memory allocation sampling into Android Perfetto's native analysis pipeline.
Previously, Perfetto could already analyze CPU scheduling, threads, and some native memory behavior of Flutter apps, but the main missing piece was Dart Heap support, especially the ability to analyze Dart object allocation behavior together with Android system-side performance events.
But this problem was mainly that Dart Heap data couldn't enter the system trace before. However, if it didn't, it wouldn't be easy to report to Google Play Console. So after discussing from April until now, it's finally starting to land.
This is not on the same level as the current Memory support in DevTools; previously DevTools could see Dart Heap, class instances, and GC, while Android's heapprofd can track native malloc/free, and ART has its own Java allocation sampling. But the problem is that Flutter's Dart objects are managed by the Dart VM, and the system side only sees native allocation, meaning it can't tell from which Dart call path _GrowableList or business objects were created. So the existing Perfetto memory profiling documentation also separates Native, ART allocation sampling, and Heap Dump into different tools.
Originally, the plan was to put the Perfetto communication layer into the Dart VM, but after discussion, an old problem surfaced: Release binary size would be inappropriate:
| Original approach | Binary increase | Approach |
|---|---|---|
| Use Perfetto high-level DataSource | ~145 KiB | Integrate more complete SDK abstraction, less VM code |
| Only use Perfetto IPC | ~80 KiB | Handle upper-layer data yourself, reuse communication layer |
| Only reuse Proto, implement IPC yourself | ~45 KiB | Smaller binary, but maintain more protocol code yourself |
Since Dart already exposes a C API for Heap Sampling, why should the VM maintain a whole Android Perfetto integration? Isn't it more layered to have the Flutter Engine call the existing interface and report via Android heapprofd's standalone client?
Yes, that's the sentence we've been waiting for:
- Enable
dart_include_sampling_heap_profilerwhen compiling the Release Engine - Call the existing sampling interface, then distribute
heapprofd_standalone_client.sotogether with the Engine - Even dynamic linking is possible, avoiding stuffing the entire communication implementation into the Engine
And the Dart VM already has sampling capabilities, so what the Engine needs to add is just the last mile. For Dart developers, the most interesting part is that here we don't need to handle a new Dart object sampler; for example, dart_api.h already leaves an entry for embedders:
Dart_RegisterHeapSamplingCallback: when an object is sampled, the callback gives Isolate, Isolate Group, class name, and object size; when GC reclaims this sample, a delete callback is calledDart_SetHeapSamplingPeriodandDart_EnableHeapSampling: control how many bytes are allocated on average per sample, and when to enable it; default sampling interval is 512 KDart_ReportSurvivingAllocations: read the currently alive sampled objects; can also run a full GC first, then report remaining samples
But note here: sampling does not record every object in the Dart Heap. For example, configuring a sample every 512 K means approximately one sample per that many bytes allocated, not that every object of size 512 K will be recorded. It is suitable for finding allocation hotspots; the sample count, surviving count, and the real Heap size are not the same thing.
Then under Release there is another compilation condition: the Dart VM implementation uses FORCE_INCLUDE_SAMPLING_HEAP_PROFILER to control sampling logic in Product builds. So it's not guaranteed that calling the C API will work; the Engine also needs to include the sampler at compile time. Therefore, the --enable-heapprofd added by the PR will simultaneously enable dart_include_sampling_heap_profiler.
This is also why I think it's more reasonable:
The Dart VM is responsible for telling the outside world "which Dart object is allocated when and when it is GC'd"; as for how the Android system starts sampling, transfers data, and writes to Perfetto, that should be handled by the platform-specific Engine. In this way, the Dart VM doesn't need to directly depend on Android, nor will it change the runtime architecture of other platforms for Android-specific features.
Looking at the current PR implementation, it adds DartHeapProfiler / DartHeapProfilerDelegate in the Engine as a bridge between the VM C API and platform implementation. Then through the Android-side TracedHeapProducer, it connects to /dev/socket/traced_producer, registers data sources like android.heapprofd, android.java_hprof, and android.java_hprof.oom, and sends the sampling records out via Perfetto's SharedMemoryABI.
This implementation is actually quite complex. For example, during sampling, the prototype gets the Dart class name and object size, then uses Android backtrace() to obtain the native/Dart AOT call stack PC, and through ELF mapping, Build ID, and additional DWARF unwind information, it tries to let Perfetto locate these allocation call paths.
After GC reclaims the samples, the Engine accumulates the freed amount and count. Whether it can be restored to the developer-familiar Dart method names depends on the AOT artifacts and the subsequent symbolization pipeline.
However, this prototype currently only initializes on Android 13 / API 33 and above, because its stack collection uses the Bionic backtrace() that is available from that version.
When it lands, consider a Flutter long list where the app's memory usage keeps rising as the user keeps scrolling. Previously you'd have to go back and forth between Perfetto and DevTools, and AI was also annoying. If unified, on Android devices with a Profile/Release Engine that includes sampling, you could let Perfetto start heap sampling, then continuously perform operations like scrolling, image loading, page transitions, and then analyze along a single path:
- Dart VM samples a
_GrowableListorUint8Listallocation - Engine records class name, size, and call stack
- Android Producer writes the sample into the system trace
- GC reclaims and supplements release records
- In Perfetto, compare allocation, release, and system scheduling changes during this period
If a call stack has large cumulative allocation but also large release after GC, you can suspect allocation churn and GC pressure due to frequent object creation.
And if the surviving sample count keeps increasing, you need to further investigate why objects remain in the Heap.
The PR also tries to encode surviving sampled objects into Perfetto HeapGraph, combined with Android 17's OOM profiling direction, which is indeed imaginative.
If we can stably obtain symbolizable Dart allocation stacks on Release devices, it will be much more practical.