Java objects memory size

Here we try to analyze how much memory will consume Java objects. All numbers are given for 64-bit machines.

All numbers here are given for 64-bit JVMs.

Primitive types

Type Size, bytes
byte 1
char 2
short 2
int 4
long 8
float 4
double 8

boolean is 1 bit.

Size of objects

Generally, size of any Java object in memory is a sum of following:

  • object header
  • memory for primitive fields
  • memory for reference fields
  • padding, usually multiple by 8 byte :w

Object header

On 64-bit JVM object header has size 16 bytes and following form:

Memory for primitive types

Memory for primitive types is allocated according to their respective sizes (see first section of this doc)

Memory for reference fields

Each reference costs 8 bytes

Example

Consider following class:

class Person {
  String name;
  int age;
  boolean male;
}

Let’s calculate memory necessary for it:

16 bytes (object header) + 8 bytes (reference to String variable name) + 4 bytes (int) + 1 byte (boolean) + 3 bytes (for padding) = 32 bytes

Size of arrays

Now let’s talk about arrays. In Java each array is a object, and hence each array has a 16-bytes object header. Then array has integer length field, and this adds another 4 bytes. 3rd pard is array itself, I mean array cells, and memory required for it is calculated as number of these cells multiplied by size of each cell. If it is array of primitive types, this means that each cell size will be in accordance with Section 1 of this doc, if it is array of objects this means that each cell size will be 8 bytes.

But referene to an array goes as a reference to an object and takes 8 bytes.

Example 1. Arrays of primitives

Let’s consider following array: int[100]. 16 bytes (object header) + 4 bytes (length) + 400 bytes (100 ints) + 4 (padding) = 424 bytes.

Example 2. Arrays of booleans.

Let’s consider following array: boolean[10]. 16 bytes (object header) + 4 bytes (length) + 16 bytes (100 ints) + 4 (padding) = 40 bytes.

You see that it is quite not effective to use array of booleans, and for boolean only 1 bit is informational, and another 7 are wasted. Hence, if you need to use array of booleans please consider usage of BitSet for this.

Example 3. Array of objects.

Let’s consider following array: String[10]. 16 bytes (object header) + 4 bytes (length) + 80 bytes (10 references to String objects) + 4 (padding) = 104 bytes.

Useful links: