Performance penalty of String.intern()

后端 未结 5 1394
猫巷女王i
猫巷女王i 2020-11-28 04:56

Lots of people talk about the performance advantages of String.intern(), but I\'m actually more interested in what the performance penalty may be.

My main concerns a

5条回答
  •  生来不讨喜
    2020-11-28 05:41

    String.intern become slow is becuase two reasons:
    1. the -XX:StringTableSize limitation.
    In java,it uses a internal hashtable to manage string cache,in java 6,the default StringTableSize value is 1009,which means string.intern is O(the number of string object/ 1009),when more and more string object been created,it's becoming slower.

    \openjdk7\hotspot\src\share\vm\classfile\symbolTable.cpp

    oop StringTable::intern(Handle string_or_null, jchar* name,  
                            int len, TRAPS) {  
      unsigned int hashValue = java_lang_String::hash_string(name, len);  
      int index = the_table()->hash_to_index(hashValue);  
      oop string = the_table()->lookup(index, name, len, hashValue);  
      // Found  
      if (string != NULL) return string;  
      // Otherwise, add to symbol to table  
      return the_table()->basic_add(index, string_or_null, name, len,  
                                    hashValue, CHECK_NULL);  
    }
    

    2. In java 6,the string cache pool is in the perm area,not in the heap,Most of the time,we config the perm size relatively small.

提交回复
热议问题