就对象大小而言,Dalvik是否比HotSpot更需要内存?

2022-09-01 17:48:58

我一直在想一个对象在Android上占用了多少内存。有许多与HotSpot JVM相关的资源(像这样)告诉一个空对象需要8个字节,一个空数组需要12个字节,并且所有对象都与8字节边界对齐。因此,没有额外字段的对象应该占用8个字节,最小的对象至少有一个额外的字段 - 16个字节,一个空数组 - 16个字节,对吧?

在这个问题上,我没有找到关于Dalvik的具体信息,并决定通过测试来弄清楚。运行测试的结果令人惊讶

关于计算方法的几句话。Android的Object.hashCode()实现只是返回指向投射到int.的对象的指针(看起来很明显和通用,但事实证明,[另一个惊喜],它不在HotSpot JVM上 - 例如,使用HotSpot运行MemTest并查看)。因此,我使用Dalvik上hashCode()的简单性来计算Android上的对象大小,方法是连续分配两个测试类的实例,并且分配的空间量应该等于它们的hashCode()值的差异(假设Dalvik在完全随机的地址上分配这些实例没有多大意义)。只是为了确保我在每个测试类的行中始终分配4个对象,这总是提供相同的hashCode()差异。因此,我相信该方法的正确性几乎没有疑问。

