Skip to content

Instantly share code, notes, and snippets.

@misTrasteos
Created December 19, 2021 00:27
Show Gist options
  • Save misTrasteos/bceee8b42e3aafcc962d67a97f56eee3 to your computer and use it in GitHub Desktop.
Save misTrasteos/bceee8b42e3aafcc962d67a97f56eee3 to your computer and use it in GitHub Desktop.

Shallow size

Jol$Patient1 object internals:
OFF  SZ                  TYPE DESCRIPTION                VALUE
  0   8                       (object header: mark)      N/A
  8   4                       (object header: class)     N/A
 12   4                   int Patient1.patientID         N/A
 16   1               boolean Patient1.exitusIndicator   N/A
 17   3                       (alignment/padding gap)
 20   4      java.lang.String Patient1.givenName         N/A
 24   4   java.time.LocalDate Patient1.birthDate         N/A
 28   4                       (object alignment gap)
Instance size: 32 bytes
Space losses: 3 bytes internal + 4 bytes external = 7 bytes total

Jol$Patient2 object internals:
OFF  SZ                  TYPE DESCRIPTION                  VALUE
  0   8                       (object header: mark)        N/A
  8   4                       (object header: class)       N/A
 12   4                   int Patient2.patientID           N/A
 16   2                  char Patient2.administrativeSex   N/A
 18   1               boolean Patient2.exitusIndicator     N/A
 19   1                       (alignment/padding gap)
 20   4      java.lang.String Patient2.givenName           N/A
 24   4   java.time.LocalDate Patient2.birthDate           N/A
 28   4                       (object alignment gap)
Instance size: 32 bytes
Space losses: 1 bytes internal + 4 bytes external = 5 bytes total

We can see there is some padding applied, so adding or removing fields may not increase or decrease object shallow size.

Deep size

Jol$Patient1@d6da883d footprint:
     COUNT       AVG       SUM   DESCRIPTION
         1        32        32   Jol$Patient1
         1        32        32   [B
         1        24        24   java.lang.String
         1        24        24   java.time.LocalDate
         4                 112   (total)


Jol$Patient1@61d47554d footprint:
     COUNT       AVG       SUM   DESCRIPTION
         1        32        32   Jol$Patient1
         1        40        40   [B
         1        24        24   java.lang.String
         3                  96   (total)

references set to Null do not increase deep size

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.openjdk.jol:jol-core:0.16
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
public class JolTest {
static class Patient1{
int patientID;
String givenName;
boolean exitusIndicator;
java.time.LocalDate birthDate;
}
static class Patient2{
int patientID;
String givenName;
boolean exitusIndicator;
java.time.LocalDate birthDate;
char administrativeSex;
}
public static void main(String... args) {
System.out.println(ClassLayout.parseClass(Patient1.class).toPrintable());
System.out.println(ClassLayout.parseClass(Patient2.class).toPrintable());
Patient1 p1 = new Patient1();
p1.givenName = "Art Vandelay";
p1.birthDate = java.time.LocalDate.now();
System.out.println(GraphLayout.parseInstance(p1).toFootprint());
Patient1 p2 = new Patient1();
p2.givenName = "My friend Bob Sacamano";
System.out.println(GraphLayout.parseInstance(p2).toFootprint());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment