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.
intern() 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.
When you assigned a literal to string variable it will automatically be interned. This means the string object will be added to the java string pool automatically. In contrast, when you create a string using new operator you have applied intern() method to add its copy to the java string pool.
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. The java. lang. String class implements Serializable, Comparable and CharSequence interfaces.
This has (almost) nothing to do with string comparison. String interning is intended for saving memory if you have many strings with the same content in you application. By using String.intern()
the application will only have one instance in the long run and a side effect is that you can perform fast reference equality comparison instead of ordinary string comparison (but this is usually not advisable because it is realy easy to break by forgetting to intern only a single instance).
When would I use this function in favor to String.equals()
when you need speed since you can compare strings by reference (== is faster than equals)
Are there side effects not mentioned in the Javadoc?
The primary disadvantage is that you have to remember to make sure that you actually do intern() all of the strings that you're going to compare. It's easy to forget to intern() all strings and then you can get confusingly incorrect results. Also, for everyone's sake, please be sure to very clearly document that you're relying on the strings being internalized.
The second disadvantage if you decide to internalize strings is that the intern() method is relatively expensive. It has to manage the pool of unique strings so it does a fair bit of work (even if the string has already been internalized). So, be careful in your code design so that you e.g., intern() all appropriate strings on input so you don't have to worry about it anymore.
(from JGuru)
Third disadvantage (Java 7 or less only): interned Strings live in PermGen space, which is usually quite small; you may run into an OutOfMemoryError with plenty of free heap space.
(from Michael Borgwardt)
String.intern()
is definitely garbage collected in modern JVMs.
The following NEVER runs out of memory, because of GC activity:
// java -cp . -Xmx128m UserOfIntern
public class UserOfIntern {
public static void main(String[] args) {
Random random = new Random();
System.out.println(random.nextLong());
while (true) {
String s = String.valueOf(random.nextLong());
s = s.intern();
}
}
}
See more (from me) on the myth of non GCed String.intern().
I have recently written an article about String.intern() implementation in Java 6, 7 and 8: String.intern in Java 6, 7 and 8 - string pooling.
I hope it should contain enough information about current situation with string pooling in Java.
In a nutshell:
String.intern()
in Java 6, because it goes into PermGenString.intern()
in Java 7 & Java 8: it uses 4-5x less memory than rolling your own object pool-XX:StringTableSize
(the default is probably too small; set a Prime number)Comparing strings with == is much faster than with equals()
5 Time faster, but since String comparision usually represents only a small percentage of the total execution time of an application, the overall gain is much smaller than that, and the final gain will be diluted to a few percent.
String.intern() pull the string away from Heap and put it in PermGen
String internalized are put in a different storage area : Permanent Generation which is an area of the JVM that is reserved for non-user objects, like Classes, Methods and other internal JVM objects. The size of this area is limited and the is much precious than heap. Being this area smaller than Heap there are more probability to use all the space and get an OutOfMemoryException.
String.intern() string are garbage collected
In the new versions of JVM also internalized string are garbage collected when not referenced by any object.
Keeping in mind the above 3 point you could deduct that String intern() could be useful only in few situation when you do a lot of string comparison, however it is better don't use internal string if you don't know exactly what you are doing ...
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