100M Java values. One flat array. Valhalla changes the memory game.
🔸 TLDR
Project Valhalla is not just new syntax.
It changes how Java can represent data in memory.
Value objects have no identity, allowing the JVM to flatten them and reduce object overhead and indirection.
And JDK 28 gives us a concrete preview of that future. 🚀
🔸 TRUTH
100,000,000 Java objects… without 100,000,000 separate heap objects. 🤯
Project Valhalla is reaching a major milestone with Value Classes and Objects previewed in the JDK 28 mainline.
And this small example shows why it matters.
value record Id(int value) {}
var ids = new Id[100_000_000];
for (int i = 0; i < ids.length; i++) {
ids[i] = new Id(i);
}
Then inspect the heap:
#instances #bytes class
1 800000016 [LId;
🔸 WHAT JUST HAPPENED?
▪️ The array contains 100 million Id values
▪️ But there are no 100 million individual Id objects in the histogram 😮
▪️ The JVM can store value objects directly inside the array
▪️ No object header for every Id
▪️ No extra pointer indirection to reach every Id
That is one of Valhalla's key goals: identity-free value objects can use much more compact representations when the JVM can flatten them.
🔸 WHY 8 BYTES PER ELEMENT HERE?
In this example:
▪️ int value → 4 bytes
▪️ nullness information → additional storage
▪️ alignment/padding → brings the representation to 8 bytes
So roughly:
100M × 8 bytes ≈ 800 MB
Instead of allocating 100M independent Java objects. 🔥
🔸 TAKEAWAYS
▪️ value objects are identity-free
▪️ They can be flattened in arrays and fields
▪️ Fewer allocations can mean better memory density
▪️ Less indirection can improve data locality
▪️ This is preview technology, not something to blindly move production code to yet ⚠️
Valhalla is becoming real Java. ☕🔥
#Java #OpenJDK #ProjectValhalla #JDK28 #JVM #JavaPerformance #Programming #SoftwareEngineering #BackendDevelopment
Go further with Java certification:
Java👇
Spring👇
SpringBook👇
JavaFullstackBook👇