Object header
Every Java object carries a small block of bookkeeping data before its fields, called the object header. On a normal 64-bit JVM it is 12 bytes, which is why even an empty object costs 16 bytes after padding.
Detailed Explanation
An object in Java is more than the fields you declared. The JVM needs to know, for any address in the heap, what type lives there, whether it is locked, what its identity hash code is, and how many times the collector has copied it. That information is stored in a fixed prefix on every single object.
The header has two parts. The mark word is 8 bytes and holds mutable per-object state: the identity hash code once someone asks for it, the lock state when a thread synchronizes on the object, and the GC age counter that decides when the object gets promoted out of the young generation. The class pointer identifies the object's class so that virtual calls and instanceof work.
The class pointer would be 8 bytes on a 64-bit machine, but the JVM cheats. With compressed ordinary object pointers, enabled by default whenever the heap is under 32 GB, references are stored as 32-bit offsets from the heap base and scaled by 8. That makes the class pointer 4 bytes, so the total header is 12 bytes rather than 16, and every reference field in your objects is 4 bytes rather than 8.
Objects are then aligned to 8-byte boundaries. So the size of an object is the header, plus the fields, rounded up to the next multiple of 8. A class with no fields at all: 12 bytes of header, padded to 16. An Integer: 12 plus a 4-byte int, exactly 16. A Long: 12 plus 8, padded to 24. Arrays carry an extra 4-byte length field, so their header is 16 bytes.
This is why boxing is expensive. A million int values in an int[] occupy about 4 MB. The same values in a List<Integer> cost roughly 16 MB of Integer objects plus another 4 MB for the backing Object[] of references, and every read costs a pointer chase into a random cache line.
| Object | Header | Fields | Total |
|---|---|---|---|
new Object() | 12 | 0 | 16 |
Integer | 12 | 4 | 16 |
Long | 12 | 8 | 24 |
new int[0] | 16 | 0 | 16 |
You reach for this when you are sizing a cache or a large in-memory index and the measured footprint is three times your arithmetic. The header, the padding, and the reference fields are where the difference went. The tool for checking is JOL, which prints the actual layout the JVM chose rather than your estimate.
The cost of the header is fixed and unavoidable per object, so the way to reduce it is to have fewer, larger objects: primitive arrays instead of collections of boxes, one array of structs-of-arrays instead of a million tiny records.
Code Examples
# Confirm compressed oops are on. They switch off automatically above a 32 GB heap.
java -XX:+PrintFlagsFinal -version | grep UseCompressedOops
# prints: bool UseCompressedOops = true {product lp64_product} {ergonomic}
# Java Object Layout prints the real header and field offsets.
java -jar jol-cli.jar internals java.lang.Integer
# OFFSET SIZE TYPE DESCRIPTION
# 0 8 (object header: mark)
# 8 4 (object header: class)
# 12 4 int Integer.value
# Instance size: 16 bytesimport java.util.ArrayList;
import java.util.List;
public class Footprint {
public static void main(String[] args) {
int n = 1_000_000;
int[] primitives = new int[n]; // 16-byte header + 4 bytes per element
List<Integer> boxed = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
boxed.add(i); // each value above 127 is a fresh 16-byte object
}
// primitives: about 4 MB in one contiguous block.
// boxed: about 16 MB of Integer objects plus ~4 MB of references.
System.out.println(primitives.length == boxed.size()); // prints: true
}
}Common Gotchas
An empty object is not free
`new Object()` costs 16 bytes. A cache of ten million tiny wrapper objects spends 160 MB on headers and padding before storing any data.
A 33 GB heap can hold less than a 31 GB heap
crossing 32 GB disables compressed oops, so every reference and class pointer grows to 8 bytes. The extra capacity is eaten by the wider pointers. Either stay under the threshold or go far above it.
Headers are shrinking
`-XX:+UseCompactObjectHeaders` reduces the header to 8 bytes by folding the class pointer into the mark word. It arrived as an experimental option in JDK 24 and became a product option in JDK 25.
Calling `hashCode()` mutates the header
the identity hash is computed lazily and then stored in the mark word. That is harmless, but it means an object's header contents differ before and after the first hash, which surprises people reading raw memory dumps.