I've been wondering how much memory does an Object occupy on Android. There are numerous resources (like this) related to HotSpot JVM telling that an empty object takes 8 bytes and an empty array 12 bytes and that all objects are aligned to 8 byte boundary. Thus an object with no extra fields should take 8 bytes, the smallest object with at least one extra field – 16 bytes, an empty array – 16 bytes, right?

I've found no specific information about Dalvik on this matter and decided to figure it out by testing. Running the test had surprising results.

Few words about the method of calculation. Android's implementation of Object.hashCode() simply returns the pointer to the object casted to int. (seemed obvious and general, but [another surprise] as it turned out, it does NOT on HotSpot JVM for instance – run MemTest with HotSpot and see). So, I've used the simplicity of hashCode() on Dalvik to calculate the object size on Android by allocating two instances of the tested class in a row and the amount of the allocated space should be equal to the difference of their hashCode() values (assuming that it makes little sense for Dalvik to allocate those at completely random addresses). Just to be sure I've allocated always 4 objects in a row per test class, which delivered always the same difference of hashCode(). So, I believe there is little doubt about correctness of the method.

Here is the source code of the test:

public class MemTest {
    public static void run() {
        Object o1 = new Object();
        Object o2 = new Object();
        Object o3 = new Object();
        Object o4 = new Object();

        EmptyObject eo1 = new EmptyObject();
        EmptyObject eo2 = new EmptyObject();
        EmptyObject eo3 = new EmptyObject();
        EmptyObject eo4 = new EmptyObject();

        ObjectWithBoolean ob
Edit
Report