Fundamentals 7 min read

Unveiling Java Object Headers: Inside the JVM Memory Layout

This article explains the three-part structure of a Java object in the HotSpot JVM—object header, instance data, and alignment padding—detailing the Mark Word and Klass Pointer fields, their bit‑level layout, and how they affect memory size, GC age, and locking behavior.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Unveiling Java Object Headers: Inside the JVM Memory Layout

Java Object Composition

In the HotSpot JVM, a real Java object consists of three parts: the object header, instance data, and alignment padding.

1. Object Header

The object header stores runtime data such as hashcode, type information, GC generation age, lock status flags, thread‑held lock, biased thread ID, and biased timestamp.

2. Instance Data

Instance data holds the class’s field information, including data from super‑classes and the object’s own fields.

3. Alignment Padding

Padding bytes ensure the object size is a multiple of 8 bytes, satisfying JVM alignment requirements.

Java Object Header Details

The header is divided into two parts: the Mark Word and the Klass Pointer.

Mark Word

The Mark Word stores runtime information such as hashcode, GC generation age, lock status, thread‑held lock, biased thread ID, and biased timestamp.

Its size equals the JVM word size: 4 bytes on a 32‑bit JVM and 8 bytes on a 64‑bit JVM.

Because space is limited, the Mark Word uses a flexible, non‑fixed data structure.

Lock (2 bits) : distinguishes four lock states; a fifth state appears due to the biased‑lock flag.

Biased Lock (1 bit) : indicates whether the lock is biased (1) or not (0).

Age (4 bits) : records how many GC cycles the object has survived; max value 15 (2⁴‑1), matching the JVM option XX:MaxTenuringThreshold.

hashCode : stored lazily; computed via System.identityHashCode() and moved to the monitor when the object is locked.

Thread ID : in biased mode, records the ID of the thread that holds the lock.

Epoch : biased‑lock timestamp used during CAS lock operations.

ptr_to_lock_record : pointer to the lock record on the stack for lightweight locks.

ptr_to_heavyweight_monitor : pointer to the heavyweight monitor when a lock is inflated.

Klass Pointer

This field stores a pointer to the class metadata (Klass) of the object, occupying one JVM word (4 bytes on 32‑bit, 8 bytes on 64‑bit).

Summary of Java Object Header

Analyzing the memory layout of a Java object reveals how to calculate its total size (header + instance data + padding), how the GC generation age is tracked via the Mark Word, and why the maximum GC age is 15 due to the 4‑bit age field. The header also contains several lock‑related fields that underpin synchronized behavior, lock inflation, and optimization.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMmemory layoutMark WordObject Header
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.