The size of instances of reference types, like
primitives, depends on the specific JVM implementation and its launch parameters. Typically, when this question is asked, it refers to the
most popular JVM - Oracle's HotSpot.
The size of primitive fields can be larger than necessary, for example, due to
alignment. Because of this alignment,
gaps may appear between fields in memory.
As mentioned
earlier, a reference in Java is not exactly the same as a pointer in C++; it’s not a memory address. Because of this, the size of a reference field might not match the size of a machine word. For example, HotSpot might use the
"Compressed OOP" optimization, which reduces the size of references.
In addition to fields and gaps, every object in HotSpot starts with a header containing runtime metadata. The header takes
up 8 to 16 bytes.
Generally speaking, it can only be said that the size of an object is strictly larger than the sum of its fields' sizes. The approximate size of a specific object can be measured using
instrumentation tools.