Java variable sizes
SHALLOW, RETAINED, AND DEEP OBJECT SIZES
The shallow size of an object is the size of the object itself. If the object contains a reference to another object, the 4 or 8 bytes of the reference is included, but the size of the target object is not included.
The deep size of an object includes the size of the object it references. The difference between the deep size of an object and the retained memory of an object lies in objects that are otherwise shared.
Objects that retain a large amount of heap space are often called the dominators of the heap.
Type | Size |
---|---|
byte | 1 |
char | 2 |
short | 2 |
int | 4 |
float | 4 |
long | 8 |
double | 8 |
reference | 8 (on 32-bit Windows JVMs, 4) |
The reference type here is the reference to any kind of Java object—instances of classes or arrays. That space is the storage only forthe reference itself. The size of an object that contains references toother objects varies depending on whether we want to consider theshallow, deep, or retained size of the object, but that size also includessome invisible object header fields. For a regular object, the size of theheader fields is 8 bytes on a 32-bit JVM, and 16 bytes on a 64-bit JVM(regardless of heap size).For an array, the size of the header fields is 16 bytes on a 32-bit JVM ora 64-bit JVM with a heap of less than 32 GB, and 24 bytes otherwise.
Java Performance, 2nd Edition by Scott OaksPublished by O’Reilly Media, Inc., 2020