There is a better way to calculate the size of an object as a whole, since there is a specialized tool for it called JOL .
It is not entirely correct how you think about it. The total size of this object will be 40 bytes . Let's see where this space comes from:
12 bytes headers (8 bytes + 4 bytes, since there are two headers)
You thought it was 16 bytes (8 + 8), but there is a compressed oops option that is enabled by default. You can disable it via -XX:-UseCompressedOops , in which case the size of the headers will be 16 bytes .
4 bytes size of the array (arrays have an int size that is stored in headers) 4 bytes is the reference size of the Integer (1) 4 bytes alignment (since objects are 8 bytes aligned - this is the trick behind CompressedOops btw)
So far you have 24 bytes for the array.
Now you store an integer inside it, that is, an object, and thus:
12 bytes headers 4 bytes for the actual int value inside Integer
So the total size is 40 bytes .
source share