-
-
Save PlugFox/0a0154a20b82913d521359e7f7ffe527 to your computer and use it in GitHub Desktop.
dart print out memory usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:developer' as developer; | |
import 'package:intl/intl.dart'; | |
// https://pub.dev/packages/vm_service | |
import 'package:vm_service/vm_service.dart' show MemoryUsage, VM, VmService; | |
import 'package:vm_service/vm_service_io.dart' show vmServiceConnectUri; | |
Future<void> printHeapUsage() async { | |
MemoryUsage mem = await getMemoryUsage(); | |
print(NumberFormat.compact().format(mem.heapUsage)); | |
} | |
// Be sure to call service.dispose() otherwise each call will leak a socket descriptor (i.e., OS Error: Too many open files, errno = 24 on iOS). | |
Future<MemoryUsage> getMemoryUsage() async { | |
developer.ServiceProtocolInfo info = await developer.Service.getInfo(); | |
VmService service = | |
await vmServiceConnectUri(info.serverWebSocketUri.toString()); | |
VM vm = await service.getVM(); | |
String? isolateId = vm.isolates?.first.id; | |
MemoryUsage mem; | |
if (isolateId == null) { | |
mem = MemoryUsage(externalUsage: 0, heapCapacity: 0, heapUsage: 0); | |
} else { | |
mem = await service.getMemoryUsage(isolateId); | |
} | |
return mem; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment