What is the memory consumption of an object in Java?

前端 未结 12 2337
眼角桃花
眼角桃花 2020-11-22 03:20

Is the memory space consumed by one object with 100 attributes the same as that of 100 objects, with one attribute each?

How much memory is allocated for an object?<

12条回答
  •  花落未央
    2020-11-22 03:25

    Is the memory space consumed by one object with 100 attributes the same as that of 100 objects, with one attribute each?

    No.

    How much memory is allocated for an object?

    • The overhead is 8 bytes on 32-bit, 12 bytes on 64-bit; and then rounded up to a multiple of 4 bytes (32-bit) or 8 bytes (64-bit).

    How much additional space is used when adding an attribute?

    • Attributes range from 1 byte (byte) to 8 bytes (long/double), but references are either 4 bytes or 8 bytes depending not on whether it's 32bit or 64bit, but rather whether -Xmx is < 32Gb or >= 32Gb: typical 64-bit JVM's have an optimisation called "-UseCompressedOops" which compress references to 4 bytes if the heap is below 32Gb.

提交回复
热议问题