以下是测试的源代码:

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 ob1 = new ObjectWithBoolean();
        ObjectWithBoolean ob2 = new ObjectWithBoolean();
        ObjectWithBoolean ob3 = new ObjectWithBoolean();
        ObjectWithBoolean ob4 = new ObjectWithBoolean();

        ObjectWithBooleanAndInt obi1 = new ObjectWithBooleanAndInt();
        ObjectWithBooleanAndInt obi2 = new ObjectWithBooleanAndInt();
        ObjectWithBooleanAndInt obi3 = new ObjectWithBooleanAndInt();
        ObjectWithBooleanAndInt obi4 = new ObjectWithBooleanAndInt();

        ObjectWithLong ol1 = new ObjectWithLong();
        ObjectWithLong ol2 = new ObjectWithLong();
        ObjectWithLong ol3 = new ObjectWithLong();
        ObjectWithLong ol4 = new ObjectWithLong();

        ObjectWith4Ints o4i1 = new ObjectWith4Ints();
        ObjectWith4Ints o4i2 = new ObjectWith4Ints();
        ObjectWith4Ints o4i3 = new ObjectWith4Ints();
        ObjectWith4Ints o4i4 = new ObjectWith4Ints();

        ObjectWith4IntsAndByte o4ib1 = new ObjectWith4IntsAndByte();
        ObjectWith4IntsAndByte o4ib2 = new ObjectWith4IntsAndByte();
        ObjectWith4IntsAndByte o4ib3 = new ObjectWith4IntsAndByte();
        ObjectWith4IntsAndByte o4ib4 = new ObjectWith4IntsAndByte();

        ObjectWith5Ints o5i1 = new ObjectWith5Ints();
        ObjectWith5Ints o5i2 = new ObjectWith5Ints();
        ObjectWith5Ints o5i3 = new ObjectWith5Ints();
        ObjectWith5Ints o5i4 = new ObjectWith5Ints();

        ObjectWithArrayRef oar1 = new ObjectWithArrayRef();
        ObjectWithArrayRef oar2 = new ObjectWithArrayRef();
        ObjectWithArrayRef oar3 = new ObjectWithArrayRef();
        ObjectWithArrayRef oar4 = new ObjectWithArrayRef();

        byte[] a0b1 = new byte[0];
        byte[] a0b2 = new byte[0];
        byte[] a0b3 = new byte[0];
        byte[] a0b4 = new byte[0];

        byte[] a1b1 = new byte[1];
        byte[] a1b2 = new byte[1];
        byte[] a1b3 = new byte[1];
        byte[] a1b4 = new byte[1];

        byte[] a5b1 = new byte[5];
        byte[] a5b2 = new byte[5];
        byte[] a5b3 = new byte[5];
        byte[] a5b4 = new byte[5];

        byte[] a9b1 = new byte[9];
        byte[] a9b2 = new byte[9];
        byte[] a9b3 = new byte[9];
        byte[] a9b4 = new byte[9];

        byte[] a12b1 = new byte[12];
        byte[] a12b2 = new byte[12];
        byte[] a12b3 = new byte[12];
        byte[] a12b4 = new byte[12];

        byte[] a13b1 = new byte[13];
        byte[] a13b2 = new byte[13];
        byte[] a13b3 = new byte[13];
        byte[] a13b4 = new byte[13];

        print("java.lang.Object", o1, o2, o3, o4);
        print("Empty object", eo1, eo2, eo3, eo4);
        print("Object with boolean", ob1, ob2, ob3, ob4);
        print("Object with boolean and int", obi1, obi2, obi3, obi4);
        print("Object with long", ol1, ol2, ol3, ol4);
        print("Object with 4 ints", o4i1, o4i2, o4i3, o4i4);
        print("Object with 4 ints and byte", o4ib1, o4ib2, o4ib3, o4ib4);
        print("Object with 5 ints", o5i1, o5i2, o5i3, o5i4);

        print("Object with array ref", new Object[]{oar1, oar2, oar3, oar4});

        print("new byte[0]", a0b1, a0b2, a0b3, a0b4);
        print("new byte[1]", a1b1, a1b2, a1b3, a1b4);
        print("new byte[5]", a5b1, a5b2, a5b3, a5b4);
        print("new byte[9]", a9b1, a9b2, a9b3, a9b4);
        print("new byte[12]", a12b1, a12b2, a12b3, a12b4);
        print("new byte[13]", a13b1, a13b2, a13b3, a13b4);
    }

    static void print(String title, Object... objects) {
        StringBuilder buf = new StringBuilder(title).append(":");
        int prevHash = objects[0].hashCode();
        int prevDiff = -1;
        for (int i = 1; i < objects.length; i++) {
            int hash = objects[i].hashCode();
            int diff = Math.abs(hash - prevHash);
            if (prevDiff == -1 || prevDiff != diff) {
                buf.append(' ').append(diff);
            }
            prevDiff = diff;
            prevHash = hash;
        }
        System.out.println(buf.toString());
    }

    /******** Test classes ******/

    public static class EmptyObject {
    }

    public static class ObjectWith4Ints {
        int i1;
        int i2;
        int i3;
        int i4;
    }

    public static class ObjectWith4IntsAndByte {
        int i1;
        int i2;
        int i3;
        int i4;
        byte b;
    }

    public static class ObjectWith5Ints {
        int i1;
        int i2;
        int i3;
        int i4;
        int i5;
    }

    public static class ObjectWithArrayRef {
        byte[] b;
    }

    public static class ObjectWithBoolean {
        boolean b;
    }

    public static class ObjectWithBooleanAndInt {
        boolean b;
        int i;
    }

    public static class ObjectWithLong {
        long l;
    }
}

以下是结果:

java.lang.Object: 16
Empty object: 16
Object with boolean: 16
Object with boolean and int: 24
Object with long: 24
Object with 4 ints: 32
Object with 4 ints and byte: 32
Object with 5 ints: 32
Object with array ref: 16
new byte[0]: 24
new byte[1]: 24
new byte[5]: 32
new byte[9]: 32
new byte[12]: 32
new byte[13]: 40

总结结果:

  • 8字节边界对齐与HotSpot相同,这是唯一相同的东西。

  • 普通对象至少为 16 个字节(热点上为 8 个字节)

  • 显然,一个空对象本身占用了12个字节(而在HotSpot上为8个字节),并且有4个额外字节的空间,直到对象大小从16个字节“跳转到”下一个24字节的边界。

  • 空数组的最小值为 24 个字节(而 HotSpot 上为 12 个字节)

  • 类似地,数组本身占用 20 个字节(HotSpot 上为 12 个字节),并且有空间容纳 4 个额外的数组数据字节,直到对象大小从 24 个字节“跳转到”下一个边界 32 字节。

