How Java Creates Objects: From Memory Allocation to Object Layout
This article explains the complete lifecycle of Java object creation, covering the JVM's handling of the new bytecode, class loading, memory allocation strategies such as pointer bumping and free lists, object header structure, layout, and tools for inspecting object memory.
Object Creation
Java creates objects using the
newkeyword. When the JVM encounters a
newbytecode, it first resolves the class reference in the constant pool and ensures the class is loaded; otherwise class loading is performed.
Object Creation Process
After class verification, the JVM allocates memory for the new object. The allocated memory (excluding the object header) is zero‑initialized, and then the object header is populated with metadata such as class pointer, hash code, GC generation age, and lock state.
Finally, the constructor (
<init>()) is invoked, possibly chaining to superclass constructors and initialization blocks, completing the Java object creation.
Object Memory Allocation
Memory allocation typically occurs on the heap. Two main strategies are used:
Pointer Bumping (Dump The Pointer) : When the heap is contiguous, a pointer is moved forward by the object size.
Free List : When the heap is fragmented, the JVM maintains a list of free memory regions and selects a suitably sized block.
The choice depends on whether the garbage collector can compact the heap (e.g., Serial, ParNew use pointer bumping; CMS uses a free list).
Concurrent Allocation
Concurrent allocation is made thread‑safe using either CAS with retry or Thread‑Local Allocation Buffers (TLAB). TLAB usage can be toggled with the
-XX:+/-UseTLABflag.
Object Memory Layout
In HotSpot, an object consists of three parts: Header , Instance Data , and Padding . The header contains the Mark Word, klass pointer, and (for arrays) length.
Mark Word (64‑bit)
The Mark Word stores hash code, lock information, age, and other flags. Its layout varies between normal, biased, and CMS‑related objects.
Klass Pointer
The klass pointer references the class metadata stored in the metaspace (since JDK 8).
Array Length
Array objects include an additional field in the header to record their length.
Instance Data
This region holds the actual fields defined in the class hierarchy.
Padding
Padding aligns the object size to an 8‑byte boundary, ensuring the start address satisfies alignment requirements.
Object Size Calculation
On a 32‑bit JVM, the header is 8 bytes (4 bytes for klass pointer + 4 bytes for Mark Word).
On a 64‑bit JVM without compressed oops, the header is 16 bytes (8 bytes each).
With compressed oops, the header is 12 bytes (4 bytes klass pointer + 8 bytes Mark Word). Array headers add length and alignment fields.
Static fields are not counted in the object size.
Inspecting Object Layout with JOL
JOL (Java Object Layout) is an open‑source tool that uses Unsafe, JVMTI, and the Serviceability Agent to print actual object layout, size, and references.
<code>import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
public class ObjectTest2 {
public static void main(String[] args) {
Object obj = new Object();
System.out.println(ClassLayout.parseInstance(obj).toPrintable());
System.out.println();
System.out.println(GraphLayout.parseInstance(obj).toPrintable());
System.out.println();
System.out.println(GraphLayout.parseInstance(obj).totalSize());
}
}
</code>Object Access Methods
Handle Access
With handle access, a reference points to a handle that stores both the object's address and its type information, allowing the reference to remain stable during GC moves.
Direct Pointer Access
Direct access stores the raw object address in the reference, eliminating an extra indirection and offering faster access, which HotSpot primarily uses (except for some collectors like Shenandoah).
Comparison
Handles provide stability at the cost of an extra pointer dereference, while direct pointers give speed but require updates when objects are moved.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.