Anil Kumar C
Anil Kumar C

Reputation: 1624

What is the size of the object in java

As we all know that java uses the following data types

byte    Occupy 8 bits in memory
short   Occupy 16 bits in memory
int     Occupy 32 bits in memory
long    Occupy 64 bits in memory 

If I create a class like

class Demo{
    byte b;        
    int i;
    long l;
}

Demo obj = new Demo();

Now my question is obj size is < or > or = the size of b+i+l which is 104 bytes. Please give me the clarification with proper reason.

Thanks,
Anil Kumar C

Upvotes: 5

Views: 3609

Answers (5)

Calin Andrei
Calin Andrei

Reputation: 176

From http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

  1. a bare Object takes up 8 bytes;
  2. an instance of a class with a single boolean field takes up 16 bytes: 8 bytes of header, 1 byte for the boolean and 7 bytes of "padding" to make the size up to a multiple of 8;
  3. an instance with eight boolean fields will also take up 16 bytes: 8 for the header, 8 for the booleans; since this is already a multiple of 8, no padding is needed;
  4. an object with a two long fields, three int fields and a boolean will take up:
    • 8 bytes for the header;
    • 16 bytes for the 2 longs (8 each);
    • 12 bytes for the 3 ints (4 each);
    • 1 byte for the boolean;
    • a further 3 bytes of padding, to round the total up from 37 to 40, a multiple of 8.

Upvotes: 7

Peter Lawrey
Peter Lawrey

Reputation: 533432

The header of an object can take 8 bytes on a 32-bit JVM and 12 bytes on a 32-bit JVM.

Each primitive takes the number of bits (not bytes you indicate)

Object allocation is 8 byte aligned so there is up to 7 bytes of padding at the end of a object. i.e. the space actually used is rounded up to the next multiple of 8.

class Demo{ // 8 or 12 bytes
    byte b; // 1 byte
    int i;  // 4 bytes
    long l; // 8 bytes
}

Demo obj = new Demo();

So the size of the object can take 24 bytes on a 32-bit JVM and 32 bytes on a 64-bit JVM.

Upvotes: 0

zch
zch

Reputation: 15278

First, you confused bits and bytes.

Second, it will also need pointer to "vtable", where information about its class is stored. It will, most likely, be 4 bytes (32 bits) on 32bit systems and 8 bytes on 64bit sytems.

Finally, note that due to memory fragmentation, total program memory might be higher than sum of all objects.

Upvotes: 1

alain.janinm
alain.janinm

Reputation: 20065

Hard to say that will be the size of obj in memory, type size indication help the developers but actually in the memory it's a bit different. I advise you to read this article, it's really interesting.

Upvotes: 2

thkala
thkala

Reputation: 86323

The in-memory size of the object depends on the architecture, mainly on whether the VM is 32 or 64-bit. The actual VM implementation also matters.

For each object, you need space for its object header (typically 2*8 bytes on 64-bit VMs), its fields (extra space for alignment depending on the VM implementation). The final space is then rounded up to the nearest multiple of the word size.

Upvotes: 4

Related Questions