Description
An interesting feature provided by Android is the ability to programmatically trigger the collection of a heap snapshot from within a running application. This gives developers the ability to collect the current state of the heap immediately before and after performing some operation, allowing for analysis of changes to the heap that occurred as a result of that operation.
While DevTools already provides the ability to inspect the diff between two heap snapshots, making this analysis workflow technically possible, it requires developers to manually trigger the heap snapshot from within DevTools. This manual interaction leaves time for the application to further manipulate the contents of the heap (e.g., perform garbage collection, allocate additional objects, etc.) before the snapshot can be collected, adding noise to the snapshot diff.
It should be relatively simple to add support for programmatically triggering heap snapshots from Dart code through a new ‘collectHeapSnapshot()’ API in dart:developer
. This API would first check to see if any VM service clients were subscribed to the HeapSnapshot
stream to avoid performing this expensive operation when no developer tooling is connected. Then, the heap snapshot would be collected and streamed to tooling.
With the support of the DevTools team, these streamed snapshots would be automatically added to the DevTools Diff Snapshot
tool for viewing by the developer. An automatic snapshot event could also be added to the memory chart to give a visual indication as to when the snapshot was collected.