Kevin Kevin - 4 months ago 32
Java Question

Android & Dalvik - Get the size of an object

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.

Answer

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.

Comments