Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Project Valhalla aims to add value classes that let the JVM flatten objects and avoid headers and indirection, but this is not a final feature in JDK 25. How should a company with memory-heavy Java systems plan around it over several years?

We plan for Valhalla without depending on it. Today's memory problems are solved with what is final now: primitive arrays, primitive collections and compact data layouts. At the same time, we prepare the code for value semantics by removing synchronization on, and == comparison of, wrapper objects and value-based classes such as Integer and Optional, using the warnings the JDK already gives, and by modelling data as immutable records, which are the likeliest candidates to become value classes. We test key workloads on early-access builds to estimate the gain and revisit the plan when the feature becomes final.

Background to this question

Project Valhalla is the long-running effort to let Java objects that are just data be stored like primitives: flat in arrays and fields, without headers or pointers. For systems that hold hundreds of millions of small objects, it could cut memory dramatically. But it is not final in JDK 25, and its shape has changed several times over the years. A technical leader in a memory-heavy company has to avoid two mistakes: waiting for Valhalla instead of solving today's problems, and building on something that may change. In this answer, we will see what Valhalla promises and why it matters, what we should do now instead, how to prepare our code, and how to evaluate and adopt it when it arrives.

1What Valhalla Promises

Every Java object today has identity: it has a header, lives at its own address, can be locked with synchronized, and two objects with the same fields are still different objects as far as == is concerned. That identity is why an array of a million Point objects is really an array of a million references to a million separate objects, each with a 12-byte header and each possibly in a different place in memory.

Value classes, proposed in JEP 401, give up identity. A value object is defined only by its field values, == compares those values, and it cannot be used as a lock. Because it has no identity, the JVM is free to flatten it, storing its fields directly inside an array or another object, with no header and no pointer. An array of a million value Points could then be a single block of two million ints.

An array of one million points
LayoutMemoryAccess
Point[] with identity objects todayabout 28 MB (4 MB refs + 24 MB objects)Pointer chase per element
int[] xs and int[] ys today8 MBSequential, cache friendly
Flattened value Point[] (future)about 8 MBSequential, with object syntax
Illustrative, with compressed oops; a flattened layout depends on the final design.

2Solving Today's Problems With What Is Final

The table shows that the main benefit of Valhalla is available today, at the cost of less convenient code. For memory-heavy hot spots we can store data as primitive arrays, one array per field, which is often called structure of arrays. We can use primitive collections, such as those in fastutil or Eclipse Collections, which keep long or int keys without boxing. We can pack small values into a long, and use compact encodings such as dictionary-encoded strings.

The rule for the organisation is simple: no system design, budget or deadline may depend on an unreleased feature. Teams fix measured memory problems now, with the tools above, behind interfaces that keep the compact layout an internal detail, so that it can later be replaced by value classes without changing callers.

Java
// Boxed: a Map<Long, Long> with 10 million entries holds 20 million Long objects
Map<Long, Long> lastSeen = new HashMap<>();
// Primitive collection: no boxing, much smaller (fastutil)
Long2LongOpenHashMap lastSeenCompact = new Long2LongOpenHashMap(10_000_000);
lastSeenCompact.put(42L, 1_730_000_000L);
long t = lastSeenCompact.get(42L); // 1730000000, no Long objects created

3Preparing the Code for Value Semantics

Some existing code would break or change meaning if the classes it uses became value classes. The most important cases are synchronizing on, or comparing with ==, instances of wrapper classes and other value-based classes, which the JDK documents as candidates: Integer, Long, Double, Optional, LocalDate, Duration and others. Code that locks on an Integer would fail, and == on two Integer objects would start comparing values, silently changing behaviour where code relied on identity.

Java
Integer lock = 42;
synchronized (lock) { ... }
// javac -Xlint:synchronization:
// warning: [synchronization] attempt to synchronize on an instance of a value-based class
// at runtime: -XX:DiagnoseSyncOnValueBasedClasses=2 logs each such lock
Integer a = 1000, b = 1000;
System.out.println(a == b); // false today: two different objects
System.out.println(a.equals(b)); // true: what the code almost always meant
new Integer(5); // deprecated for removal since JDK 16; use Integer.valueOf(5)