补充:(回应Louis的建议)另一个压力测试表明,即使分配一百万个对象实例,任何两个实例之间的距离也永远不会小于16字节。这证明了对象之间潜在的8字节孔肯定是进一步分配的死空间,否则当大约一半的内存被分配给对象时,dalvik肯定也应该将它们中的一些放入“孔”中,并且压力测试将返回8,而不是16。

public static void run2() {
    int count = 1024 * 1024;
    Object[] arr = new Object[count];
    for (int i = 0; i < count; i++) {
        arr[i] = new Object();
    }
    int[] hashes = new int[count];
    for (int i = 0; i < count; i++) {
        hashes[i] = arr[i].hashCode();
    }
    Arrays.sort(hashes);

    int minDist = Integer.MAX_VALUE;
    for (int i = 1; i < count; i++) {
        int dist = Math.abs(hashes[i] - hashes[i - 1]);
        if (dist < minDist) {
            minDist = dist;
        }
    }
    System.out.println("Allocated "+ count + " Objects, minimum distance is "+ minDist);
}

与 HotSpot 相比,Dalvik 的对象最多需要 8 个字节数组需要 8-12 个字节,这是否正确?


答案 1

(是的,这是一个老问题,但结果有点有趣,所以我稍微戳了一下。

该方法需要制作对象的完整按位复制。要做到这一点,它需要知道一个物体有多大。如果你看一下,你会看到它对数组使用一种方法,对对象使用另一种方法。Object.clone()dvmCloneObject()

对于数组,它调用 ,它将数组长度乘以元素宽度(1、2、4 或 8),然后从对象的开头添加数组数据的偏移量。每个对象都有一个8字节的标头;数组具有 4 字节的宽度,并包含额外的 4 个字节的填充,以确保 64 位值正确对齐。因此,对于 的 5 元素数组,它将是 16 + 5 * 2。dvmArrayObjectSize()short

对于普通对象,它只使用类对象中的字段。这是由一个相当复杂的函数设置的,该函数称为 。该函数确保所有对象引用都排在第一位(因此GC在扫描时可以跳过较少),然后对所有64位字段紧随其后。为了确保 64 位字段正确对齐,它可能会向上移动其中一个 32 位基元字段以填充内容。(如果没有合适的 32 位字段,则只需获得 4 个字节的填充。objectSizecomputeFieldOffsets()

我应该补充一点:所有字段都是 32 位的,除了 和 ,它们是 64 位的。对象引用是 32 位的。longdouble

因此,要确切地说出一个非数组对象有多大是很棘手的,但一般来说,你采用8字节的对象标头,汇总其他字段的宽度,然后向上舍入到8个字节的下一个倍数 - 最后一个是因为所有对象都必须是64位对齐的。

这就是理论。为了在实践中看到它,我将其添加到:dvmCloneObject()

ALOGD("class=%s size=%d", clazz->descriptor, clazz->objectSize);

并看到logcat输出如下:

D dalvikvm: class=Ljava/util/Locale; size=24
D dalvikvm: class=Ljava/util/Date; size=16

区域设置有 4 个参考字段,日期有一个字段,因此这些值符合预期。long

理想情况下,这正是需要多少空间。但是,该对象分配有 ,这会再增加 4 或 (有时) 8 个字节的开销。因此,上述值所需的实际空间为 32 和 24,这与您的实验结果相匹配。mspace_calloc()


答案 2

我没有答案给你,但我可以建议几个地方,你可以在来源中寻找更多信息。

您可以查看 dalvik/vm/oo/Object.h 中的 DataObject 和 ArrayObject 结构。基于此,似乎空对象应该只占用 8 个字节,而空数组应该占用 12 个字节。这似乎与你的结果不符,尽管我不确定为什么。

您还可以查看类对象结构中 objectSize 字段的用法,以获取更多见解。快速搜索该字段的用法显示,dalvik/vm/alloc/Alloc.cpp 中的 dvmAllocObject 方法似乎负责为新对象分配内存。