Java 8 String deduplication vs. String.intern()

Hilikus picture Hilikus · Sep 30, 2015 · Viewed 8.8k times · Source

I am reading about the feature in Java 8 update 20 for String deduplication (more info) but I am not sure if this basically makes String.intern() obsolete.

I know that this JVM feature needs the G1 garbage collector, which might not be an option for many, but assuming one is using G1GC, is there any difference/advantage/disadvantage of the automatic deduplication done by the JVM vs manually having to intern your strings (one obvious one is the advantage of not having to pollute your code with calls to intern())?

This is especially interesting considering that Oracle might make G1GC the default GC in java 9

Answer

ZhongYu picture ZhongYu · Sep 30, 2015

With this feature, if you have 1000 distinct String objects, all with the same content "abc", JVM could make them share the same char[] internally. However, you still have 1000 distinct String objects.

With intern(), you will have just one String object. So if memory saving is your concern, intern() would be better. It'll save space, as well as GC time.

However, the performance of intern() isn't that great, last time I heard. You might be better off by having your own string cache, even using a ConcurrentHashMap ... but you need to benchmark it to make sure.