NOTE
7.16 Object Layout in Memory
Java objects store headers and instance data, with arrays also carrying length information. These notes compare ordinary-object and array layouts.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Object Composition
There are two kinds of objects: ordinary objects and array objects.

1.1. Object Header
- Mark Word
Stores three kinds of information: the object’s hashCode, GC information, and lock information.
- Lock information: the meaning of bits differs by state. The final three bits are related to locks: 001 represents the unlocked state, 00 represents a lightweight lock, 10 represents a heavyweight lock, and 101 represents a biased lock.

-
Class pointer: points to the class. For example, if I call
new Test(), this points to the address ofClass<Test>in the method area. -
Array length: for an array, this value represents the length of the array.
1.2. Instance Data
For example, if Test has an Integer val field, its value is stored here.
1.3. Padding
Ensures 8-byte alignment by adding padding as needed.
2. Experiment
- pom.xml
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.10</version>
</dependency>
- ObjectTest
public class ObjectTest
{
public static void main(String[] args)
{
Object object = new Object();
String s = ClassLayout.parseInstance(object).toPrintable();
System.out.println(s);
}
}
- Output

2.1. How Many Bytes Does an Object Occupy?
16 bytes.
3. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub