My favorites | Sign in
Project Logo
             
Search
for
Updated Sep 01, 2009 by robin.drew
Labels: Java, sizeof
Calculations  
How to calculate the size of a Java object

Primitives

Any 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;

Objects

Knowing 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;

Rules

Object Rules:

Field Rules:

Array Rules:


Examples

There 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


Sign in to add a comment
Hosted by Google Code