Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does String.intern() really increase performance?

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:

  1. finding hash code associated with the given String
  2. finding an appropriate bucket
  3. comparing the given String to all another Strings in the bucket. Before this step there may be 0 Strings, one String or a LOT OF Strings in the bucket. So if the given String has been previously put in the bucket we will get at least one comparison (that's the best case. Of course there might have been a lot of collisions and now many other Strings are in the bucket)
  4. If the String has been found in the bucket then it should be returned by intern() method
  5. If the String has not been found in the bucket then it should be put in the bucket and returned by intern() method

So 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?

like image 494
Anton Kasianchuk Avatar asked Apr 07 '14 15:04

Anton Kasianchuk


People also ask

Should I use string intern?

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.

What is String intern () When and why should it be used?

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.

What is the purpose of intern () method in the string class?

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.

What is the use of the intern () method?

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.


1 Answers

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.

like image 82
dkatzel Avatar answered Nov 15 '22 12:11

dkatzel