skip to content

On a 64-bit HotSpot JVM with default settings, why does `new Object()` take up heap memory even though it declares no fields?

level: juniorimportance: should knowfreq 45%

answer

  1. every object carries a header
  2. mark word plus class word
  3. 8 + 4 bytes before padding
  4. sizes round up to 8 bytes

basics

~20 s

Every HotSpot object starts with a header: an 8-byte mark word for hash, GC age and lock state, and a 4-byte compressed class pointer. Those 12 bytes round up to 8-byte alignment, so new Object() costs 16 bytes on JDK 25.

solid answer

~40 s

Every object on the heap carries a **header** before its fields. On a 64-bit HotSpot JVM running JDK 25 with default settings the header has two parts: an 8-byte **mark word**, holding the identity hash once one is computed, the GC age and the lock state, and a 4-byte compressed **class word** pointing at the class's metadata, which the JVM needs for type checks, virtual calls and to know the object's size and where its references are. That is 12 bytes, and every object size is rounded up to a multiple of `ObjectAlignmentInBytes`, 8 by default, so `new Object()` occupies 16 bytes: 12 of header and 4 of padding. With compact object headers (`-XX:+UseCompactObjectHeaders`, opt-in on JDK 25) the header is a single 8-byte word and the empty object takes 8 bytes.

code

java · 8 lines
java
import org.openjdk.jol.info.ClassLayout;

public class EmptyObjectSize {
    public static void main(String[] args) {
        // Prints the mark word, the class word and the alignment gap of a plain Object
        System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());
    }
}

go deeper

for a junior

Recall that every object carries a header, a mark word plus a class word, and that new Object() takes 16 bytes on a default 64-bit JDK 25 JVM.

for a middle

Explain where the 16 comes from: an 8-byte mark word, a 4-byte compressed class pointer and 4 bytes of alignment padding that a small first field can reuse.

for a senior

Tie header cost to footprint: for millions of tiny objects the header can rival the payload, and the JVM mode, especially compact object headers, changes the number.

for a principal

Weigh per-object overhead when a team chooses between many small objects and fewer, larger or array-backed structures for memory-heavy services.

## Every object starts with a header A Java object is not just its fields. Before the first field, the HotSpot JVM writes an **object header**: bookkeeping the runtime needs for every object, whatever its class. An object with no instance fields, such as `new Object()`, is nothing but that header plus alignment padding, which is why it still occupies heap memory. The header exists because the JVM has to answer a few questions about any object at any moment: - **What type is it?** Casts, `instanceof`, virtual method calls and array-store checks all need the object's class. - **How big is it, and where are its references?** The garbage collector walks the heap object by object and must know each object's size and which of its slots hold references. That information lives with the class, so the header points at the class. - **What is its identity hash?** `System.identityHashCode` (and an `Object.hashCode()` that is not overridden) must return the same value for the object's whole life, so once computed it is stored in the header. - **Is it locked?** `synchronized` needs somewhere to record lock state for each object. - **How old is it?** Generational collectors count how many collections an object has survived. ## The two header words On a 64-bit HotSpot JVM running JDK 25 with default settings the header has two parts. | Part | Size | What it holds | |---|---|---| | **Mark word** | 8 bytes | identity hash (once computed), GC age bits, lock bits, a bit the collectors use | | **Class word** | 4 bytes | a compressed pointer to the class's metadata in metaspace | The class word is 4 bytes rather than 8 because **compressed class pointers** (`UseCompressedClassPointers`) are on by default. How that pointer is encoded is a metaspace topic; for sizing, what matters is that it is 4 bytes. Arrays add a third part, a 4-byte length, right after the class word. ## Why 12 bytes become 16 HotSpot rounds every object's size up to a multiple of `ObjectAlignmentInBytes`, which is 8 by default. A 12-byte header therefore occupies 16 bytes: 12 of header and 4 of **alignment padding**. That is the whole of `new Object()`. The padding is not always wasted. The first instance field can start at offset 12, so a class with a single `int` field (4 bytes) is also 16 bytes: the field occupies the space that would otherwise have been padding. A class with a single `long` needs 8-byte alignment for that field, so the long starts at offset 16 and the object becomes 24 bytes. ## The number depends on the mode The 16-byte answer is for the default configuration of JDK 25. Other settings change it: | Setting on a 64-bit JDK 25 JVM | Header | `new Object()` | |---|---|---| | Defaults (compressed class pointers on, compact headers off) | 12 bytes | 16 bytes | | Heap large enough that compressed references are off | 12 bytes | 16 bytes | | `-XX:-UseCompressedClassPointers` (deprecated in JDK 25) | 16 bytes | 16 bytes | | `-XX:+UseCompactObjectHeaders` | 8 bytes | 8 bytes | Two rows deserve a note: 1. A large heap turns off compressed *references*, which makes reference fields 8 bytes, but class pointers stay compressed on their own flag, so the header itself is unchanged. 2. **Compact object headers** fold the class pointer into the mark word, so the whole header is one 8-byte word. On JDK 25 they are a product option that is off by default; JEP 534 makes them the default layout in JDK 27. ## Static fields do not count Static fields belong to the class, not to each instance. HotSpot stores them with the class's `java.lang.Class` object, once per class, so they never add to an instance's size. An instance costs its header, its instance fields and its padding, nothing else. ## Seeing it rather than guessing OpenJDK's **JOL** (Java Object Layout) tool prints the real layout of a class or an instance on the running JVM: each header word, each field's offset and size, and the alignment gap. Printing a plain `Object` on default JDK 25 shows the 8-byte mark, the 4-byte class word and a 4-byte alignment gap, for an instance size of 16 bytes. Measuring matters because the answer changes with the JVM's flags, and a layout remembered from an older release or another mode is easy to get wrong. ## Why this matters in practice For a handful of objects the header is noise. For millions of tiny objects it is not: an object whose fields add up to 8 bytes may occupy 24 or more, and a large share of a heap full of small objects can be headers and padding. That is the reason footprint-sensitive code favours fewer, larger objects or primitive arrays, and the reason the JDK has worked to shrink the header itself.

  • Does an object with a single int field cost more than new Object() on the same default JDK 25 JVM?
    No. With the 12-byte default header the first field can start at offset 12, and a 4-byte `int` fits exactly where the padding was, so the object is still 16 bytes. A single `long` is different: it needs 8-byte alignment, starts at offset 16, and the object becomes 24 bytes.
  • Do static fields make each instance of a class bigger?
    No. Static fields belong to the class, and HotSpot stores them with the class's `java.lang.Class` object, once per class. An instance's size is its header, its instance fields and its alignment padding, so a class with many static fields and no instance fields still produces 16-byte instances under JDK 25 defaults.

A parcel service that puts a shipping label and a handling tag on every box and sells boxes only in sizes that are multiples of 8: even an empty box carries both and is rounded up, though a small item can still fit in the space the rounding left.

  • An object with no fields takes no heap space; only the reference to it costs memory.
  • On a default JDK 25 JVM, new Object() is 8 bytes because the header is one word.
  • Static fields are copied into every instance and add to each object's size.
  • A heap over 32 GB makes every object header 16 bytes on JDK 25.
  • The 4 bytes of padding after the header can never hold a field.

Assumes a 64-bit JDK 25 JVM with default flags; compact object headers, off by default on JDK 25 and the default from JDK 27, make new Object() 8 bytes.