skip to content

A 64-bit HotSpot JVM with a 12 GB heap holds tens of millions of small cache entries, and heap use is about twice their summed field sizes. What explains the gap, and how do you confirm and reduce it?

level: seniorimportance: should knowfreq 40%

answer

  1. bytes before the first field
  2. header plus fields, rounded to 8
  3. 4-byte references below 32 GB
  4. measure the class, not the source
  5. only 8-byte steps show up

basics

~20 s

Each entry pays a 12-byte header and rounds up to 8 bytes, so 16 bytes of fields become a 32-byte object on JDK 25 defaults. Confirm with a layout tool and a class histogram; shrink it with compact headers, narrower fields or fewer objects.

solid answer

~50 s

Take `Entry { long key; int hits; Object value; }`: with a 12 GB heap references are 4 bytes by default, so the fields add up to 16 bytes. HotSpot puts the 12-byte header first, the `int` at offset 12, the `long` at 16 and the reference at 24, ends at 28 and rounds to **32 bytes**, twice the field sum. At 50 million entries that is about 800 MB of headers and padding. Confirm it rather than assume it: OpenJDK's JOL prints the class's real offsets and instance size, and `jcmd <pid> GC.class_histogram` gives instances and bytes per class, so bytes divided by instances is the real per-object size. To shrink it on JDK 25, `-XX:+UseCompactObjectHeaders` makes the header 8 bytes and the entry 24; narrowing `key` to an `int` also gives 24; replacing per-entry objects with parallel primitive arrays removes the per-entry header entirely. Reordering the fields does nothing.

code

console · 10 lines
console
Entry object internals:
OFF  SZ               TYPE DESCRIPTION               VALUE
  0   8                    (object header: mark)     N/A
  8   4                    (object header: class)    N/A
 12   4                int Entry.hits                N/A
 16   8               long Entry.key                 N/A
 24   4   java.lang.Object Entry.value               N/A
 28   4                    (object alignment gap)    
Instance size: 32 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

go deeper

for a junior

Remember that an object costs more than its fields: a header and padding come first and last.

for a middle

Compute the entry's layout: 12-byte header, int in the gap at 12, long at 16, reference at 24, rounded to 32 bytes.

for a senior

Confirm with JOL and a class histogram on the production JVM mode, then pick fixes that cross an 8-byte boundary, knowing that header and field changes do not always stack.

for a principal

Decide whether a JVM-wide change such as compact headers or a data-structure redesign is the right lever, weighing risk across services against the memory a single cache saves.

## Where the extra bytes come from Counting fields from the source file misses two costs that every HotSpot object pays: the **object header** and **alignment padding**. Consider a typical entry class: ```java final class Entry { long key; int hits; Object value; } ``` On a 64-bit JDK 25 JVM with a 12 GB heap, compressed references are on by default (they apply to heaps below roughly 32 GB), so `value` is 4 bytes and the fields total 8 + 4 + 4 = 16 bytes. HotSpot lays the object out like this: | Offset | Size | Content | |---|---|---| | 0 | 8 | mark word: identity hash, GC age, lock bits | | 8 | 4 | compressed class word | | 12 | 4 | `hits` (int), filling the gap before the aligned long | | 16 | 8 | `key` (long) | | 24 | 4 | `value` (compressed reference) | | 28 | 4 | padding to an 8-byte boundary | The instance is **32 bytes**: 16 of data, 12 of header and 4 of padding. The measured doubling is not a leak, it is the layout. ## The arithmetic at scale - 50 million entries at 32 bytes is about 1.6 GB, against the 800 MB the team counted. - The difference, about 800 MB, is headers and padding. - The map or index that holds the entries usually allocates its own per-entry objects, each with its own header, so the whole structure can cost more again. - Because every object rounds up to a multiple of 8, savings come in **8-byte steps**: removing 4 bytes helps only if the object's unpadded end crosses an 8-byte boundary. ## Confirming it instead of guessing 1. **Print the layout.** OpenJDK's JOL (`ClassLayout.parseClass(Entry.class).toPrintable()`) shows each field's offset, the header words, the alignment gap and the instance size on the JVM you run it on, with that JVM's flags. 2. **Count real instances.** `jcmd <pid> GC.class_histogram` lists, per class, the number of instances and the total bytes. Dividing bytes by instances for `Entry` gives the real per-object size; the same table shows whether the map's node objects or the values dominate instead. 3. **Compare like with like.** Run both on the same JDK release and flags as production. The header size depends on the mode, and a layout remembered from another release or from a run with compact headers will mislead. Taking a class histogram walks the whole heap, so on a large production heap it causes a pause; take it where that is acceptable. ## Reducing it | Change | `Entry` size | Trade-off | |---|---|---| | JDK 25 defaults | 32 bytes | baseline | | `-XX:+UseCompactObjectHeaders` | 24 bytes | opt-in on JDK 25, the default from JDK 27; a JVM-wide change | | `key` narrowed to `int` | 24 bytes | only if the key range fits in 32 bits | | both of the above | 24 bytes | the steps do not add: 20 bytes rounds back up to 24 | | parallel arrays (`long[]`, `int[]`, `Object[]`) | about 16 bytes per entry plus three array headers | no per-entry object, but harder code and no object identity per entry | The last row is the structural fix: one header per array instead of one per entry. The first row is the cheapest to try because it changes no code, but it changes every object in the JVM, so it needs a performance and compatibility check of its own. ## What does not help - **Reordering the fields.** HotSpot sorts fields by size itself; the `int` already fills the hole at offset 12. - **Making the fields `final`.** Finality changes semantics, not layout. - **Raising `ObjectAlignmentInBytes`.** A larger alignment rounds every object up further; it exists to let compressed references cover a larger heap, which is a heap-sizing trade-off, not a footprint saving. - **Assuming 8-byte references.** With a 12 GB heap references are 4 bytes by default; estimating with 8 overstates the fields and hides where the real overhead is. ## The general lesson For small objects the fixed per-object cost, header plus padding, can equal the payload. Estimating footprint from field sizes alone will be wrong by that amount, and the correction depends on the JVM mode. Measure the layout on the target JVM, then decide between a JVM-wide header change, narrower fields, or fewer objects.

  • If you enable compact object headers and also narrow the long key to an int, why do the savings not add up?
    Object sizes move in 8-byte steps. With compact headers the entry ends at 24 bytes. With an `int` key and either header the fields end at 20 or 24 bytes, which rounds to 24. Each change alone already reaches 24, and doing both lands on the same step, so measure each candidate with a layout tool before combining them.
  • Would raising -XX:ObjectAlignmentInBytes to 16 shrink this cache?
    No. Every object would round up to a multiple of 16: this 32-byte entry stays 32, but a 24-byte object grows to 32. A larger alignment exists so compressed references can span a bigger heap, which is a heap-sizing choice, not a way to save space on small objects.

  • Heap use at twice the field sum must be a memory leak.
  • Reordering the fields in the source will remove the padding.
  • References are always 8 bytes on a 64-bit JVM, whatever the heap size.
  • Summing field sizes from the source gives an accurate footprint estimate.
  • Compact object headers and a narrower field always add their savings together.

Assumes JDK 25 defaults with a 12 GB heap: 12-byte headers and 4-byte compressed references; compact object headers are opt-in on JDK 25 and the default from JDK 27.