“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Today we’re going to take a look at how the memory view function is implemented, but what does it look like before we look at the code

So today I’m going to look at it in three main parts, VM Info and Memory, respectively Info and Class memory information, the main code for today is in the flutter_ume_kit_perf module, the page code is in the memory_info_page.dart, and the main logic is in the memory_service.dart module

VM Info

Get the VMService through ServiceWrapper

ServiceWrapper should have been seen yesterday, and the path was obtained from the VM information obtained from this custom class

Get VM through VMService,VM contains Pid, CPU, Verison information

Information in the VM

{type: VM, name: VM, architectureBits: 64, hostCPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60ghz, operatingSystem: Dev (dev) (Sun Aug 22 13:00:21 2021-0700) on "iOS_x64 ", pid: 2480, startTime: 1636098434474, isolates: [ {type: @Isolate, id: isolates/290036810778119, number: 290036810778119, name: main, isSystemIsolate: false} ], isolateGroups: [ {type: @IsolateGroup, id: isolateGroups/12365199645329027070, number: 12365199645329027070, name: main.dart, isSystemIsolateGroup: false} ], systemIsolates: [ {type: @Isolate, id: isolates/2845908153139319, number: 2845908153139319, name: vm-service, isSystemIsolate: true} ], systemIsolateGroups: [ {type: @IsolateGroup, id: isolateGroups/5190594318708735198, number: 5190594318708735198, name: vm-service, isSystemIsolateGroup: true} ] }Copy the code

Memory Info

The vm calls getMemoryUsage to obtain Memory allocation information, and returns bytes>0 and instances>0 from the Members traversal

{
type: MemoryUsage, 
externalUsage: 5668480, 
heapCapacity: 134545408, 
heapUsage: 115805296
}
Copy the code

Class memory information

The vm getAllocationProfile method is used to obtain the allocation object information, the members attribute is used to obtain the heap information occupied by each class, and the list is sorted. Names in the list that do not start with ‘_’ are stored to display non-private classes

void _heapInfoList(List<ClassHeapStats> list) { allClasses = list; allClasses.sort((a, b) => b.accumulatedSize! .compareTo(a.accumulatedSize!) ); infoList = allClasses .where((element) => ! element.classRef! .name! .startsWith("_")) .toList(); }Copy the code

ClassHeapStats in Infolist stores the information we need, and ClassHeapStats contains the following fields

ClassRef: class information

AccumulatedSize: The number of bytes allocated to the class instance since the accumulator was last reset

BytesCurrent: indicates the current memory size

InstancesAccumulated: The number of class instances allocated since the last accumulator was reloaded

int? InstancesCurrent: indicates the number of current instances

AccumulatedSize, instancesAccumulated and classRef are shown in the list

The info details

The info detail page is used to retrieve the class id from the above classRef, iterating through fields and functions in the class

void getClassDetailInfo( String classId, Function(ClsModel?) completion) async { Class cls = await serviceWrapper.getObject(classId) as Class; ClsModel? _clsModel; if (cls.fields ! = null && cls.fields! .isNotEmpty) { List<Property> properties = []; List<String> functions = []; cls.fields? .forEach((fieldRef) { Property _property = Property(fieldRef.isConst, fieldRef.isStatic, fieldRef.isFinal, fieldRef.declaredType! .name, fieldRef.name); properties.add(_property); }); for (var fucRef in cls.functions!) { String? code; Obj func = await serviceWrapper.getObject(fucRef.id!) ; if (func is Func) { code = func.code! .name; if (func.code! .name! .contains("[Stub]")) { continue; } code = code! .replaceAll('[Unoptimized] ', ''); code = code.replaceAll('[Optimized] ', ''); functions.add(code); } } _clsModel = ClsModel(propeties: properties, functions: functions); }Copy the code

Ok, today’s source view is over here, as a student of Flutter, I hope you can give me more advice and discuss with me where there are problems