Java.Collections.What is Object header ?

Every object in Java has an object header — a block of metadata that the JVM attaches to every object on the heap.

It’s not part of your fields or values — it’s internal data used by the JVM for:

  • 🔁 Object identity (hashCode())
  • 🧹 Garbage collection
  • 🔐 Synchronization (e.g., when using synchronized)
  • 📏 Type information

📐 How big is the object header?

Depends on the JVM and system architecture:

JVM ArchitectureObject Header Size
32-bit JVM8 bytes
64-bit JVM12 bytes (with compressed oops) ✅
64-bit JVM (no compression)16 bytes ❌

🧠 Breakdown (on 64-bit JVM with compressed oops):

  • 8 bytes for the Mark Word
    • Stores hash code, GC flags, locking info, etc.
  • 4 bytes for the Class Pointer
    • Points to the object’s class metadata (type info)

So total = 12 bytes

Then, the JVM adds padding to align the object size to 8-byte boundaries.


🧪 Example:

class A {
    int x;
}

This tiny object:

  • 12 bytes (header)
  • 4 bytes (int field)
  • +4 bytes padding
    → total = 20 bytes (rounded to 24 due to alignment)

🧠 Why is this important?

  • Every object has this overhead — even empty ones!
  • So thousands/millions of small objects (like Node in LinkedList) → significant memory use

📌 Summary:

TermDescription
Object HeaderJVM metadata stored with every object
Typical Size12–16 bytes on 64-bit JVM
What’s Inside?Hash info, class pointer, lock state
You Can’t See ItBut it’s always there 😄
This entry was posted in Без рубрики. Bookmark the permalink.