The string is one of the most used classes in java. The string is used in almost every aspect of programming. We use String class in hashmap for creating a key, creating variables and many more places. Hence, it becomes a priority for java to optimise the amount of memory that a String class object uses.
To
achieve these, concepts like String pool and String interning were introduced.
Today in this tutorial we will discuss these topics. Let’s get started.
What is String pool
String pool is a place where a collection
of String objects are stored. There are mainly two ways by which we can create
String object first is by using the new keyword and second is
by placing the String into double-quotes. String pool is possible only because of
the Immutability of String.
If we create a String object by
using new keyword then the string object will be created into
heap memory. However, with the help of intern() method, we can
put the String object into the String pool or refer it to the already created
object.
On the other hand, if we create the
String object using double quotes first the String pool will be referred and if
the object is present then the reference will be returned else new object will
be created and then the reference will be returned. String pooling help in
saving a lot of space at runtime.
The below diagram explains the above
two statements more clearly.
String Interning
The process of Storing
only one copy of every literal String in the pool is called String Interning.
String Interning is possible because of the Immutability of String. String
Interning saves a lot of memory at runtime.
After creating the String object the JVM will look for the same value into the String pool, if found, the Java compiler will return the reference to its memory address without new allocation. If not, string interning will take place and reference will be returned.
Let’s see some example on the difference between String
Literal and String Object
Example 1:
Creating a string
object using String Literal.
String first = “Java8”;
String second = “Java8”;
System.out.println(first == second)
// true
Example 2:
Creating string
object using the new operator
String third = new String(“Java8”);
String fourth = new String(“Java8”);
System.out.println(third == fourth)
// false
Example 3:
Comparing String
literal with String Objects created with new Operator.
String fifth = “Java8”;
String sixth = new String(“Java8”);
System.out.println(fifth == sixth) // false
In general, we should use String interning whenever possible. JVM will have a chance to optimise the code.
The intern() method
The intern() method helps us to do the
manual interning. We can call intern() method
on the String object that we want to intern. This is called manual interning. Manual
interning will store the string object into string pool and when will return its
reference whenever we need.
Garbage Collection
Before Java7, PermGen space is used to store the Java
String Pool. The PermGen can not be
extended at runtime as it has fixed space hence, no garbage collection is
allowed.
If we are
interning the string into PermGen instead of Java heap we can get OutOfMemory error if we intern too many
String.
From Java7, the
string interning is taking place at heap memory hence garbage collection is
allowed. Hence, the risk of OutOfMemory error
will be reduced as the unused String objects will be removed at the runtime
making the space for more String literals.
Changes in Java9
Until Java 8,
strings were known internally as an array of character - char[], encoded in
UTF-16, so that each character would use two bytes of memories.
A new
representation has been provided with Java 9, called Compact Strings. This new
format will select the appropriate encoding between char[] and bytes[] depending
on the stored content.
The new string representation will use UTF-16 encoding only when needed, significantly reducing the amount of heap memory, reducing waste collectors overhead on JVM.
Related Articles:
1. Core java interview questions.
2. Java 8 interview questions.
3. Internal working of Hashmap in java.
0 Comments