JVM Object Creation: Memory Allocation, Layout, and Access Explained
This article explains the JVM's process for creating a Java object, covering class loading checks, memory allocation strategies such as bump-pointer and free list, thread-safe allocation mechanisms, object header composition, instance data layout, padding, and the ways references locate objects via handles or direct pointers.
1. Object Creation
When the JVM encounters a new instruction, it first checks whether the class reference in the constant pool has been loaded, linked, and initialized. If not, the class loading process is triggered.
After the class is verified, the JVM allocates memory for the new object. The required size is known after class loading, and the JVM can obtain a contiguous block from the Java heap using two common strategies:
Bump‑Pointer (Pointer Bumping) : The heap is treated as a contiguous region; a pointer marks the boundary between used and free space. Allocating an object moves the pointer forward by the object’s size.
Free List : When the heap is fragmented, the JVM maintains a list of free memory blocks and selects a sufficiently large block for the new object.
In a multithreaded environment, allocation must be thread‑safe. The JVM uses either:
Synchronised allocation with CAS and retry to ensure atomic updates.
Thread‑Local Allocation Buffers (TLAB) that pre‑allocate a small region of the heap for each thread.
After memory is reserved, the JVM zero‑initialises the fields (excluding the object header) and fills the object header with metadata such as the class pointer, hash code, GC generation age, and lock information.
Finally, the Java program continues execution of the constructor to perform user‑defined initialization, completing the creation of a fully usable object.
2. Object Memory Layout
In HotSpot, an object’s memory consists of three parts: the header, instance data, and optional padding.
(1) Header
The first part, called the “Mark Word”, stores runtime data such as hash code, GC age, lock state, and bias information.
The second part is the class pointer, which links the object to its class metadata.
(2) Instance Data
This region holds the actual fields defined in the class.
(3) Padding
Padding aligns the object size to satisfy the JVM’s alignment requirements; it carries no semantic meaning.
3. Object Access
References on the stack point to objects on the heap. Two main access mechanisms are used:
(1) Handle Access
The JVM reserves a handle pool; a reference stores the handle’s address, and the handle contains both the object’s address and its type information.
(2) Direct Pointer Access
When using direct pointers, the reference holds the actual object address, and the object layout must include any necessary metadata for type identification.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
