How Java Objects Are Created and Managed in the JVM
This article explains the JVM's process for creating Java objects, covering class loading checks, memory allocation strategies such as bump‑the‑pointer and free‑list, object header composition, instance data layout, alignment padding, and the ways references locate objects via handles or direct pointers.
1. Object Creation
When the JVM encounters a new instruction, it first verifies that the constant‑pool entry points to a class that has already been loaded, linked, and initialized; otherwise the class‑loading process is triggered.
After the class check passes, the JVM allocates memory for the new object. The required size is known after class loading, and the allocation is performed by carving out a block of that size from the Java heap. Two common allocation methods are used:
1. Bump‑the‑Pointer: Assuming the heap is contiguous, a pointer separates used and free memory; allocation simply moves the pointer forward by the object’s size.
2. Free List: When the heap is fragmented, the JVM maintains a list of free blocks and selects a sufficiently large block for the new object.
In concurrent scenarios, allocation must be thread‑safe. The JVM solves this either by synchronizing the allocation operation (using CAS with retry) or by assigning each thread a small pre‑allocated region called a Thread‑Local Allocation Buffer (TLAB).
After memory is allocated, the JVM zero‑initializes the fields (excluding the object header) and sets up the object header, which stores runtime data such as hash code, GC generation age, lock state, and other metadata.
Finally, from the JVM’s perspective the object exists; the Java program then runs 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) Object Header
The first part, called the Mark Word, holds runtime information like hash code, GC age, lock flags, biased lock thread ID, and timestamp.
The second part is the class pointer, which references the object’s class metadata, allowing the JVM to determine the object’s type.
(2) Instance Data
This region stores the actual fields defined in the class, i.e., the data the programmer works with.
(3) Padding
Padding may be added to satisfy alignment requirements; it carries no semantic meaning.
3. Object Access and Location
Java programs access heap objects via references stored on the stack. Two main access mechanisms exist:
(1) Handle Access
The JVM allocates a handle pool; a reference contains a handle that points to a structure holding both the object’s address and its type information.
(2) Direct Pointer Access
With direct pointers, the reference stores the actual memory address of the object, and the object layout must include any necessary metadata for this access.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.