Quick revision
The short answer
First we confirm the finding with proper heap analysis and find which strings are duplicated and who holds them. Then we evaluate options in order of cost: turn on the collector's string deduplication and measure the saving and the CPU, canonicalise the known small vocabularies such as currency codes and category names at parse time, and, if catalogs are simply too big to hold in every instance, change the data model to share them. We roll out whatever wins through the shared platform configuration with before and after metrics.
What to remember
In this answer, we treated the 30 percent finding as a hypothesis to confirm with a class histogram and a heap dump analysed by value and by holder. We then evaluated string deduplication as a configuration-only change, canonicalisation of small vocabularies at parse time with enums, Currency and a bounded canonical map, and data-model changes that stop every instance from holding the whole catalog. Whatever wins is rolled out through shared platform configuration on a canary first, with heap, GC CPU and latency measured before and after.
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.
First we confirm the finding with proper heap analysis and find which strings are duplicated and who holds them. Then we evaluate options in order of cost: turn on the collector's string deduplication and measure the saving and the CPU, canonicalise the known small vocabularies such as currency codes and category names at parse time, and, if catalogs are simply too big to hold in every instance, change the data model to share them. We roll out whatever wins through the shared platform configuration with before and after metrics.
Why this question is asked
A 30 percent waste across a fleet is real money: it means larger instances, more GC time and more nodes than needed. At the fifteen-year level, the interviewer wants to see a structured evaluation rather than a favourite trick: measure first, try the cheapest fleet-wide lever, and only then change code or architecture, each with evidence. In this answer, we will confirm the problem, look at deduplication, parse-time canonicalisation and data-model changes in turn with what each costs, and finish with how to roll the choice out across the fleet.
1Confirming What the Heap Dumps Say
Before changing anything, we want three facts: how much heap the duplicates really take, which values they are, and what holds them. A heap dump from a typical instance, taken after the catalog has loaded, answers all three. In Eclipse MAT we open the histogram, group java.lang.String by value and sort by retained heap; for each top value, the path to GC roots shows who holds it, for example ProductCategory.name inside a catalog map.
The class histogram is cheap and shows that strings dominate, but it cannot tell duplicates from unique values; the dump analysis can. Suppose it shows that "EUR", "USD" and about 2,000 category names account for 25 points of the 30 percent, held by product objects. That tells us the vocabulary is small and the copies come from parsing, which shapes every option below.
2Option 1: Turn On String Deduplication
The cheapest lever is a JVM flag. With -XX:+UseStringDeduplication, the collector finds long-lived strings with equal content and makes them share one byte array. Every service gets it without a code change. G1 has supported it since JDK 8u20, and since JDK 18 the Serial, Parallel, ZGC and Shenandoah collectors do too.
What it costs is background CPU for hashing and comparing candidates, and it only removes the byte arrays, not the String objects themselves, which are 24 bytes each. For short values like "EUR" the object header and fields are most of the cost, so deduplication saves less than the dump suggests. We measure it on a canary: heap after full collection, the deduplication statistics from GC logging (for example -Xlog:stringdedup*=debug, check your version), GC CPU time and p99 latency.
3Option 2: Canonicalise Small Vocabularies at Parse Time
Because the duplicates come from a small known set, we can avoid creating them at all. Currency codes become java.util.Currency, which returns one shared instance per code. Fixed categories can become an enum. Open but small vocabularies, such as category names, go through a bounded canonical map during deserialization.
Notice the size limit: without it, a bad feed full of unique values would turn the canonical map into a leak. This removes both the String objects and their arrays for those fields, so it usually saves more than deduplication, but it costs code in each service's model or in a shared parsing library. Parsers often already intern field names; their values need this kind of explicit help.
4Option 3: Change the Data Model
The deeper question is why every instance holds the full catalog in its heap. If each of 200 instances keeps the same 3 GB catalog, the fleet holds 600 GB of identical data. Options here include loading only the part of the catalog an instance serves, keeping the catalog in a shared cache such as Redis and fetching on demand with a small local cache, storing it off-heap or in a memory-mapped file, or replacing repeated text with small ids that point to one lookup table.
These are the largest savings and the largest changes: they move latency, add a dependency or require a migration. They are worth it when the catalog keeps growing, because options 1 and 2 only shrink a constant factor of a number that is still going up.
| Option | Effort | Typical saving | Main cost |
|---|---|---|---|
| String deduplication flag | config only | byte arrays of duplicates | GC background CPU |
| Canonicalise at parse time | shared library change | objects and arrays | code, bounded map |
| Change the data model | design and migration | most of the catalog per instance | latency, new dependency |
5Rolling Out Across the Fleet
A fleet-wide change should go through the shared platform configuration, such as the base container image, a common JAVA_TOOL_OPTIONS setting or the shared service template, rather than through 40 separate pull requests. We enable it for a canary group, compare heap after GC, instance memory, GC CPU and latency for a few days against a control group, and then widen it in stages.
The before and after numbers matter beyond this change. If deduplication recovers 15 of the 30 percent and canonicalisation another 10, we can lower memory limits and node counts and show the saving in money, which is what justifies the data-model work for the remaining growth.
- Heap dump analysiswhich values, how much, held by whom
- Dedup on a canarymeasure heap, GC CPU, p99
- Enough saving?YesRoll out via platform configNoCanonicalise vocabularies at parse time
- Catalog still growing?YesRedesign: share or split the catalogNoDone; lower memory limits
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just call intern() on every parsed value?
Because most catalog values, such as SKUs and descriptions, are unique, so interning them costs a lookup each and saves nothing, and the shared native table adds contention during big parses. Canonicalising only the known small vocabularies gets the saving without that cost.
Would a compact strings check matter here?
It is worth confirming that compact strings are enabled, which they are by default since JDK 9, and that the services are not on Java 8. For Latin-1 text like currency codes and English category names, the byte arrays already use one byte per character, so the remaining waste is the duplication itself.
How do you stop the problem from coming back?
Add a heap-composition metric, such as the share of heap in String and byte[], to the service dashboards and alert when it rises, and make the canonicalising deserializers part of the shared catalog client so new services get them by default.
7Conclusion
In this answer, we treated the 30 percent finding as a hypothesis to confirm with a class histogram and a heap dump analysed by value and by holder. We then evaluated string deduplication as a configuration-only change, canonicalisation of small vocabularies at parse time with enums, Currency and a bounded canonical map, and data-model changes that stop every instance from holding the whole catalog. Whatever wins is rolled out through shared platform configuration on a canary first, with heap, GC CPU and latency measured before and after.
Open with
I would start by confirming it. A class histogram shows how much of the heap is String and byte arrays, and a heap dump in MAT, grouped by value, tells me which values are duplicated and, through the paths to GC roots, which objects hold them.
Then, if they want more
Say it shows that currency codes and a couple of thousand category names make up most of the waste. Then I would evaluate options from cheapest to most invasive. First, string deduplication, which is one JVM flag on G1 and, since JDK 18, on the other collectors. It shares the byte arrays of equal long-lived strings without changing identity.
I would run it on a canary and measure heap after GC, GC CPU and p99 latency, because it costs background CPU and does not remove the String objects themselves.
Second, canonicalising at parse time: Currency instances or enums for fixed sets, and a bounded canonical map for category names, done once in the shared catalog client so every service benefits. That removes the objects as well as the arrays. Third, if the catalog keeps growing, question the data model: why does every instance hold the whole catalog?
Splitting it, a shared cache, or ids instead of repeated text saves the most but costs the most. Whatever we choose, I would ship it through the platform's base configuration, roll it out in stages, and report the before and after numbers so we can actually lower memory limits.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 18, Java 8, Java 9
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 fleet of services loads large JSON product catalogs, and heap dumps show 30 percent of...": https://scrivio-interview-library.netlify.app/java/strings/ff69b1ff44/