Created
January 21, 2022 00:17
-
-
Save devxpy/9889f54966ab7e8572cb7af7380b5ba3 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)); | |
} | |
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
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).