Quick revision
The short answer
I would treat the 40 percent claim as a hypothesis to verify with heap histograms and production-like load tests, then check the library's health, licence, release cadence, JDK compatibility and its effect on jar size and native images. If it passes, I would approve it for the named services that showed the saving, with an owning team, wrapped behind a small internal API in hot paths so its types never cross service boundaries, and with a note on how Valhalla value classes may change the picture later.
What to remember
In this answer, we saw that primitive collections save memory by storing numbers directly in arrays instead of boxed objects, so a 40 percent heap saving is plausible only where boxed data dominates. We validate it with heap histograms and production-like load tests, assess the library's health, licence, JDK support, jar size and native image impact, wrap it behind an internal API so its types never leave the service, keep in mind that Valhalla value classes may narrow the gap later, and approve it for named services with an owner and a review date.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would treat the 40 percent claim as a hypothesis to verify with heap histograms and production-like load tests, then check the library's health, licence, release cadence, JDK compatibility and its effect on jar size and native images. If it passes, I would approve it for the named services that showed the saving, with an owning team, wrapped behind a small internal API in hot paths so its types never cross service boundaries, and with a note on how Valhalla value classes may change the picture later.
Why this question is asked
Primitive collections libraries such as fastutil, Eclipse Collections and HPPC store ints, longs and doubles directly in arrays instead of boxing them into objects, and for data-heavy services they can cut heap use dramatically. That makes them tempting, and it also makes them a typical governance question: a good local optimisation that becomes a company-wide dependency nobody owns. Interviewers ask this to see how a leader balances a team's real gain against long-term cost. In this answer, we will see where the saving comes from, validate the claim, assess the library, contain it in the code, consider Valhalla, and decide how to approve it.
1Where the Saving Comes From
Java's standard collections hold objects, so a Map<Long, Integer> stores each key as a boxed Long (16 bytes) and each value as a boxed Integer (16 bytes), inside a HashMap node of 32 bytes, plus a share of the bucket array. That is roughly 70 bytes for 12 bytes of actual data. A primitive map such as fastutil's Long2IntOpenHashMap keeps a long[] of keys and an int[] of values with open addressing, roughly 12 bytes of data divided by the load factor, so about 16 to 25 bytes per entry, and no objects for the garbage collector to trace.
So the 40 percent claim is plausible for services whose heap is dominated by large maps or lists of boxed numbers. It is implausible for services whose heap is mostly strings, byte buffers or domain objects. That is exactly why we have to check it per service.
Notice the last line. Primitive maps cannot return null, so a missing key returns a default value. Code moved from Map<Long, Integer> that relied on null for "absent" changes behaviour silently, which is one of the migration risks to review.
2Validating the Saving Properly
A microbenchmark that fills a map with 10 million entries will always show a big saving, and it proves little about a real service. We want three pieces of evidence. First, a heap histogram from the running service, jcmd <pid> GC.class_histogram, or a heap dump analysed in a tool such as Eclipse MAT, showing what share of the live heap is boxed numbers and collection nodes today. Second, the same measurement on a branch using the library, under a production-like load test, along with GC time, pause times and latency. Third, a check of what the saving buys: if the containers keep the same -Xmx and memory limit, a smaller live heap reduces GC work but saves no money until we resize them.
JMH benchmarks are still useful for the hot operations, because some primitive maps are faster and some are slower than HashMap depending on the access pattern and load factor.
- Heap histogram of each service todayis boxed data a large share?
- Prototype on a branchload test: heap, GC, latency
- Saving confirmed for this service?YesAssess the library itselfNoDecline for this service
- Health, licence, JDK, jar size, native imageacceptable risk?
- Approve for named servicesowner, wrapper API, review date
5 steps in order. Play them, or step through them one at a time.
3Assessing the Library Itself
A dependency is a long-term relationship, so we look at the project as well as the code. Project health: how many active maintainers, how quickly issues and security reports are handled, and whether releases are regular. All three named libraries are mature, but their activity differs, and that changes over time, so we check current data rather than reputation. Licence: fastutil and HPPC are under the Apache 2.0 licence and Eclipse Collections under the Eclipse Public and Distribution licences; our legal policy must allow the one we pick. JDK compatibility: the library must run on our current JDK, Java 25 for instance, and have a record of keeping up with new releases.
Packaging matters too. The full fastutil jar is large, over 20 MB, because it generates classes for every combination of primitive types; a smaller core artifact exists, and tools such as ProGuard can strip unused classes. For services built as GraalVM native images, we check that the library works without extra reflection configuration and see how much it adds to build time and image size.
4Containing It in the Code
Even a good library should not spread its types through the codebase. We wrap it behind a small internal interface in the hot paths that need it, such as a LongIntTable with get, put and forEach, and keep its classes out of public method signatures, DTOs, events and API contracts. Two reasons. If the library is abandoned, or Valhalla makes it unnecessary, we replace one implementation instead of hundreds of call sites. And services that talk to each other must never depend on the same internal collections library to understand each other's data; the wire format stays JSON, Avro or Protobuf.
An architecture test, for example with ArchUnit, can enforce that only the wrapper package imports the library.
5Considering Valhalla's Value Classes
Project Valhalla is the long-running OpenJDK effort to add value classes, objects without identity that the JVM can store flat in arrays and fields, and, later, generics over primitives. If and when that work is delivered and fully supported, an ArrayList of a value type could approach the density of a primitive array, which would narrow the gap these libraries fill. At the time of writing, value classes are a preview or early-access feature rather than a final one, and generic specialisation is further away, so check your version and the current JEP status. The practical effect on our decision is the investment horizon: we expect to keep the library for several years, which justifies it only with the wrapper in place so that moving off it later is cheap.
6Approving It for Named Services
The decision I would make is a scoped yes, not a blanket one. The library is approved for the services where the measured saving is real, each with a named owning team responsible for upgrades and security patches. It is added to the dependency allow-list with a version managed centrally in the platform BOM, so all users move together. New services that want it bring their own heap evidence to the architecture review. We set a review date, for example in a year, to look at actual savings, any incidents, the library's health and Valhalla's progress. This keeps the gain without creating a free-for-all dependency that nobody can remove.
7Follow-up questions
What if only one service benefits?
Then the approval covers that one service. A dependency's cost is mostly fixed, upgrades, security monitoring and knowledge, so one service with a large measured saving, say 6 GB less heap per instance across 40 instances, can still justify it. The decision is about evidence, not popularity.
Could we get the saving without a new library?
Sometimes. Sorted primitive arrays with binary search, plain long[] and int[] columns, or a better choice of data structure can cut the same boxing overhead with no dependency. For read-mostly data, these are often enough, and we should check them first.
How do you avoid every team picking a different library?
By choosing one approved primitive collections library for the company, managed in the shared BOM, and steering requests for others to the architecture review. Two libraries doing the same job double the upgrade and security work for no gain.
8Conclusion
In this answer, we saw that primitive collections save memory by storing numbers directly in arrays instead of boxed objects, so a 40 percent heap saving is plausible only where boxed data dominates. We validate it with heap histograms and production-like load tests, assess the library's health, licence, JDK support, jar size and native image impact, wrap it behind an internal API so its types never leave the service, keep in mind that Valhalla value classes may narrow the gap later, and approve it for named services with an owner and a review date.
Open with
I would treat the 40 percent as a hypothesis. First, evidence per service: a heap histogram with jcmd GC.class_histogram or a heap dump to show that boxed numbers and collection nodes really dominate the live heap, then a branch with the library under a production-like load test, measuring heap, GC time and latency, not just a microbenchmark that fills a map.
Then, if they want more
I would also check whether the saving turns into money, since containers keep their memory limit unless we resize them. Second, the library itself: maintainers and release cadence, how it handles security reports, licence compatibility, support for our JDK, the jar size, fastutil's full jar is over 20 MB, and whether it works in native images.
Third, containment: in hot paths it sits behind a small internal interface, and its types never appear in public APIs, DTOs or messages between services, which an ArchUnit rule can enforce.
That also keeps the door open for Valhalla value classes, which may narrow the gap in future JDKs; that is still preview work, so I would check the current status.
Finally, I would approve it for the named services that showed the saving, each with an owner, the version pinned in our shared BOM, and a review in a year, rather than as a free-for-all dependency.
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 official documentation for the version you use. 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 "A team proposes adding a third-party primitive collections library (for example...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/b984f14864/