Quick revision
The short answer
The history suggests that memory optimisations for text mostly belong in the platform. Moving the pool to the heap, compact strings and GC deduplication each helped every application without a code change, while application tricks such as manual interning were tuned for one JDK and often became useless or harmful after an upgrade. So I guide teams to upgrade the JDK and use supported options first, measure, and use application-level canonicalisation only where heap analysis shows a real problem, documented so it can be removed later, with no dependence on String internals.
What to remember
In this answer, we saw that moving the pool to the heap, string deduplication and compact strings each reduced memory for every application without code changes, while application tricks such as aggressive interning, defensive substring copies, custom byte-array text and reflection on String internals became useless, harmful or broken after upgrades. The guidance that follows is to upgrade and use supported platform options first, measure, apply narrow and documented canonicalisation only where heap analysis demands it, and never depend on String internals, while keeping data-model decisions firmly in the application.
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.
The history suggests that memory optimisations for text mostly belong in the platform. Moving the pool to the heap, compact strings and GC deduplication each helped every application without a code change, while application tricks such as manual interning were tuned for one JDK and often became useless or harmful after an upgrade. So I guide teams to upgrade the JDK and use supported options first, measure, and use application-level canonicalisation only where heap analysis shows a real problem, documented so it can be removed later, with no dependence on String internals.
Why this question is asked
This is a question about where engineering effort should go. Every large Java organization has old code full of performance workarounds, and many of them were written for a JDK that no longer exists. At the twenty-year level, interviewers want to see that we can read the platform's history as evidence and turn it into practical guidance that saves teams effort and avoids fragile code. In this answer, we will walk through the three changes in order, see what each did for applications that changed nothing, look at what happened to application-level tricks, and then set out the guidance I would give teams.
1A Quick Recap of the Three Changes
Three platform changes shaped how much memory Java text uses. In JDK 7, interned strings moved from PermGen, a small fixed area that was rarely collected, to the normal heap, and JDK 8 removed PermGen altogether. In JDK 8u20, G1 gained string deduplication, which lets the collector make equal long-lived strings share one backing array; since JDK 18 the other collectors have it too. In JDK 9, compact strings changed String's storage from a char[] of two bytes per character to a byte[] plus a coder that uses one byte per character for Latin-1 text.
None of these required applications to change code. A service that simply moved to a newer JDK and, for deduplication, set one flag got the benefit.
| Change | JDK | Effect without code changes |
|---|---|---|
| Interned strings moved to the heap | 7 (PermGen removed in 8) | no more PermGen errors from interning |
| String deduplication | 8u20 G1, 18 other GCs | equal long-lived strings share arrays |
| Compact strings | 9 | Latin-1 text uses half the memory |
| invokedynamic concatenation | 9 | faster + without recompiling twice |
2What Happened to the Application Tricks
Now look at the workarounds teams wrote in the meantime. On JDK 6, teams avoided intern() because of PermGen, or interned carefully with tuned flags. After JDK 7, the PermGen fear was obsolete, and some teams swung to interning everything, which on large inputs mostly added hash lookups and contention on the shared StringTable. Other teams stored text as byte[] in UTF-8 to save memory over char[]; after compact strings, a plain String of Latin-1 text was as small as their byte array and far easier to use, so the custom code became pure cost.
There are subtler cases. Code that called new String(s.substring(...)) to avoid the old substring leak kept copying after 7u6, when substring already copied, so it did two copies. Code that used reflection to read String's value field as a char[] broke outright in JDK 9, because the field became a byte[]. Each trick was reasonable for one JDK and wrong for the next.
Notice the pattern: the platform kept improving underneath, and the code that tried to outsmart it either stopped helping or broke.
3Why the Platform Is the Better Place
The platform has advantages no application has. It sees every string in the process, not just one team's. It can change private layouts, as the String examples show, because nobody outside depends on them. Its engineers can test changes across a huge range of workloads and use JIT intrinsics, hand-written machine code for hot operations, which application code cannot. And one improvement reaches every service at once, while application tricks must be written, reviewed and maintained in every codebase.
This does not mean applications never optimise text. It means the default answer is to make the platform do the work, and application-level tricks need evidence that the platform is not enough.
4The Guidance I Would Give Teams
First, upgrade. Being on a current LTS JDK, Java 21 or 25, is itself the largest text memory optimisation available, because it brings compact strings, better collectors and deduplication everywhere. Second, use supported options: choose the collector deliberately, try -XX:+UseStringDeduplication where heaps are full of long-lived strings, and measure heap after GC and CPU before and after.
Third, use application-level canonicalisation only with heap analysis that shows a real problem, such as millions of copies of a small set of values, and keep it small and documented: a comment saying which dump justified it, which JDK it was measured on, and how to verify whether it is still needed. That turns a clever trick into a removable decision. Fourth, keep code free of dependencies on String internals: no reflection on its fields, no assumptions about its layout or about which strings are pooled, so future changes stay free for everyone.
- On a current LTS JDK?YesMeasure the heapNoUpgrade first
- Heap dump shows a real string problem?YesTry supported options: dedup, GC choiceNoDo nothing in code
- Still a problem after measuring?YesNarrow canonicalisation, documentedNoDone
- Re-check after each JDK upgraderemove tricks that no longer pay
4 steps in order. Play them, or step through them one at a time.
5Follow-up questions
What if a team cannot upgrade the JDK soon?
Then supported flags and narrowly targeted fixes are reasonable, but I would tie them to the upgrade plan, with a ticket to re-measure and remove them after the move. The worst outcome is a workaround that outlives the reason for it by ten years.
How do you find old tricks that are now harmful?
Search the codebase for patterns such as intern(), new String(s.substring, reflection on String fields, and custom byte-array text holders, then check each against a current profile. Static analysis rules can flag the obviously obsolete ones, such as copying a substring.
Is there any text optimisation that clearly belongs in the application?
Data-model choices. The platform cannot decide that a service should not hold a whole catalog in memory, or that a value should be an enum rather than free text. Those are design decisions, and they usually save far more than any string-level trick.
6Conclusion
In this answer, we saw that moving the pool to the heap, string deduplication and compact strings each reduced memory for every application without code changes, while application tricks such as aggressive interning, defensive substring copies, custom byte-array text and reflection on String internals became useless, harmful or broken after upgrades. The guidance that follows is to upgrade and use supported platform options first, measure, apply narrow and documented canonicalisation only where heap analysis demands it, and never depend on String internals, while keeping data-model decisions firmly in the application.
Open with
I read that history as evidence that text memory optimisations mostly belong in the platform. Each of those changes, the pool moving to the heap in JDK 7, deduplication in G1 from 8u20 and in the other collectors from 18, and compact strings in 9, helped every application that upgraded, with no code change.
Then, if they want more
Meanwhile the application-level tricks people wrote aged badly. Interning everything was a fix for one era and added lookups and contention in the next. Copying substrings to avoid the old leak meant copying twice after 7u6. Storing text as UTF-8 byte arrays lost its point once compact strings arrived, and reflection on String's value field simply broke in JDK 9.
The platform can see every string, can change private layouts and has JIT intrinsics, so it is better placed. So my guidance to teams is in order. Get onto a current LTS JDK, because that is the biggest optimisation available. Use supported options like deduplication and a deliberate collector choice, and measure heap after GC and CPU.
Only if a heap dump still shows a real problem, such as millions of copies of a few values, add a narrow canonicalisation, documented with the evidence and the JDK it was measured on so it can be removed later. And never depend on String internals.
The one place where the application really should act is the data model, like not holding a whole catalog per instance, because the platform cannot make that choice for us.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 6, Java 7, Java 18
- 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 "Over Java's history the string pool moved from PermGen to the heap, String gained...": https://scrivio-interview-library.netlify.app/java/strings/f8c92033d6/