Site Search:

memory usage of java objects

Back>

Memory usage of basic java types:


boolean 1 byte
byte 1 byte
char 2 bytes
int 4 bytes
float 4 bytes
long 8 bytes
double 8 bytes

object overhead 16 bytes
array overhead 24 bytes
Reference 8 bytes
Each object uses padding of a multiple of 8 bytes

Total memory usage of a data type value:

Primitive type: 1 byte for boolean or byte, 2 bytes for char, 4 bytes for int or float, 8 bytes for long or double.
Object reference: 8 bytes.
Array: 24 bytes + memory of each array entry.
Object: 16 bytes + memory for each instance variable + 8 if inner class (for pointer to enclosing class).
Padding: round up to multiple of 8 types.

Example


class example {
    int[] a = new int[8];
    String b = "abc";
    long timestamp c = 0L;
    boolean d = false;
}

For an object of class example, we calculate the total amount of memory usage as the following:

the memory usage = 16 bytes object overhead 
                              + 24 bytes array overhead + 4 bytes x 8
                              + 8 bytes reference
                              + 8 bytes long
                              + 1 byte boolean
                              +  7 bytes padding to round up to multiple of 8 bytes
------------------------------------------------------------------
                                  96 bytes