Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

Where does the string pool live in modern JDKs, how does intern() work, and when is calling it a good or bad idea?

Since JDK 7, interned strings are ordinary objects on the Java heap, and the pool itself is a native hash table inside HotSpot, called the StringTable, that holds references to them. intern() looks up an equal string in that table and returns it, or adds the current one; pooled strings that nothing else references can be garbage collected. Manual interning can save memory when a program holds many duplicates, but it costs a hash lookup and contention on a shared native table, so the collector's string deduplication is often the safer choice.

Why this question is asked

Interning comes up whenever a heap is full of repeated strings, and many teams have old code that interns aggressively because of advice written for Java 6. Interviewers ask this to see whether we know how the pool works today, not in the PermGen era, and whether we can judge when interning helps. In this answer, we will recap what the pool is for, see where it lives now and how intern() works step by step, look at sizing the table, weigh when interning is a good or bad idea, and compare it with string deduplication.

1A Quick Recap of the String Pool

The string pool is the JVM's set of canonical strings: for any text, at most one pooled String object. Every literal in our classes is pooled automatically, which is why two occurrences of "OK" in different classes are the same object. The intern() method lets us put runtime strings into the same set, so equal strings can share one object.

Up to JDK 6, interned strings lived in the permanent generation, PermGen, a fixed-size area for class metadata. It was small and rarely collected, so heavy interning caused OutOfMemoryError: PermGen space. JDK 7 moved interned strings to the normal heap, and JDK 8 removed PermGen entirely in favour of Metaspace for class metadata.

2Where the Pool Lives Now

It helps to separate the two parts. The String objects themselves, with their byte arrays, are regular heap objects, allocated and collected like any other. The pool is only an index: a hash table in the JVM's native memory, the StringTable, whose entries point to those heap objects. The table holds its references weakly, so a pooled String that nothing else points to can be collected, and the GC then removes its entry.

Step throughThe pool today
  1. Our codecalls s.intern() or uses a literal
  2. StringTable (native memory)hash table of weak references, keyed by content
  3. Java heapthe pooled String objects and their byte arrays
  4. Garbage collectorclears entries whose String is no longer referenced

4 layers, from the top down. Step through them one at a time.

Only the index is native; the strings are ordinary heap objects since JDK 7.

3How intern() Works Step by Step

When we call s.intern(), the JVM computes the hash of s and looks in the StringTable. If it finds an entry with equal content, it returns that pooled String. If not, it adds a reference to s itself and returns s. Either way, the result is the canonical instance for that text.

Java
String a = new String("EUR"); // a new object, not the pooled literal
String b = a.intern(); // the pool already has the literal "EUR"
System.out.println(b == "EUR"); // true: intern returned the pooled literal
System.out.println(a == b); // false: a itself was not added
String code = new StringBuilder("X").append("YZ").toString(); // "XYZ", not a literal anywhere
System.out.println(code.intern() == code); // true: code itself became the pooled one

Notice the two outcomes. When an equal string is already pooled, we get that one and our own object stays unpooled, so we must use the returned value. When none exists, our object becomes the canonical one. The lookup is a hash computation plus a probe of a shared table, so each call costs far more than a field read.

4Sizing the Table

Like any hash table, the StringTable slows down when it has too many entries per bucket. Its size can be set with -XX:StringTableSize. Older JDK 7 and 8 builds used a fixed, fairly small default, and teams that interned millions of strings had to raise it. Since JDK 11 the default is larger, and recent JDKs also grow the table automatically when it gets crowded, so manual tuning is rarely needed now; check your version's defaults before copying an old flag.

To see how full it is, we can run jcmd <pid> VM.stringtable, or start the JVM with -XX:+PrintStringTableStatistics, which prints the number of entries, buckets and the average chain length when the JVM exits.

5When Interning Helps and When It Hurts

Interning is a good idea when a program keeps many long-lived strings drawn from a small set of values, such as country codes, status names or field names read from millions of records. Interning collapses them to one object per value, and equality checks can short-circuit on identity. A million references to "ACTIVE" then cost a million pointers instead of a million String objects.

