How does HotSpot on JDK 25 decide the order and offsets of an object's instance fields, and where does alignment padding end up?
answer
- source order is not memory order
- largest primitives first
- best fit into existing holes
- references grouped after primitives
- total rounded up to 8
basics
~20 sHotSpot ignores declaration order: inherited fields keep their offsets, then primitives go largest first and references after them, each naturally aligned and placed in the smallest hole that fits. The total rounds up to 8 bytes, so most padding ends up at the end.
solid answer
~40 sThe JVM, not the source file, chooses the layout. On JDK 25 HotSpot starts from the header (12 bytes by default) and the superclass's fields, which keep their offsets. It then places this class's primitive fields sorted largest first (`long`/`double`, then `int`/`float`, `short`/`char`, `byte`/`boolean`), declaration order breaking ties, and then its reference fields; each field is aligned to its own size and goes into the smallest existing hole that fits, otherwise at the end. If the superclass ends with a reference, the subclass leads with its references so the garbage collector's reference maps merge. Finally the instance size is rounded up to `ObjectAlignmentInBytes`, 8 by default. So a 4-byte field fills the gap at offset 12 before an 8-aligned `long`, and padding collects mostly at the tail.
code
java · 12 linesclass A {
byte x; // offset 12; A is 16 bytes on JDK 25 defaults
}
class B extends A {
byte y; // offset 13, inside A's tail padding; B is still 16 bytes
}
class C {
long a; // offset 16: a long needs 8-byte alignment
int b; // offset 12: fills the hole before a; C is 24 bytes
}go deeper
Remember that the JVM, not your source order, decides where fields live, and that each object's size is rounded up to 8 bytes.
Walk through the JDK 25 rules: inherited offsets fixed, primitives largest first, references after, best-fit hole filling, 8-byte rounding, and show the int filling the gap before a long.
Predict an object's size from its fields and header mode, recognise that reordering source fields changes nothing, and know which edits actually cross an 8-byte boundary.
Steer footprint work toward changes that move real bytes, field widths, object counts and header mode, rather than cosmetic field reordering that HotSpot undoes.
## The JVM picks the layout, not the source file The Java language and the JVM specification say nothing about where in memory an object's fields live. That is left to the implementation, and HotSpot computes a layout for each class when the class is loaded. Declaration order in the source file is used only to break ties. Everything below describes HotSpot on JDK 25; older JDKs used a different algorithm and offered flags to choose an allocation style, which JDK 25 no longer has. ## The JDK 25 strategy, step by step HotSpot's field layout builder (`fieldLayoutBuilder.cpp`) handles a regular class like this: 1. **Start after the header and the superclass.** The first bytes are the object header, 12 bytes with default settings or 8 with compact object headers. If there is a superclass, its fields keep exactly the offsets they have in the superclass, and any holes between them are marked as free. 2. **Sort this class's primitive fields by size, largest first.** `long` and `double` (8 bytes), then `int` and `float` (4), `short` and `char` (2), `byte` and `boolean` (1). Fields of equal size keep their declaration order. 3. **Place each field by best fit.** Every field must sit at an offset that is a multiple of its own size. For each field, HotSpot looks for the smallest free hole that can take it with that alignment; if none exists, it appends the field at the end of the layout, inserting a gap if alignment requires one. 4. **Place the reference fields.** References (4 bytes with compressed references, 8 without) normally come after the primitives. If the superclass's layout ends with a reference, this class's references go first instead, so the two runs of references sit next to each other. 5. **Round up.** The instance size is the end of the last field rounded up to a multiple of `ObjectAlignmentInBytes`, which is 8 by default. Static fields are laid out separately, with the class's `java.lang.Class` object, and never appear in an instance. ## Why references are grouped The garbage collector finds an object's references through **oop maps**: per-class lists of offset ranges that hold references. Keeping a class's references together keeps those lists short, and leading with references after a superclass that ended with one lets the two ranges merge. The collector then scans fewer, longer ranges. ## A worked example Take this class on a 64-bit JDK 25 JVM with default settings and a heap small enough for compressed references: ```java class Sample { byte flag; long id; int count; Object ref; short code; } ``` | Offset | Size | Content | |---|---|---| | 0 | 8 | mark word | | 8 | 4 | class word | | 12 | 4 | `count` (int) | | 16 | 8 | `id` (long) | | 24 | 2 | `code` (short) | | 26 | 1 | `flag` (byte) | | 27 | 1 | gap, aligning the reference | | 28 | 4 | `ref` (compressed reference) | The `long` is placed first but cannot sit at offset 12, so it goes to 16 and leaves a 4-byte hole; the `int` then fills that hole by best fit. The fields total 19 bytes and the object is 32. Declaring the fields in any other order produces the same 32 bytes, because the sort, not the source, decides. ## Inheritance and the superclass's tail - A subclass never moves its superclass's fields: code compiled against the superclass must find them at the same offsets in every subclass instance. - Subclass fields may fill holes inside the superclass's layout. - They continue right after the superclass's **last field**, not after its rounded-up size. A class `A { byte x; }` is 16 bytes, and `B extends A { byte y; }` is still 16: `y` lands at offset 13, inside what was `A`'s tail padding. - After a superclass that uses the `@Contended` annotation, HotSpot inserts padding instead of letting subclass fields into its gaps; that annotation is a false-sharing tool covered elsewhere. ## Where padding remains Because large fields go first and small ones fill holes, padding inside an object is usually small. What remains is: - the gap before an 8-byte field when nothing small is available to fill it; - small alignment gaps before a reference that follows odd-sized fields; - the **external** padding that rounds the total up to 8 bytes. A `boolean` occupies a whole byte, so eight `boolean` fields take eight bytes, not one. ## What this means in practice Reordering fields in the source is not a footprint optimisation on HotSpot. What changes the size is which fields exist and how wide they are: replacing a `long` with an `int`, or removing a field, can drop an object below an 8-byte boundary. A layout tool such as OpenJDK's JOL shows the offsets HotSpot actually chose, which is the only reliable way to check.
- Can a subclass's fields use the padding at the end of its superclass's instances?Yes, on JDK 25. The subclass layout keeps the superclass's offsets and continues right after the superclass's last field, not after its rounded-up size, and fills any holes in between. So `class A { byte x; }` is 16 bytes and `class B extends A { byte y; }` is still 16, with `y` at offset 13.
- Why does HotSpot keep a class's reference fields together instead of interleaving them with primitives?The garbage collector locates references through the class's oop maps, lists of offset ranges that hold references. Grouping references keeps those lists short, and when the superclass ends with a reference the subclass places its references first so the two ranges merge, letting the collector scan fewer, longer ranges.
- Fields are laid out in memory in the order they are declared in the source.
- Reordering field declarations is a reliable way to shrink an object on HotSpot.
- A boolean field takes one bit, so eight booleans share a single byte.
- Every field is padded to 8 bytes on a 64-bit JVM.
- The JVM specification fixes the field layout that every JVM must follow.
- A subclass's fields always start at its superclass's rounded-up instance size.
Assumes HotSpot on JDK 25 with the default 12-byte header; field placement is an implementation detail, and offsets start at 8 with compact object headers.