- Stay away from
String.intern()
method on Java 6 due to a fixed size memory area (PermGen) used for JVM string pool storage. - Java 7 and 8 implement the string pool in the heap memory. It means that you are limited by the whole application memory for string pooling in Java 7 and 8.
- Use
-XX:StringTableSize
JVM parameter in Java 7 and 8 to set the string pool map size. It is fixed, because it is implemented as a hash map with lists in the buckets. Approximate the number of distinct strings in your application (which you intend to intern) and set the pool size equal to some prime number close to this value multiplied by 2 (to reduce the likelihood of collisions). It will allowString.intern
to run in the constant time and requires a rather small memory consumption per interned string (explicitly used JavaWeakHashMap
will consume 4-5 times more memory for the same task). - The default value of
-XX:StringTableSize
parameter is 1009 in Java 6 and Java 7 until Java7u40. It was increased to 60013 in Java 7u40 (same value is used in Java 8 as well). - If you are not sure about the string pool usage, try
-XX:+PrintStringTableStatistics
JVM argument. It will print you the string pool usage when your program terminates.
Ref: http://java-performance.info/string-intern-in-java-6-7-8/
No comments:
Post a Comment