It is a bad idea when values are mostly unique, such as order ids or user input, because each call pays the lookup and adds an entry that saves nothing. It also hurts in hot paths: the table is shared by all threads, so heavy concurrent interning adds contention, and the table becomes a growing structure we have no direct control over. And code that relies on == after interning is fragile, because one missing intern() call breaks it.

intern() or deduplication?
intern()String deduplication
Whereour code, explicit callthe garbage collector, automatic
What is sharedthe whole String objectonly the backing byte array
Identityequal strings become ==unchanged; == stays false
Costhash lookup on every call, shared tablebackground GC work, some CPU
Turn oncode change-XX:+UseStringDeduplication
Deduplication saves most of the memory without code changes.

6String Deduplication: Often the Safer Choice

String deduplication was added to G1 in JDK 8u20, and since JDK 18 it is also available in the Serial, Parallel, ZGC and Shenandoah collectors. With -XX:+UseStringDeduplication, the collector looks at strings that have survived a few collections, and when it finds two with equal content, it points both at a single byte array. The String objects stay separate, so identity does not change and no code can break, but the bulk of the memory, the character data, is shared.

Because it runs in the background and only on long-lived strings, it adds little latency, and it can be switched on and measured with a flag. That is why I would usually try deduplication first and keep explicit interning, or better a bounded canonical map, for the few places where a profile shows a large, small-vocabulary set of strings.

7Follow-up questions

How would you measure duplicate strings in a heap dump?

I take a dump with jcmd <pid> GC.heap_dump /secure/path/app.hprof and open it in Eclipse MAT. Its histogram can group java.lang.String instances by value, which shows each text with its instance count and retained size, and the duplicate strings view in tools such as MAT or JXRay ranks the waste directly. For a quick numeric view without a dump, jcmd <pid> GC.class_histogram shows how much heap String and byte[] take overall, though not how much of it is duplicated.

Can interned strings cause a memory leak today?

Not in the PermGen sense, because unreferenced interned strings are collected. But if our code keeps references to everything it interns, for example in a cache, they all stay, and the StringTable grows with them. The leak is then our reference, not the pool.

Is calling intern() on every JSON field value a good idea?

Usually not. Many values are unique, so we pay the lookup for nothing. Parsers often intern field names already, which are a small vocabulary. For values, a canonical map limited to known vocabularies, or GC deduplication, gives the saving with less cost.

8Conclusion

In this answer, we saw that since JDK 7 interned strings are ordinary heap objects indexed by a native StringTable that holds weak references, so pooled strings can be collected. intern() returns an existing equal string or adds the current one. The table can be sized with -XX:StringTableSize, though recent JDKs size it well and resize it automatically. Interning saves memory for long-lived, repeated values but costs a lookup and shared-table contention, and string deduplication in the collector usually gives most of the saving with no change to identity or code.

What to say in the interviewAbout 90 seconds aloud

Open with

Since JDK 7 the interned strings themselves are normal objects on the heap. What people call the pool is a native hash table in HotSpot, the StringTable, which holds weak references to those objects, so an interned string that nothing references anymore is garbage collected and its entry removed.

Then, if they want more

Before that, in JDK 6, they lived in PermGen, which is why old advice warns about PermGen errors. When I call intern, the JVM hashes the string and looks it up. If an equal one is there, it returns that one, and my object stays unpooled, so I have to use the return value.

If not, it adds my object and returns it. The table size can be set with StringTableSize, and jcmd VM.stringtable shows its statistics, but recent JDKs have a reasonable default and resize it themselves. I would intern only when a profile shows many long-lived copies of a small set of values, like currency codes or status names.

For mostly unique values it is pure cost: a hash lookup per call and contention on a shared table. In most cases I would first turn on string deduplication, which G1 has had since 8u20 and the other collectors since JDK 18.

It shares the backing arrays of equal strings during GC without changing identity, so nothing can break, and I can measure the saving with one flag.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 6, Java 11, 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.
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 "Where does the string pool live in modern JDKs, how does intern() work, and when is...": https://scrivio-interview-library.netlify.app/java/strings/8595ef344e/