The JDK already helps: the compiler lint warns about synchronization on value-based classes, the runtime diagnostic flag finds the cases that only appear at runtime, and the wrapper constructors are deprecated for removal. Turning these warnings into build failures across the company is cheap and removes the main source of incompatibility in advance.

4Modelling Data So It Is Ready

The classes most likely to benefit from value classes are small, immutable data carriers: money amounts, coordinates, identifiers, time ranges. Modelling them today as records, with final fields and no identity-dependent behaviour such as locking or identity hash maps, is good design anyway, and it means they can become value records with a small change when the feature is final. Classes that need identity, such as entities with a lifecycle, should stay ordinary classes.

5Evaluating and Adopting When It Arrives

Valhalla's early-access builds let us try value classes before they are final. A small group can port one memory-heavy hot spot, such as a price grid or an index of small entries, run its benchmark and record heap use and throughput. The result tells us how much the feature is worth to us, which shapes the priority of adopting it, and it finds incompatibilities in our libraries early. Early-access results are estimates; the design and performance may still change.

When the feature becomes final, and more so when it is in an LTS release we run, we revisit the plan: replace the hand-made compact layouts where the value class version is simpler and as fast, and keep the ones that are still better. Until then, the work we did, cleaner data classes and no identity misuse, already paid off.

Step throughPlanning around an unreleased feature
  1. Measured memory problem today?Heap profile, cost
    YesFix now with primitive layoutsNoNo action needed
  2. Clean up identity misuseLint and runtime warnings as build failures
  3. Model data carriers as recordsImmutable, no locking
  4. Try early-access builds on one hot spotEstimate gain, find incompatibilities
  5. Feature final in an LTS we run?Release notes
    YesReplace compact layouts where simplerNoRevisit at the next release

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why not wait for Valhalla if it will fix the problem properly?

Because its release date and final shape are not known, and the problem costs money now. A primitive-array layout behind an interface solves it today, and the interface makes it easy to swap in value classes later. Waiting means paying for extra memory for an unknown number of years.

Will existing code get faster without changes when value classes arrive?

Some may, because certain JDK classes such as Integer or Optional are candidates to become value classes, which could reduce boxing costs. But the large gains come from our own data classes being declared as value classes, which needs code changes and a JDK where the feature is final.

What risks come with early-access builds?

They are for evaluation only: not supported, not secure for production and subject to change. We run them in a separate benchmark environment and treat the numbers as an estimate, never as a basis for a production plan.

7Conclusion

In this answer, we saw that Valhalla's value classes would let the JVM flatten objects and drop headers and pointers, but that the feature is not final in JDK 25. The plan is to solve memory problems now with primitive arrays, primitive collections and compact layouts behind interfaces, prepare code by removing synchronization on and == comparison of wrappers and value-based classes using the JDK's warnings, model data carriers as immutable records, and measure the gain on early-access builds before revisiting the plan when the feature is final.

What to say in the interviewAbout 90 seconds aloud

Open with

My first rule would be that nobody designs a system that depends on an unreleased feature. Valhalla's value classes would let the JVM flatten objects into arrays and fields without headers or pointers, which could save a lot of memory for us, but it is not final in JDK 25 and its design has changed before.

Then, if they want more

So for today's problems we use what is final: primitive arrays laid out one per field, primitive collections like fastutil instead of boxed maps, and compact encodings, ideally behind interfaces so the layout is an internal detail we can replace later. At the same time, we prepare the code.

Value classes have no identity, so code that synchronizes on Integer or other value-based classes, or compares wrappers with double equals, would break or change meaning. The JDK already warns about this: there is a javac lint for synchronization on value-based classes, a runtime diagnostic flag, and the wrapper constructors are deprecated for removal.

I would turn those into build failures. I would also encourage modelling small data carriers as immutable records, since those are the likeliest candidates to become value classes.

Then a small group tests one memory-heavy hot spot on early-access builds to estimate the gain and find library problems, and we revisit the plan when the feature is final, especially once it is in an LTS we run.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Project Valhalla aims to add value classes that let the JVM flatten objects and avoid...": https://scrivio-interview-library.netlify.app/java/memory-layout/de50f309c3/