getSnapshot static method

Future<HeapSnapshotGraph> getSnapshot(
  1. VmService service,
  2. IsolateRef isolate, {
  3. bool calculateReferrers = true,
  4. bool decodeObjectData = true,
  5. bool decodeExternalProperties = true,
  6. bool decodeIdentityHashCodes = true,
})

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:

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;
}