23

As we all know Java 5 introduced the ability for Instrumentation to get the size of an object with ease. Is there such a method on Android and Dalvik?

The java.lang.instrument package is not available on Android.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
  • Check out this: http://www.javapractices.com/topic/TopicAction.do?Id=83 – slayton Jan 25 '12 at 20:39
  • Looks and feels like a crummy hack... I went with Samuel's answer. – Kevin Parker Jan 25 '12 at 20:45
  • Just curious... what is your actual use case for needing that? – Manfred Moser Feb 21 '12 at 21:07
  • There are tons of cases were I'd like to measure the size of an Object. In this case its a bitmap cache. I know I could "calculate" the size of the cache my self, as a bitmap is basically a 2D array of pixel values but I don't want to go down that route. – Kevin Parker Feb 22 '12 at 14:41
  • 2
    [Here](http://www.java2s.com/Code/Android/Development/Functionthatgetthesizeofanobject.htm) is a cleaner example I found. Still requires a serialization of sorts. – Samuel Jan 25 '12 at 20:27
  • There must be... I can't even consider that such a high level programming language isn't able to do this. Serializing it to check its size? I mean come on... – Kevin Parker Jan 25 '12 at 20:29
  • I would expect high level languages to be _less_ good at telling you the size of types. That's the point, no? – Louis Wasserman Jan 25 '12 at 23:30
  • No. I would expect it to know an objects freaking size. This isn't rocket science, oh wait... it is. – Kevin Parker Feb 21 '12 at 21:04
  • 1
    This is the size of the object after serialization, which includes headers, metadata, information about types, etc, not the size in memory. – Randy Sugianto 'Yuku' Feb 28 '12 at 18:56
  • Take a look at my answer from here, it counts the bytes of the object. http://stackoverflow.com/questions/14366410/how-to-get-the-in-memory-size-of-an-object-in-android-or-performance-benchmarks – Nativ Jul 04 '16 at 20:34

3 Answers3

16

For what it's worth, I have looked at the Dalvik VM source code and can not find any stable API to get the size of an Object. If you want to take a look yourself, the size of an object is stored in ClassObject::objectSize : size_t, see dalvik/vm/oo/Object.h.

There is, however, internal APIs to get the size of an Object. It is used by DDMS to report detailed information about object sizes. But, since the API is internal, it is likely to change between different versions of Android. Plus, the API is sends raw byte[] data around, and is client/server based and not a simple library call, so it will be extremely awkward to use. If you want to take a look, start in dvmAllocObject() in dalvik/vm/alloc/Alloc.cpp and the dvmTrackAllocation() call.

To sum it up: there is unfortunately not any readily usable, stable API to get the size of an Object in the Dalvik VM.

Martin Nordholts
  • 10,338
  • 2
  • 39
  • 43
  • Hit the jackpot , dvmAllocObject is the function called when new operator is used . As for the size , its a true thing , app users can get the size . – human.js May 02 '13 at 13:48
0

My idea to get the size of a ArrayList<String> on the heap was to serialize the object into a Byte-Array, where the lengths is the size.

I've used the SerializationUtils from Apache Commons Lang.

To serialize:

byte[] data = SerializationUtils.serialize((Serializable) arrayList);

The lenght of the data array should be approximately the size in bytes on the heap.

As mentioned, this requires Commons Lang library. It can be imported using Gradle:

api 'org.apache.commons:commons-lang3:3.5'

more ways mentioned here

Manuel Schmitzberger
  • 5,162
  • 3
  • 36
  • 45
-1

For this problem, it is better not to use a programatic approach but to use the Memory Analyzer Tool (MAT) from www.eclipse.org/mat/ It has both standalone version and plugin for Eclipse. With it you can create a snapshot of Dalvik heap and then you could sort the list of objects by their size or also by the size of their referenced objects (retained size). I used this exactly for troubleshooting a bitmap cache leaks.

WindRider
  • 11,958
  • 6
  • 50
  • 57