Quick revision
The short answer
Nothing is misconfigured. The fleet's drop comes mostly from compact strings in JDK 9 and later, which store a String in one byte per character only when every character is Latin-1. Japanese and Chinese characters are outside Latin-1, so that service's content stays UTF-16 at two bytes per character, exactly as on Java 8. To reduce its memory we look at other levers: string deduplication, holding less text in the heap, and keeping pass-through content as encoded bytes rather than decoding it into Strings, and we set memory expectations per service from measurements rather than from the fleet average.
What to remember
In this answer, we saw that most of the fleet's heap drop came from compact strings, which halve string memory only when every character is Latin-1. Japanese and Chinese content stays UTF-16 at two bytes per character, so the CJK service's flat result is expected, which we confirm with a heap dump and a check that compact strings were not disabled. For that service we evaluate string deduplication, holding less text in the heap, and passing encoded content through as bytes, keeping in mind that UTF-8 is larger than UTF-16 for CJK. Finally, we set expectations per service from measurements rather than from fleet averages.
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.
Nothing is misconfigured. The fleet's drop comes mostly from compact strings in JDK 9 and later, which store a String in one byte per character only when every character is Latin-1. Japanese and Chinese characters are outside Latin-1, so that service's content stays UTF-16 at two bytes per character, exactly as on Java 8. To reduce its memory we look at other levers: string deduplication, holding less text in the heap, and keeping pass-through content as encoded bytes rather than decoding it into Strings, and we set memory expectations per service from measurements rather than from the fleet average.
Why this question is asked
After a big migration, teams compare every service against the headline result, and an outlier looks like a mistake. At the fifteen-year level the interviewer wants to see that we can explain the mechanism quickly, confirm it with data rather than reassurance, and then turn the conversation toward what would actually help that service. In this answer, we will recap why most services shrank, explain why this one did not, show how to confirm it, look at the levers that do help CJK-heavy services, and finish with how to set expectations across a fleet.
1A Quick Recap: Why Most Services Shrank
Moving from Java 8 to 17 brings several memory improvements, but for string-heavy services the largest is compact strings. On Java 8, every String held a char[], two bytes per character. Since JDK 9, a String holds a byte[] and a coder. If all characters are Latin-1, the first 256 Unicode characters, the coder is LATIN1 and each character takes one byte. Otherwise the coder is UTF16 and each character takes two.
Most services handle mainly Latin-1 text: English content, ids, JSON keys, URLs and log messages. Their string data roughly halved, and since strings are often a quarter to a half of a heap, total heap use dropped noticeably.
2Why the CJK Service Did Not
Japanese and Chinese characters, such as the kanji for Tokyo, U+6771 and U+4EAC, are far outside Latin-1. A product description or article body in Japanese contains almost only such characters, so it is stored as UTF16, two bytes per character, exactly the same size as the char[] on Java 8.
| Service content | Coder of most strings | String memory vs Java 8 |
|---|---|---|
| English, ids, JSON keys | LATIN1 | about half |
| French, German, Spanish | LATIN1 (accents are Latin-1) | about half |
| Japanese, Chinese, Korean | UTF16 | about the same |
| Mixed CJK with ASCII keys | mostly UTF16 | small saving on keys only |
Notice the mixed case too: a single CJK character turns the whole string into UTF-16, so even keys or labels combined with Japanese content lose the saving. For this service, most bytes in strings are CJK content, so compact strings barely change its heap. The other gains from the migration, such as better collectors, still apply, so a small improvement is plausible, but not the fleet-wide drop.
3Confirming It With Data
Saying it is expected is not enough; we should show it. A heap dump from the CJK service and from a comparable Latin-1 service, analysed in a tool such as Eclipse MAT, shows the share of heap in String and byte[], and we can sample strings to see how many have the UTF16 coder. A quick check is the ratio of byte[] size to String length for the largest strings: about 2 means UTF-16.
We should also rule out real misconfiguration while we are there: confirm the JVM was not started with -XX:-CompactStrings, which some teams copied from old tuning guides, and that the heap and collector settings match the rest of the fleet. If those are clean and the content is CJK, the case is closed and we can move to what would help.
4Levers That Do Help This Service
String deduplication is the first one to try. If the service holds many copies of the same text, such as cached descriptions or repeated category names in Japanese, -XX:+UseStringDeduplication lets the collector share their byte arrays regardless of coder. It costs some background CPU, so we measure it on a canary.
The second lever is holding less text in the heap at all. Many content services cache whole rendered pages or full article bodies in an in-process cache. Capping that cache, moving it to an external cache such as Redis, or caching compressed bytes for rarely used entries often saves far more than any encoding trick.
The third lever applies to text that is only passed through, for example content read from a database or object store and written straight into an HTTP response. Decoding it into a String and encoding it again costs a UTF-16 copy plus the String object. Keeping it as the encoded bytes we received, and writing those bytes out, avoids both. Notice that this is about avoiding the copy, not about UTF-8 being smaller: most CJK characters take three bytes in UTF-8 against two in UTF-16, so converting stored CJK text to UTF-8 to save space would make it larger. Compressed bytes, by contrast, can be much smaller in either encoding.
5Setting Expectations Per Service
The real lesson for the organization is that fleet averages hide the reasons. A migration report that says heap dropped 30 percent invites every team to expect 30 percent. Better to publish per-service before and after numbers with a short note on why each changed: Latin-1 heavy services saved most, CJK-heavy services saved little, services dominated by non-string data saw other effects.
Then each team sets memory limits and capacity plans from its own measurements. For the CJK service, that means not lowering its memory limits along with the rest of the fleet, and planning its savings around the levers above rather than around the migration.
6Follow-up questions
Would switching the service to UTF-8 internally fix this?
No. Java's String has no UTF-8 coder, and for CJK text UTF-8 is larger anyway, about three bytes per character against two. The useful idea is to avoid creating Strings for text that is only passed through, not to change the encoding of text we must process.
Could the service be configured to use Latin-1 somehow?
No. Latin-1 cannot represent Japanese or Chinese characters at all; the coder choice is made from the content and is not a setting. The only related flag, -XX:-CompactStrings, turns the optimisation off for everyone.
How do you explain this to engineers who are not JVM specialists?
I say that Java now stores text in one byte per letter when the text uses only Western letters, and in two bytes otherwise, as before. Their text is Japanese and Chinese, so it still needs two bytes; nothing is broken. Then I show the measurements and the three things we can do instead.
7Conclusion
In this answer, we saw that most of the fleet's heap drop came from compact strings, which halve string memory only when every character is Latin-1. Japanese and Chinese content stays UTF-16 at two bytes per character, so the CJK service's flat result is expected, which we confirm with a heap dump and a check that compact strings were not disabled. For that service we evaluate string deduplication, holding less text in the heap, and passing encoded content through as bytes, keeping in mind that UTF-8 is larger than UTF-16 for CJK. Finally, we set expectations per service from measurements rather than from fleet averages.
Open with
I would tell them it is expected, and then show them why. Most of the drop after moving from 8 to 17 comes from compact strings: since JDK 9 a String uses one byte per character when every character is Latin-1, and two bytes otherwise, which is what everything used on Java 8.
Then, if they want more
Most of our services handle English, ids and JSON keys, so their string data roughly halved. Japanese and Chinese characters are outside Latin-1, so that service's content is stored as UTF-16 exactly as before, and even a key combined with Japanese text becomes UTF-16.
I would confirm it with a heap dump, comparing the share of String and byte arrays and the byte-to-length ratio of the big strings, and I would check nobody started that JVM with CompactStrings turned off. Then the useful part is what we can do for that service.
First, try string deduplication on a canary, because content services often hold many copies of the same text. Second, look at how much text it caches in the heap, since capping or moving a page cache usually saves the most. Third, for content it only passes through, keep the bytes it read and write them out without decoding to String.
I would be careful to say this is about avoiding the copy, because UTF-8 is actually three bytes per CJK character. And I would ask that the migration report show per-service numbers, so teams plan from their own measurements instead of the fleet average.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8, Java 9, Java 17
- 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 "After a Java 8 to 17 migration, heap usage dropped noticeably for most services but not...": https://scrivio-interview-library.netlify.app/java/strings/12abb2073f/