2

DDMS shows the smallest size of an object (i.e. an empty object) is 16 bytes in VM Heap tab. But struct Object is only 8 bytes in dalvik source code vm/oo/Object.h. Why is there a difference? How is that related to alignment issues?

fadden
  • 51,356
  • 5
  • 116
  • 166
Cyker
  • 9,946
  • 8
  • 65
  • 93
  • I would think there is an 8 byte allocation for housekeeping.Instances of an object on the dalvik heap don't just take up memory for their actual fields. Inevitably, they also require some "housekeeping" information, such as recording an object's class, ID and status flags such as whether the object is currently reachable, currently synchronization-locked etc. – Vrashabh Irde Aug 27 '13 at 12:54
  • 1
    Some more answers by Romain Guy and Chet Haase https://speakerdeck.com/romainguy/android-memories – Gaurav Vashisth Dec 13 '13 at 16:44

1 Answers1

4

Short answer: 8 bytes of overhead for any Object (class pointer + lock word), plus 4 or 8 bytes of overhead for the underlying dlmalloc-based heap allocation mechanism. All objects are aligned on 8-byte boundaries, so a 12-byte object will have 4 bytes of padding.

Longer answer.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • I think dlmalloc has some overhead for each allocated piece of memory. But how does DDMS take this into account when reporting memory usage? I guess the calculation should be done inside Dalvik but I haven't found the exact source code for this. – Cyker Aug 27 '13 at 21:18
  • `dalvik/docs/debugmon.html` (http://milk.com/kodebase/dalvik-docs-mirror/docs/debugmon.html) describes the protocol -- look for the HPSG chunk. `dalvik/vm/alloc/DdmHeap.cpp` generates the data; it computes the size of an object and then adds `HEAP_SOURCE_CHUNK_OVERHEAD` (4 bytes) to everything. This isn't correct for all objects but it serves as a reasonable approximation. – fadden Aug 27 '13 at 22:32