5

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

5

5 Answers 5

7

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.
1
  • This is more accurate answer than what has been accepted. Jan 4, 2019 at 13:38
4

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.

5
  • silly question of the day: and what about method references? Are they in the object or not ? if so, for each method, we should add size of its reference.
    – BigMike
    May 11, 2012 at 10:16
  • 2
    @BigMike: generally, method references are part of the class object, not the instantiated objects. Of course, HotSpot-based VMs are free to complicate things up at their leisure...
    – thkala
    May 11, 2012 at 10:18
  • so basically at least a reference to the virtual table (can I use this term?) has to sum up to the object size or does the VM keeps completely separated object instances and classes ? - classes used improperly in my sentence -
    – BigMike
    May 11, 2012 at 10:20
  • @BigMike: IIRC, part of the header of each object is a reference to its corresponding class object. The method references are not duplicated for each new instance...
    – thkala
    May 11, 2012 at 10:22
  • Object allocation is 8-byte aligned in 32-bit and 64-bit JVMs. May 11, 2012 at 11:57
2

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.

1

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.

1
  • +1 I was just wondering how method references comes into play.
    – BigMike
    May 11, 2012 at 10:18
0

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.

1
  • 1
    The header of an object can take 8 bytes on a 32-bit JVM and 12 bytes on a 32-bit JVM Whats the difference then?
    – MaVRoSCy
    May 17, 2013 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.