I did a little investigation to find out how the String.intern()
method is implemented in java.
I looked at C++ implementation of Intern pool from Open JDK 6 and there I saw a simple HashSet
. For me it meant that when someone are trying to intern a String
the next steps should be done:
String
intern()
methodintern()
methodSo many people say that str1.intern() == str2.intern()
would be faster than str1.equals(str2)
.
But I cannot see the reason it should be faster.
As I can see in case of str1.equals(str2)
we always have two strings comparing char by char in String.equals()
method.
In case of str1.intern() == str2.intern()
, how many comparisons we would have to get or to put the String to/from the pool (right, it can be a lot of comparisons and they are simple char by char comparisons too)?
So in case of str1.intern() == str2.intern()
even if we use ==
to compare Strings we also will have many additional actions such as comparisons described previously.
When I understood it I decided to make some benchmark testing.
The first results shewed me that str1.intern() == str2.intern()
was faster than str1.equals(str2)
.
This behaviour was caused by the fact that String.intern()
method is native so it shouldn't be interpreted every time and String.equals()
is a java method.
So then I decided to use -Xcomp
option to make JVM compile all the code on start.
After that equals shewed a better speed than intern.
I tested it on Java 6 and 7.
So my question is have you ever seen a situation when interning increased speed of String comparison? I yes how can it be?
Or maybe intern()
can only help to save more free memory?
From Java 7 onward, the intern() method has become even more useful because the String pool is relocated to the main heap space of JVM. This will help to further reduce String duplication by using String.
String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.
The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.
The intern() method creates an exact copy of a String that is present in the heap memory and stores it in the String constant pool. Takeaway - intern() method is used to store the strings that are in the heap in the string constant pool if they are not already present.
String.intern()
is meant to decrease memory use.
Only use interned Strings (if ever) when you have many, many multiple copies of the SAME String in memory. by interning the Strings, all those copies will use the same reference.
I have only seen interning Strings being helpful when I have millions of copies of the same String.
As with any kind of optimization, only do it after there is a performance or memory problem and you have profiled it so that you have detected that this is the bottleneck.
See this Blog post for more details on String interning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With