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

Anil Kumar C
  • 1,604
  • 4
  • 22
  • 43
  • 2
    See http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object to know how to calculate the size of the object in memory – Teg May 11 '12 at 10:14
  • 1
    You can take a look at this: [Almost Same Question][1] [1]: http://stackoverflow.com/questions/7060141/what-determines-java-object-size – Konstantin V. Salikhov May 11 '12 at 10:14
  • Look at http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html it will help you to approximately measure size of your objects. – yatul May 11 '12 at 10:12
  • how can instrumentation help? – Thomas Uhrig May 11 '12 at 10:13
  • it contains method getObjectSize for estimating object size. Here is the example of usage: http://www.javamex.com/tutorials/memory/instrumentation.shtml – yatul May 11 '12 at 10:17

5 Answers5

8

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.
Esailija
  • 138,174
  • 23
  • 272
  • 326
Calin Andrei
  • 186
  • 2
  • 5
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.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 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 '12 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 '12 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 '12 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 '12 at 10:22
  • Object allocation is 8-byte aligned in 32-bit and 64-bit JVMs. – Peter Lawrey May 11 '12 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.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
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.

zch
  • 14,931
  • 2
  • 41
  • 49
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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 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 '13 at 14:21