getSnapshot static method
Requests a heap snapshot for a given isolate and builds a HeapSnapshotGraph.
Note: this method calls VmService.streamListen and VmService.streamCancel on EventStreams.kHeapSnapshot.
Set flags to false to save processing time and memory footprint by skipping decoding or calculation of certain data:
- calculateReferrersfor HeapSnapshotObject.referrers
- decodeObjectDatafor HeapSnapshotObject.data
- decodeExternalPropertiesfor HeapSnapshotGraph.externalProperties
- decodeIdentityHashCodesfor HeapSnapshotObject.identityHashCode
Implementation
static Future<HeapSnapshotGraph> getSnapshot(
  VmService service,
  IsolateRef isolate, {
  bool calculateReferrers = true,
  bool decodeObjectData = true,
  bool decodeExternalProperties = true,
  bool decodeIdentityHashCodes = true,
}) async {
  await service.streamListen(EventStreams.kHeapSnapshot);
  final completer = Completer<HeapSnapshotGraph>();
  final chunks = <ByteData>[];
  late StreamSubscription streamSubscription;
  streamSubscription = service.onHeapSnapshotEvent.listen((e) async {
    chunks.add(e.data!);
    if (e.last!) {
      await service.streamCancel(EventStreams.kHeapSnapshot);
      await streamSubscription.cancel();
      completer.complete(HeapSnapshotGraph.fromChunks(
        chunks,
        calculateReferrers: calculateReferrers,
        decodeObjectData: decodeObjectData,
        decodeExternalProperties: decodeExternalProperties,
        decodeIdentityHashCodes: decodeIdentityHashCodes,
      ));
    }
  });
  await service.requestHeapSnapshot(isolate.id!);
  return completer.future;
}