Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Your organisation has many memory-heavy Java services full of boxed collections such as Set<Long> and Map<Integer, Double>. Some teams want to adopt primitive collection libraries, others want to wait for Project Valhalla. How would you decide and what would you standardise?

I would decide with data: measure how much heap and GC time boxed collections actually cost in each service, using heap histograms and GC logs, because the answer differs widely between services. Where the cost is large, I would allow a primitive collection library now, but only in identified hot paths and behind internal interfaces, keeping public APIs on JDK types. Valhalla's value classes are not final as of Java 25 and their effect on generic collections is still evolving, so we should not plan around them yet, and we should revisit the decision at each LTS release.

Why this question is asked

Boxed collections are one of the most common hidden costs in Java services. A Map<Integer, Double> stores every key and value as a separate object, multiplying memory use and GC work, and in data-heavy services this can be the difference between a 4 GB and a 16 GB heap. The debate between adopting a library now and waiting for the platform to fix it is a classic senior decision: a known benefit today against a cost of dependency and divergence, and a promised benefit later with uncertain timing. Interviewers ask this to see how we make such decisions across many teams. In this answer, we will recap why boxing is expensive, quantify the cost, weigh the two options, set a policy, and plan how to revisit it.

1A Quick Recap of Why Boxed Collections Cost So Much

Java generics work only with reference types, so a Set<Long> cannot hold a long directly. Each value is wrapped in a Long object, which on a typical 64-bit JVM with compressed references takes 16 to 24 bytes for an 8-byte value. Collections add their own per-element objects: a HashMap entry node is another 32 bytes. So a Map<Integer, Double> pays for a node, an Integer and a Double per entry, often 70 to 80 bytes for 12 bytes of real data.

The cost is not only memory. Every object is something the garbage collector must find and trace, and scattered small objects make poor use of CPU caches, so iteration is slower than over a primitive array. Primitive collection libraries, such as fastutil, Eclipse Collections, HPPC or Koloboke, store values in primitive arrays and avoid both costs.

2Quantify the Cost First

Before any standard, we need to know where the problem is. Some teams may be losing gigabytes and frequent GC pauses; others may hold a few thousand boxed values and gain nothing from a change. A short, uniform measurement across the heaviest services settles this.

Shell
$ jcmd <pid> GC.class_histogram | head -6
num #instances #bytes class name
1: 41200118 1318403776 java.util.HashMap$Node
2: 22100332 530407968 java.lang.Long
3: 19100011 458400264 java.lang.Double

Output like this, from a service with a 4 GB heap, says that boxed map entries hold well over half of the live data. We would collect the same numbers for each candidate service, along with GC pause time and allocation rate from GC logs or Java Flight Recorder, and rank services by potential saving. That ranking turns a debate of opinions into a list of places where a change would matter.

3Option A: Adopt a Primitive Collection Library Now

The benefit is large and immediate. Replacing a HashMap<Integer, Double> with a primitive open-addressing map typically cuts its memory by 3 to 5 times and removes almost all of its objects from the GC's work. These libraries are mature and widely used.

The costs are real too. Each library has its own API, so code that uses Int2DoubleOpenHashMap does not look like code that uses Map<Integer, Double>, and engineers must learn it. Converting at boundaries to JDK types boxes everything again, so a primitive map that is passed to a generic API loses most of its benefit. Libraries are another dependency to keep updated and secure, and if different teams pick different libraries, shared code ends up with several incompatible types.

4Option B: Wait for Project Valhalla

Project Valhalla is the OpenJDK effort to add value classes, objects without identity that the JVM can store flat in memory, and eventually to let generics work over them and over primitives. If it delivers that, a List<Long>-like type could one day store values without boxing, with the standard API.

But as of Java 25, value classes are not a final feature; the work has appeared in early-access builds and JEPs, and the part that matters most here, flattening values inside generic collections such as HashMap, is still evolving. We should check the current status before planning around it rather than assume a date. Waiting means paying the full cost of boxing for an unknown number of years, and when Valhalla does arrive, existing collections may need changes to benefit fully.

