← Latest briefing

Technology

JDK 28 preview of value classes highlights JVM optimizations and performance trade-offs

While Java's Valhalla milestone reduces memory overhead by removing object identity, erased megamorphic calls can cause unexpected heap allocations.

The short version

  • JDK 28 JEP 401 introduces value classes to the Java Virtual Machine, allowing the runtime to flatten data structures and eliminate object identity overhead.
  • Immutable fields enable the JVM to store multi-word values without reference pointers, whereas mutable fields must retain reference layouts to prevent data tearing during multi-threaded updates.
  • Megamorphic virtual calls involving generic interfaces can degrade performance by forcing scalarized values to be repeatedly allocated on the heap at method boundaries.

Key facts

  • JDK 28 JEP 401 integrates value classes as a preview feature, enabling the JVM to flatten values and represent components in registers or on the stack by eliminating object identity.[Hacker News]
  • Under JEP 539, strictly initialized final fields allow the JVM to use a non-atomic flattened layout because the enclosing object cannot be observed prior to field initialization.[Hacker News]
  • Mutable fields exceeding atomic update limits revert to reference layouts to comply with the Java Memory Model and prevent torn assignments during concurrent thread operations.[Hacker News]
  • At megamorphic call sites, type erasure in Java generics forces scalarized value objects to be converted back into standard heap references to pass through erased method signatures.[Hacker News]
  • Engineers on the C2 compiler team are actively investigating potential compiler solutions to address performance regressions caused by value class materialization in generic code.[Hacker News]

What remains uncertain

  • The timeline and specific implementation details for how the C2 compiler team will resolve allocation overhead at megamorphic call sites remain under active investigation.[Hacker News]

Sources