This is the in-memory storage layout for ordinary objects

MarkWord: Mainly used to store three types of object information (8 bytes in total)

① Lock information: related to the lock upgrade (record the lock status), no lock -> biased lock -> lightweight -> heavyweight

② Hash value (hashcode)

③ Age of the object: one of the criteria for judging the age of the object in the garbage collection process. (For example, the JVM’s default generation age will move to the old age when it reaches 15. Because the space used to store the age of the object is 4 bits, the largest number that can be represented is 15.

ClassPointer: Used to mark which class the object belongs to (4 bytes)

InstanceData: InstanceData. The size depends on the size of the member variable in the object. If there are no member variables, the default size is 0 bytes. (0 bytes)

Padding: Used to align bits so that the size of the entire object is an integer multiple of 8.

As shown above, markWord+ClassPointer+InstanceData=12 bytes. So in order to get to 8 multiples of 16, the padding is going to fill out 4 bytes.

That is, the default initial size of an ordinary object is 16 bytes.

Validation:

In the figure above, the number of bytes of InstaceData is not shown because the PeekObeject object has no member variables.

When I add a member variable of type int to PeekObeject, InstanceData becomes 4. Since markWord+ClassPointer+InstanceData=16, there is no Padding