Java String Immutability and Using same string value to create a new string

前端 未结 4 734
醉酒成梦
醉酒成梦 2021-01-19 14:46

I know that the title of the question is not very clear, sorry about that, did not know how to put it up. I have a very basic java implementation question which I want to fo

4条回答
  •  一整个雨季
    2021-01-19 15:00

    String name = new String("Sambhav");

    String myName = new String("Sambhav");

    Will Java still be able to catch that the string are the same and just point myName to the same object as created in the previous statement?

    The JVM manages to keep only one reference of equal String objects by computing a hash.

    Those String objects are kept in a String pool.

    String pooling

    String pooling (sometimes also called as string canonicalisation) is a process of replacing several String objects with equal value but different identity with a single shared String object.

    You can achieve this goal by keeping your own Map (with possibly soft or weak references depending on your requirements) and using map values as canonicalised values.

    Or you can use String.intern() method which is provided to you by JDK.

    Quick string pool differences by JVM version

    In Java 6, this String pool was located in the Perma Gen memory. This memory is usually small and limited. Also, here the String.intern() shouldn't be used because you can run out of memory.

    In Java 7 and 8 it was taken out to the heap memory and implemented with a hash-table like data structure.

    Since hash-table like structures (HashMap, WeakHashMap) use a computed hash to access the entry in constant complexity, the entire process is very fast.

    As mentioned in this article:

    • 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. It will allow String.intern() to run in the constant time and requires a rather small memory consumption per interned string (explicitly used Java WeakHashMap will consume 4-5 times more memory for the same task).

    • The default value of -XX:StringTableSize parameter is 1009 in Java 7 and around 25-50K in Java 8.

提交回复
热议问题