Why Java’s String.intern Boosts Performance: A Deep Dive into Memory Pools
This article explores the purpose and inner workings of Java's String.intern method, detailing how it interacts with the string constant pool across JDK versions, illustrating memory layout differences, code examples, and practical guidelines for when to apply intern for performance gains.
Introduction
During interviews candidates are often asked about the behavior of String.intern() and its impact on memory. The author discovered its usage in Nacos's NamingUtils.getGroupedName method, which concatenates a service name and group name and then calls intern().
Definition of intern()
public native String intern();is a native method that adds a string to the JVM's string constant pool if it does not already exist, returning the reference to the pooled instance.
String and Constant Pool Memory Structure
Strings can be created via literals or new. Literals are looked up in the constant pool first; if absent, a pool entry is created. new String(...) always allocates an object on the heap, with a separate reference in the pool for the literal part.
JDK6 memory layout (PermGen) and JDK7+ layout (heap) differ; diagrams are shown below.
When new String("hello") is used, the heap holds the object while the literal may already reside in the pool.
String str2 = new String("hello");String Concatenation
Compile‑time constant concatenation (e.g., "hello" + "world") is optimized by the compiler and the result is placed directly into the constant pool, making "hello" + "world" == "helloworld" true.
String s1 = "hello" + "world";
String s2 = "helloworld";
System.out.println(s1 == s2); // trueRuntime concatenation involving new String objects is transformed into StringBuilder operations, producing a heap object that is not automatically pooled.
String s1 = new String("he") + new String("llo");The resulting heap string can be added to the pool by calling intern(), which is exactly what the Nacos code does to improve lookup speed.
Behavior of intern() in Different JDK Versions
JDK1.6 : If the string is not already in the pool, intern() copies it into the PermGen area and returns the pool reference. Comparing the original heap object with the pooled reference yields false.
String str1 = new String("abc");
String str1Pool = str1.intern();
System.out.println(str1Pool == str1); // falseWhen the pool already contains the literal, the returned reference differs from the heap object.
JDK1.7 and later : If the string is absent from the pool, the VM stores a reference to the existing heap object instead of copying it. Consequently, the comparison str1Pool == str1 evaluates to true.
String str1 = new String("a") + new String("bc");
String str1Pool = str1.intern();
System.out.println(str1Pool == str1); // trueFrom JDK1.7 onward the string pool resides on the heap, and the default StringTable size can be adjusted with -XX:StringTableSize.
-XX:StringTableSize=99991StringTable Implementation
The underlying StringTable is a fixed‑size hash table (default 1009 entries in JDK6). Excessive unique strings cause hash collisions and degrade intern() performance.
When to Use intern()
Interning is beneficial when a particular string value is reused many times, such as service names in Nacos, because it reduces memory usage and speeds up equality checks. However, the extra step adds slight overhead, which is usually negligible compared to GC time.
Conclusion
Understanding the mechanics of String.intern(), the evolution of the string constant pool across JDK versions, and the memory implications helps developers make informed decisions about when to apply interning for performance optimization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
