The String intern () Detailed

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_36964056/article/details/102770585

1.JDK in comments

Here Insert Picture Description
Returns a string object canonical FIG. Private String class maintains a pool of strings, initially empty.
When calling intern method, if the pool already contains this string (the equals OK), the pool string is returned. Otherwise, this String object is added to the pool and returns the String object.
For any two strings s and t, if and only if s.equals (t) is true, s.intern () == t.intern () was true.
All literal strings and string constant expressions are intern.
Return Value: same content String object with the object to ensure that the unique string from the pool.

You can see from the comments came out, intern there are two cases
1. If there
   exists a reference to determine the content or constant,
    if it is a reference,
     returns a reference pointing to the heap address space objects,
    if it is constant,
     direct return constant pool constants
  2. If not,
   copy the current object references to the constant pool, and returns a reference to the current object

2.intern or referenced in the storage pool Constants

1.在堆上创建对象,在常量池创建引用
String a = new String("A") + new String("B");//在堆上创建对象AB
// a.intern();//将该对象AB的引用保存到常量池上
System.out.println(a == a.intern());//true
2.在常量池创建常量
String b="abc";//在常量池中创建常量abc
String c="abc";//直接返回常量abc
System.out.println(b==c);
3.在堆上创建对象,在常量池上创建常量
 String a3 = new String("AA");//在堆上创建对象AA,在常量池中创建常量AA
4.对象是否共存

3. The intern on data storage

Its structure is generally realized: JAVA using jni call StringTable the intern method c ++ implementation,
StringTable with the intern method to achieve HashMap in Java is similar, but not automatic expansion. The default size is 1009.
Note that, the String String Pool is a fixed size Hashtable, the default value is the size of length 1009, if put into String
Pool of String is very large, it will cause serious Hash conflict, resulting in the list will be very long, long list after the impact of the direct result is that performance will drop sharply when calling String.intern (because one by one to find).
In
jdk6 the StringTable is fixed, the length is 1009, so the string constant pool is too large will lead to decreases quickly. In jdk7, StringTable length can be specified via a parameter:
-XX: StringTableSize = 99 991 I believe a lot of JAVA programmers are doing similar String s = new String ( "abc ") This statement creates a topic of several objects.
This topic is mainly to investigate a string constant pool for programmers to grasp objects or not. The above statement is the creation of two objects, the first object is in the constant pool "abc" string is stored, the second object in JAVA
String objects in the Heap.

After 1.JDK8 StringTable The default size of the constant pool

关于StringTableSize,使用虚拟机参数 -XX:+PrintFlagsInitial或者
-XX:+PrintFlagsFinal看看Java8设置的默认值:
uintx StringTableSize = 60013 {product}
链接法处理冲突:
template <MEMFLAGS F> inline void BasicHashtable<F>::add_entry(int index, BasicHashtableEntry<F>* entry) {
  entry->set_next(bucket(index));
  _buckets[index].set_entry(entry);
  ++_number_of_entries;
}

StringTable the intern method is similar with the realization HashMap in Java, but not automatic expansion. The default size is 60013. If the String into the String Pool is very large, it will cause serious Hash conflict, resulting in the list will be very long, and the impact will be long after the list is a direct result of a substantial decline in performance when calling String.intern.

4. Analysis and summary

After the string of constant values and the String.intern Java7 () mechanism:
1) The string constants and string literals expression will Intern, added to the constant pool, for example, new String ( "1"); there are two operation: New string heap objects; "1" is added to the constant pool
2) the string s4 = "11" that will be the new "11" in the object string constant pool
3) String.intern (), as has been transferred to the heap, so no need to store a copy of the object in constant pool, so a direct memory heap String object.
intern () method of the corresponding object is to prompt string JVM cached for reuse. JDK6 use this version of history, does not recommend the use of a large number of intern. Because the cached string PermGen permanent generations, and this space is very limited, basically not take care of, so if used improperly, there will be other than GC FullGC OOM.
Subsequent versions of the cache on the heap. Generation and permanent JDK8 is replaced MetaSpace.
intern () is an explicit duplication mechanism. Side effects:
1) an explicit call to trouble
2) difficult to ensure efficiency, it is difficult to clearly expected string duplication
in the Oracle JDK 8u20, we launched a new feature, re-align strings in the G1 GC. The strings of the same data point to the same data, is to change the underlying JVM, Java class libraries do not need to be modified.
Specifies G1 GC, and turn on -XX: + UseStringDeduplication.

Guess you like

Origin blog.csdn.net/weixin_36964056/article/details/102770585