Video in progress

In which part of JVM memory is the string pool, where string literals are stored?

Since Java 7 the string pool is inside the heap. Before that it was in PermGen, which could fill up; now the strings in the pool can be cleaned by the GC like ordinary objects.

📖 Answer

Literals like "hello" and strings that have been intern()ed are recorded in a special hash table inside the JVM. The table itself is in native memory, but the string objects are on the heap.

A short history: in Java 6 and earlier the pool was in PermGen, and a lot of intern() calls gave OutOfMemoryError: PermGen space. In Java 7 it was moved to the heap. In Java 8 PermGen disappeared entirely, and the pool stayed on the heap.

Why this matters:

  • if nobody references the strings in the pool, the GC can collect them;
  • the pool size counts toward -Xmx;
  • the table size can be tuned with -XX:StringTableSize.

To see the pool state: jcmd <pid> VM.stringtable.

Practical advice: there is usually no need to intern() user input in bulk. With G1, -XX:+UseStringDeduplication merges the internal arrays of duplicate strings by itself.