我对HotSpot中堆上对象的布局相当熟悉,但对Android不太熟悉。
例如,在32位HotSpot JVM中,堆上的一个对象被实现为一个8字节的头,后面是对象的字段(一个字节用于boolean
,四个字节用于引用,以及预期的其他所有字段),按特定顺序排列(对于超类中的字段有一些特殊规则),并填充为8字节的倍数。
我做了一些研究,但找不到任何安卓系统的特定信息。
(我感兴趣的是优化一些非常广泛使用的数据结构,以最大限度地减少Android上的内存消耗。)
dalvik/vm/oo/Object.h
是您的朋友。struct Object
的评论说:
/* * There are three types of objects: * Class objects - an instance of java.lang.Class * Array objects - an object created with a "new array" instruction * Data objects - an object that is neither of the above * * We also define String objects. At present they're equivalent to * DataObject, but that may change. (Either way, they make some of the * code more obvious.) * * All objects have an Object header followed by type-specific data. */
java.lang.Class
对象是特殊的;它们的布局由CCD_ 6中的CCD_。数组对象很简单:
struct ArrayObject : Object {
/* number of elements; immutable after init */
u4 length;
/*
* Array contents; actual size is (length * sizeof(type)). This is
* declared as u8 so that the compiler inserts any necessary padding
* (e.g. for EABI); the actual allocation may be smaller than 8 bytes.
*/
u8 contents[1];
};
对于阵列,宽度以vm/oo/Array.cpp
为单位。布尔值的宽度为1,对象的长度为sizeof(Object*)
(通常为4),所有其他基元类型都有其预期的(压缩的)长度。
数据对象非常简单:
/*
* Data objects have an Object header followed by their instance data.
*/
struct DataObject : Object {
/* variable #of u4 slots; u8 uses 2 slots */
u4 instanceData[1];
};
DataObject
(所有非Class类实例)的布局由vm/oo/Class.cpp
中的computeFieldOffsets
控制。根据那里的评论:
/*
* Assign instance fields to u4 slots.
*
* The top portion of the instance field area is occupied by the superclass
* fields, the bottom by the fields for this class.
*
* "long" and "double" fields occupy two adjacent slots. On some
* architectures, 64-bit quantities must be 64-bit aligned, so we need to
* arrange fields (or introduce padding) to ensure this. We assume the
* fields of the topmost superclass (i.e. Object) are 64-bit aligned, so
* we can just ensure that the offset is "even". To avoid wasting space,
* we want to move non-reference 32-bit fields into gaps rather than
* creating pad words.
*
* In the worst case we will waste 4 bytes, but because objects are
* allocated on >= 64-bit boundaries, those bytes may well be wasted anyway
* (assuming this is the most-derived class).
*
* Pad words are not represented in the field table, so the field table
* itself does not change size.
*
* The number of field slots determines the size of the object, so we
* set that here too.
*
* This function feels a little more complicated than I'd like, but it
* has the property of moving the smallest possible set of fields, which
* should reduce the time required to load a class.
*
* NOTE: reference fields *must* come first, or precacheReferenceOffsets()
* will break.
*/
因此,超类字段排在第一位(和往常一样),后面是引用类型字段,后面是一个32位字段(如果可用,如果因为有奇数个32位引用字段而需要填充),然后是64位字段。后面是常规的32位字段。请注意,所有字段都是32位或64位(较短的基元被填充)。特别是,在这个时候,虚拟机不会使用少于4个字节来存储byte/char/short/boolean字段,尽管它在理论上肯定可以支持这一点。
请注意,所有这些都是基于在提交43241340
(2013年2月6日)时读取Dalvik源代码。由于虚拟机的这一方面似乎没有公开的文档,您不应该依赖它来稳定地描述虚拟机的对象布局:它可能会随着时间的推移而变化。