Stop Guessing Your Cache Locality: Verify JEP 401 Value Class Flattening with JFR
JEP 401 value classes are a massive win for memory density, but too many developers are cargo-culting them and assuming the JVM automatically flattens their data. If you aren't actively verifying your memory layouts with JFR and JOL, you are likely still paying the tax of pointer indirection and GC pressure.
Why Most Developers Get This Wrong
-
Assuming
value classequals flat layout: Declaring a class as avalue classis only a hint; if it is nullable or too large, the JVM silently falls back to standard heap buffering. - Relying solely on microbenchmarks: JMH benchmarks can deceive you in isolated, warm-up environments where the compiler optimizes away allocations that actually occur in production.
-
Ignoring null-restriction: Skipping the explicit null-restricted type operator (
!) means the JVM must allow fornull, completely destroying any chance of array flattening.
The Right Way
You must combine static layout validation via JOL with dynamic allocation profiling using JDK Flight Recorder (JFR) to guarantee zero-allocation execution paths.
- Assert layouts in tests: Use Java Object Layout (JOL) in your unit tests to programmatically assert the exact byte offset and verify the absence of object headers for nested fields.
-
Profile with JFR events: Monitor
jdk.ObjectAllocationInNewTLABandjdk.ObjectAllocationOutsideTLABevents on your hot paths to ensure your value types show zero allocations. -
Enforce null-restriction: Always use the
!operator (e.g.,Point!) on fields and arrays to explicitly opt-out of nullability and force the JVM to flatten the memory footprint.
Shameless plug: javalld.com has full LLD implementations with step-by-step execution traces — free to use while prepping.
Show Me The Code
// JEP 401 Value Class definition
public value class Point {
int x;
int y;
}
public class PathTracker {
// The '!' operator forces null-restriction, enabling array flattening
private Point![] coordinates = new Point![1000];
public void updatePath(int index, int x, int y) {
// Flat array write: compiles to direct memory writes, zero JFR allocation events
coordinates[index] = new Point(x, y);
}
}
Key Takeaways
-
No exclamation, no flattening: Without the
!null-restriction operator, your value classes are just typical heap-allocated objects with a fancy keyword. - Automate verification: Put JOL assertions directly into your CI pipeline to catch accidental layout bloating before it hits main.
- Trust JFR, not assumptions: Use JFR in your staging environment to verify that your critical execution loops are completely free of object allocation samples.
Top comments (0)