Shallow size

The shallow size of an object is the flat size, without children. The shallow size is defined by the sizes of all fields defined within the corresponding class and all fields defined in super-classes. It does not include the size of referenced children objects.

An object without any fields has a shallow size, because the object itself requires memory. The following example illustrates shallow size.

class A {
   float   fField;

class B extends A {
  byte   bField;
  int    iField;
  byte[] baField;
  String sField;
  boolean boolField;

The shallow size for an instance of B would be calculated as follows:

  • X bytes for the object itself (8 or 16 bytes, depending on VM).
  • 4 bytes for fField defined in A.
  • 1 byte for bField.
  • 4 bytes for iField.
  • 4 or 8 bytes for baField, depending on whether the VM is 32-bit or 64-bit. Arrays are objects.
  • 4 or 8 bytes for sField, depending on whether the VM is 32-bit or 64-bit.
  • 1 byte for boolField. A single Boolean field requires 1 byte.
  • Y bytes for alignment. The VM internally aligns objects to 8-byte borders.