PrimitivesAny Java book will include the relative sizes of each of the basic types in one of its early chapters. The MemoryUtil class includes these as static constants: /** The number of bytes used to represent a byte. */
SIZE_OF_BYTE = 1;
/** The number of bytes used to represent a short. */
SIZE_OF_SHORT = 2;
/** The number of bytes used to represent an integer. */
SIZE_OF_INT = 4;
/** The number of bytes used to represent a long. */
SIZE_OF_LONG = 8;
/** The number of bytes used to represent a boolean. */
SIZE_OF_BOOLEAN = 1;
/** The number of bytes used to represent a char. */
SIZE_OF_CHAR = 2;
/** The number of bytes used to represent a float. */
SIZE_OF_FLOAT = 4;
/** The number of bytes used to represent a double. */
SIZE_OF_DOUBLE = 8; ObjectsKnowing the size of primitives is essential to understanding the size of any instance of an object. There are also two other key sizes associated with objects, the base size of any object (including arrays), and the size of a null pointer (field or object array element): /** The number of bytes used to represent a pointer field (unassigned/null). */
SIZE_OF_NULL_POINTER = 4;
/** The number of bytes used to represent an object (with no fields). */
SIZE_OF_OBJECT_INSTANCE = 8; RulesObject Rules: - The base size of any object is 8 bytes.
- The total size of any object is always a multiple of 8 bytes (rounded up).
Field Rules: - Each primive field occupies the size of the primitive value it contains (1, 2, 4 or 8 bytes).
- Each object field occupies 4 bytes (if it is not null, the value should be calculated as a separate object).
Array Rules: - A primitive array occupies the base of 8 bytes, + 4 bytes for the length of the array, + the length multiplied by the size of the primitive.
- An object array occupies the base of 8 bytes, + 4 bytes for the length of the array, + the length multiplied by 4 bytes (the size of the null pointer).
ExamplesThere is no better way to illustrate these rules than with a few quick examples Example 1 char[] array = new char[3];
// The base size of any object is 8 bytes
// + 4 bytes for the length (as array lengths are integers)
// + 2 bytes * 3 (2 bytes is the size of the primitive char, multiplied by the length of the array)
// = 8 + 4 + (2 * 3) = 18 bytes
// Round up to nearest multiple of 8 (+ 6 bytes)
// = 24 bytes Example 2 String xml = "<xml></xml>";
// The base size of any object is 8 bytes
// The field 'offset' is an int (+ 4 bytes)
// The field 'count' is an int (+ 4 bytes)
// The field 'hash' is an int (+ 4 bytes)
// The field 'value' is an object pointer (+ 4 bytes)
// The field 'value' has a value: char[11] (+ roundUp(8+4+(2*11)))
// = 8 + 4 + 4 + 4 + 4 + 40 = 64 bytes
// Round up to nearest multiple of 8 (+ 0 bytes)
// = 64 bytes Example 3 Boolean flag = new Boolean(false);
// The base size of any object is 8 bytes
// The field 'value' is a boolean (+ 1 bytes)
// 8 + 1 = 9 bytes
// Round up to nearest multiple of 8 (+ 7 bytes)
// = 16 bytes Example 4 Number number = new Double(2.0);
// The base size of any object is 8 bytes
// The field 'value' is a double (+ 8 bytes)
// 8 + 8 = 16 bytes
// Round up to nearest multiple of 8 (+ 0 bytes)
// = 16 bytes Example 5 Number number = new BigDecimal(2.0);
// The base size of any object is 8 bytes
// The field 'intCompact' is a long (+ 8 bytes)
// The field 'scale' is an int (+ 4 bytes)
// The field 'precision' is an int (+ 4 bytes)
// The field 'stringCache' is an object pointer (+ 4 bytes)
// The field 'intVal' is an object pointer (+ 4 bytes)
// 8 + 8 + 4 + 4 + 4 + 4 = 32 bytes
// The field 'intVal' has a value: BigInteger:
// The base size of any object is 8 bytes
// The field 'signum' is an int (+ 4 bytes)
// The field 'bitCount' is an int (+ 4 bytes)
// The field 'bitLength' is an int (+ 4 bytes)
// The field 'lowestSetBit' is an int (+ 4 bytes)
// The field 'firstNonzeroByteNum' is an int (+ 4 bytes)
// The field 'firstNonzeroIntNum' is an int (+ 4 bytes)
// The field 'mag' is an object pointer (+4 bytes)
// The field 'mag' has a value: int[1] (+ roundUp(8+4+(4*1)))
// 8 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 16 = 52 bytes
// Round up to nearest multiple of 8 (+ 4 bytes)
// 32 + 56 = 88 bytes
// Round up to nearest multiple of 8 (+ 0 bytes)
// = 88 bytes
Notes- These calculations were derived under Windows XP with the latest Sun JVM for Java 6. Different JVM implementations will not necessarily give the same results as these, although I believe most would be similar if not identical. Never the less the size should always be treated as a close approximation.
|