String Pool | Java
String pool in Java is a pool of String literals and interned Strings in JVM for efficient use of String object. Since String is Immutable Class In Java, it makes sense to cache and shares them in JVM. Java creators introduced this String pool construct as an optimization on the way String objects are allocated and stored. It is a simple implementation of the Flyweight pattern, which in essence, says this: when a lot of data is common among several objects, it is better to just share the same instance of that data than creating several different “copies” of it.
There are mainly two ways to create a string.
1. Direct Initialization
String myString = "Hello!! Pumpkin";
2. Using constructor
String myString = new String("Hello!! Pumpkin")
Whenever a String is created using direct initialization, the JVM checks for the String in the Java Heap memory.
If a String with the same value is found, then only the reference of the String that is already present is pointed to the new String that we want to create.
If the string is created using new constructor then it will get allocated new space in heap regardless of the object exists in pool or not. The same is illustrated in the below image.
As the string is immutable, when you try to modify the value of the String that you have created from String Pool, a new String is created or reference from String Pool.
String Pool helps the java heap memory to be used optimally, as few Strings are created on the Java Heap. This helps in less work for the garbage collector.
Let's see JVM string pool implementation in Java 6, 7 and 8
The string pool is implemented as a fixed capacity HashMap with each bucket containing a list of strings with the same hashCode. The default size of the table/pool is 1,009 buckets. It was a constant in the early versions of Java 6 and became configurable between Java6u30 and Java6u41. You need to specify –
String pool was located in PermGen Space which is very limited.
Java7 (until Java7u40)
String pool moved from PermGen space to heap space. You are limited only by a much higher heap size. It means that you can set the string pool size to a rather high value in advance (this value depends on your application requirements). As a rule, one starts worrying about the memory consumption when the memory data set size grows to at least several hundred megabytes. In this situation, allocating 8-16 MB for a string pool with one million entries seems to be a reasonable trade off.
String pool size was increased in Java7u40 (this was a major performance update) to 60013. This value allows you to have approximately 30,000 distinct strings in the pool before you start experiencing collisions. Java 8 still accepts –
That's all for Java String Pool. You may also wants to see out other articles on
- Access Modifiers
- transient Keyword
- Wrapper Classes – Autoboxing and Unboxing, IntegerCache and Immutability
- HashMap vs Hashtable Vs SynchronizedMap Vs ConcurrentHashMap
- interface VS abstract class
- What is the use of private constructor in java?
You can also read codepumpkin's series of articles on JVM Internals.
If you guys have any suggestions or queries, feel free drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
Comments and QueriesIf you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.