flutter / devtools

Performance tools for Flutter
https://flutter.dev/docs/development/tools/devtools/
BSD 3-Clause "New" or "Revised" License
1.59k stars 326 forks source link

Why is the memory of heap capacity not equal to the heap of VM? #3349

Open makesimple opened 3 years ago

makesimple commented 3 years ago

Hi,I read many explaination about the "Allocated" item in memory tab of the devtools. The first explanation is "Allocated: Current capacity of the heap is typically slightly larger than total size of all heap objects." the second explaination from the comment in source code is:

  /// The amount of non-Dart memory that is retained by Dart objects. For
  /// example, memory associated with Dart objects through APIs such as
  /// Dart_NewFinalizableHandle, Dart_NewWeakPersistentHandle and
  /// Dart_NewExternalTypedData.  This usage is only as accurate as the values
  /// supplied to these APIs from the VM embedder or native extensions. This
  /// external memory applies GC pressure, but is separate from heapUsage and
  /// heapCapacity.
  int? externalUsage;

  /// The total capacity of the heap in bytes. This is the amount of memory used
  /// by the Dart heap from the perspective of the operating system.
  int? heapCapacity;

  /// The current heap memory usage in bytes. Heap usage is always less than or
  /// equal to the heap capacity.
  int? heapUsage;

The above comment is from : third_party/dart/pkg/vm_service/lib/src/vm_service.dart

The second one solved a lot of my confusion,but I still have a last confusion that is why the memory of heap capacity is not equal to the heap of VM?

for example ,see my screeshot,the first one is memory tab, the second is the VM. screenshot-127 0 0 1_9100-2021 09 09-14_22_23 screenshot-127 0 0 1_9100-2021 09 09-14_21_37

According to the VM tab, the total memory is 44.0 + 72.8 + 16.8 = 133.6 ACcording to the memory tab, the Allocated is 253.45

the delta is 253.45 - 133.6 = 119.85, it's a big gap among them, how to explain it?

makesimple commented 3 years ago

I found another contradiction: According to the above comment, heapCapacity don't include the externalUsage, but the code from devtools' source:

final HeapSample sample = HeapSample(
  time,
  processRss,
  // Displaying capacity dashed line on top of stacked (used + external).
  capacity + external,
  used,
  external,
  fromGC,
  adbMemoryInfo,
  eventSample,
  rasterCache,
);

the snippet was copied from the memory_protocol.dart, in MemoryTracker._recalculate function,it tells me that the capacity includes the external (according to the comment, external is the native, and HeapSample.capacity is the "Allocated" in memory hover card). This confuses me.

kenzieschmoll commented 3 years ago

@terrylucas and @bkonyi - can you add any explanation here?

kenzieschmoll commented 3 months ago

@polina-c this is an old issue, but do you know if this discrepancy still exists or was this memory calculation logic something you fixed during your time working on the DevTools memory tooling?

polina-c commented 2 months ago

Calculations definitely changed. I suggest to close the issue. If there are issues, they are different now.