Step throughDeciding for one service or code path
  1. Measure heap share and GC time of boxed collections
  2. Is the cost significant?for example over 20% of live heap or visible GC pauses
    YesTreat as a hot pathNoStay on JDK collections
  3. Can the primitive type stay internal?
    YesUse the approved library behind an internal interfaceNoRedesign the boundary or accept boxing
  4. Record the decision and the measurement
  5. Revisit at the next LTS release

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

5A Practical Policy

The policy I would standardise combines both options. Default to JDK collections everywhere; they are the common language, and in most code the boxing cost is too small to matter. Allow one approved primitive collection library, chosen once for the whole organisation so teams do not fragment, and allow it only in hot paths identified by measurement. Keep the primitive types internal: a module exposes JDK types or its own small interfaces in its public API, and the primitive library is an implementation detail behind them.

Java
// Public API stays on JDK types or a small interface of our own
public interface UserScores {
double scoreOf(int userId); // primitives in the signature, no boxing
int size();
}
// Implementation detail, swappable later
final class PrimitiveUserScores implements UserScores {
private final Int2DoubleOpenHashMap scores = new Int2DoubleOpenHashMap(); // fastutil
public double scoreOf(int userId) { return scores.get(userId); }
public int size() { return scores.size(); }
}

Notice that the interface uses primitive parameters and return types, so callers never box, and they do not know which library is used. If Valhalla or a better library arrives, we change one class, not every caller. This also limits the dependency to the modules that need it.

6Revisiting the Decision

A standard like this should have a review date built in. Each LTS release, Java 25 now and the next one after it, is a natural point: check the state of Valhalla and of the approved library, re-run the measurements on the services that adopted it, and confirm that the savings are still there. If a future release delivers value classes that make standard collections compact, the internal interfaces make migration a contained task. Writing the measurement and the reason next to each adoption makes these reviews quick, because nobody has to rediscover why a library was used.

7Follow-up questions

Why allow only one library?

Because the main costs of primitive collections are learning and integration. One library means one API to learn, one dependency to patch and one set of types in shared code. Allowing several multiplies those costs without adding much benefit, since the mature libraries offer similar performance.

What about simpler fixes before adding a library?

They often come first. A sorted int[] or long[] instead of a Set for read-mostly data, IntStream instead of Stream<Integer> in pipelines, and avoiding maps of maps can remove much of the cost with no dependency. The policy should list these as the first step.

How do you stop the library from spreading everywhere?

Through the build: an ArchUnit rule or dependency analysis that allows the library only in modules that declared a measured need, and a review rule that its types never appear in public signatures. Adoptions are then visible and deliberate.

8Conclusion

In this answer, we saw that boxed collections cost several times the memory of their data and add GC work, but that the cost varies widely, so we quantify it first with heap histograms and GC data. Primitive collection libraries give large savings now but add a dependency and non-standard APIs at boundaries, while Valhalla's value classes are not final as of Java 25. The practical policy is JDK types by default, one approved primitive library in measured hot paths behind internal interfaces, public APIs on JDK types, and a review at each LTS release.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by quantifying the cost instead of debating it. For the heaviest services, I would take heap histograms and GC logs and see how much live heap and GC time goes to HashMap nodes, Longs, Integers and Doubles.

Then, if they want more

In some services that is more than half the heap; in others it is negligible, and those should not change anything. Primitive collection libraries like fastutil or Eclipse Collections can cut that memory by several times today, but they add a dependency and a non-standard API, and passing their data to code expecting JDK types boxes it again.

Valhalla's value classes are not final as of Java 25, and how they will affect generic collections is still evolving, so I would check the current status but not plan around a date.

So my policy would be: JDK collections by default; one approved primitive library for the whole organisation, allowed only in hot paths where measurement shows a real saving; and those types kept behind internal interfaces, with public APIs on JDK types or small interfaces of our own, so we can swap the implementation later.

I would record the measurement with each adoption, enforce the boundary with an ArchUnit rule, and revisit the whole decision at every LTS release.

About this answer

Last updated
12 November 2026
Versions it names
Java 25
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 "Your organisation has many memory-heavy Java services full of boxed collections such as...": https://scrivio-interview-library.netlify.app/java/sets/42178c68f4/