What is the size of the object in java

后端 未结 5 2004
半阙折子戏
半阙折子戏 2020-12-11 19:16

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         


        
5条回答
  •  抹茶落季
    2020-12-11 19:24

    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.

提交回复
热议问题