Java-String.intern的深入研究
When---什么时候需要了解String的intern方法:
面试的时候(蜜汁尴尬)!虽然不想承认,不过面试的时候经常碰到这种高逼格的问题来考察我们是否真正理解了String的不可变性、String常量池的设计以及String.intern方法所做的事情。但其实,我们在实际的编程中也可能碰到可以利用String.intern方法来提高程序效率或者减少内存占用的情况,这个我们等下会细说。
What---String.intern方法究竟做了什么:
Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java? Language Specification.
上面是jdk源码中对intern方法的详细解释。简单来说就是intern用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接返回常量池中该对象的引用。否则,在常量池中加入该对象,然后 返回引用。下面的一个例子详细的解释了intern的作用过程:
Now lets understand how Java handles these strings. When you create two string literals:
String name1 = "Ram";
String name2 = "Ram";
In this case, JVM searches String constant pool for value "Ram", and if it does not find it there then it allocates a new memory space and store value "Ram" and return its reference to name1. Similarly, for name2 it checks String constant pool for value "Ram" but this time it find "Ram" there so it does nothing simply return the reference to name2 variable. The way how java handles only one copy of distinct string is called String interning.
How---String.intern方法在jdk1.7之前和之后的区别:
简单的说其实就一个:在jdk1.7之前,字符串常量存储在方法区的PermGen Space。在jdk1.7之后,字符串常量重新被移到了堆中。
Back---重回String设计的初衷:
Java中的String被设计成不可变的,出于以下几点考虑:
-
字符串常量池的需要。字符串常量池的诞生是为了提升效率和减少内存分配。可以说我们编程有百分之八十的时间在处理字符串,而处理的字符串中有很大概率会出现重复的情况。正因为String的不可变性,常量池很容易被管理和优化。
-
安全性考虑。正因为使用字符串的场景如此之多,所以设计成不可变可以有效的防止字符串被有意或者无意的篡改。从java源码中String的设计中我们不难发现,该类被final修饰,同时所有的属性都被final修饰,在源码中也未暴露任何成员变量的修改方法。(当然如果我们想,通过反射或者Unsafe直接操作内存的手段也可以实现对所谓不可变String的修改)。
-
作为HashMap、HashTable等hash型数据key的必要。因为不可变的设计,jvm底层很容易在缓存String对象的时候缓存其hashcode,这样在执行效率上会大大提升。
Deeper---直接来看例子:
首先来试试下面程序的运行结果是否与预想的一致:
1 String s1 = new String("aaa");
2 String s2 = "aaa";
3 System.out.println(s1 == s2); // false
4
5 s1 = new String("bbb").intern();
6 s2 = "bbb";
7 System.out.println(s1 == s2); // true
8
9 s1 = "ccc";
10 s2 = "ccc";
11 System.out.println(s1 == s2); // true
12
13 s1 = new String("ddd").intern();
14 s2 = new String("ddd").intern();
15 System.out.println(s1 == s2); // true
16
17 s1 = "ab" + "cd";
18 s2 = "abcd";
19 System.out.println(s1 == s2); // true
20
21 String temp = "hh";
22 s1 = "a" + temp;
23 // 如果调用s1.intern 则最终返回true
24 s2 = "ahh";
25 System.out.println(s1 == s2); // false
26
27 temp = "hh".intern();
28 s1 = "a" + temp;
29 s2 = "ahh";
30 System.out.println(s1 == s2); // false
31
32 temp = "hh".intern();
33 s1 = ("a" + temp).intern();
34 s2 = "ahh";
35 System.out.println(s1 == s2); // true
36
37 s1 = new String("1"); // 同时会生成堆中的对象 以及常量池中1的对象,但是此时s1是指向堆中的对象的
38 s1.intern(); // 常量池中的已经存在
39 s2 = "1";
40 System.out.println(s1 == s2); // false
41
42 String s3 = new String("1") + new String("1"); // 此时生成了四个对象 常量池中的"1" + 2个堆中的"1" + s3指向的堆中的对象(注此时常量池不会生成"11")
43 s3.intern(); // jdk1.7之后,常量池不仅仅可以存储对象,还可以存储对象的引用,会直接将s3的地址存储在常量池
44 String s4 = "11"; // jdk1.7之后,常量池中的地址其实就是s3的地址
45 System.out.println(s3 == s4); // jdk1.7之前false, jdk1.7之后true
46
47 s3 = new String("2") + new String("2");
48 s4 = "22"; // 常量池中不存在22,所以会新开辟一个存储22对象的常量池地址
49 s3.intern(); // 常量池22的地址和s3的地址不同
50 System.out.println(s3 == s4); // false
// 对于什么时候会在常量池存储字符串对象,我想我们可以基本得出结论: 1. 显示调用String的intern方法的时候; 2. 直接声明字符串字面常量的时候,例如: String a = "aaa";
// 3. 字符串直接常量相加的时候,例如: String c = "aa" + "bb"; 其中的aa/bb只要有任何一个不是字符串字面常量形式,都不会在常量池生成"aabb". 且此时jvm做了优化,不// 会同时生成"aa"和"bb"在字符串常量池中
如果有出入的话,再来看看具体的字节码分析:
1 /**
2 * 字节码为:
3 * 0: ldc #16; //String 11 --- 从常量池加载字符串常量11
4 2: astore_1 --- 将11的引用存到本地变量1,其实就是将s指向常量池中11的位置
5 */
6 String s = "11";
7
8 /**
9 * 0: new #16; //class java/lang/String --- 新开辟了一个地址,存储new出来的对象
10 3: dup --- 将new出来的对象复制了一份到栈顶(也就是s1最终指向的是堆中的另一个存储字符串11的地址)
11 4: ldc #18; //String 11
12 6: invokespecial #20; //Method java/lang/String."<init>":(Ljava/lang/String;)V
13 9: astore_1
14 */
15 String s1 = new String("11");
16
17 /**
18 * 0: new #16; //class java/lang/StringBuilder --- 可以看到jdk对字符串拼接做了优化,先是建了一个StringBuilder对象
19 3: dup
20 4: new #18; //class java/lang/String --- 创建String对象
21 7: dup
22 8: ldc #20; //String 1 --- 从常量池加载了1(此时常量池和堆中都会存字符串对象)
23 10: invokespecial #22; //Method java/lang/String."<init>":(Ljava/lang/String;)V --- 初始化String("1")对象
24 13: invokestatic #25; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
25 16: invokespecial #29; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V --- 初始化StringBuilder对象
26 19: new #18; //class java/lang/String
27 22: dup
28 23: ldc #20; //String 1
29 25: invokespecial #22; //Method java/lang/String."<init>":(Ljava/lang/String;)V
30 28: invokevirtual #30; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
31 31: invokevirtual #34; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
32 34: astore_1 ---从上可以看到实际上常量池目前只存了1
34 36: invokevirtual #38; //Method java/lang/String.intern:()Ljava/lang/String; --- 调用String.intern中,jdk1.7以后,常量池也是堆中的一部分且常量池可以存引用,这里直接存的是s2的引用
35 39: pop --- 这里直接返回的是栈顶的元素
36 */
37 String s2 = new String("1") + new String("1");
38 s2.intern();
39
40 /**
41 * 0: ldc #16; //String abc --- 可以看到此时常量池直接存储的是:abc, 而不会a、b、c各存一份
42 2: astore_1
43 */
44 String s3 = "a" + "b" + "c";
45
46 /**
47 0: new #16; //class java/lang/StringBuilder
48 3: dup
49 4: ldc #18; //String why --- 常量池的why
50 6: invokespecial #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
51 9: ldc #23; //String true --- 常量池的true
52 11: invokevirtual #25; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
53 14: invokevirtual #29; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
54 17: astore_1
55 */
56 String s1 = new StringBuilder("why").append("true").toString();
57 System.out.println(s1 == s1.intern()); // jdk1.7之前为false,之后为true
下面我们延伸一下来讲讲字符串拼接的优化问题:
1 String a = "1";
2 for (int i=0; i<10; i++) {
3 a += i;
4 }
6 0: ldc #16; //String 1
7 2: astore_1
8 3: iconst_0
9 4: istore_2 --- 循环开始
10 5: goto 30
11 8: new #18; //class java/lang/StringBuilder --- 每个循环都建了一个StringBuilder对象,对性能有损耗
12 11: dup
13 12: aload_1
14 13: invokestatic #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
15 16: invokespecial #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
16 19: iload_2
17 20: invokevirtual #29; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
18 23: invokevirtual #33; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
19 26: astore_1
20 27: iinc 2, 1 ---- 计数加1
21 30: iload_2
22 31: bipush 10
23 33: if_icmplt 8
24
25 String a = "1";
26 for (int i=0; i<10; i++) {
27 a += "1";
28 }
29 的字节码为:
30 0: ldc #16; //String 1
31 2: astore_1
32 3: iconst_0
33 4: istore_2
34 5: goto 31
35 8: new #18; //class java/lang/StringBuilder ---还是会每次建立一个StringBuilder对象
36 11: dup
37 12: aload_1
38 13: invokestatic #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
39 16: invokespecial #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
40 19: ldc #16; //String 1 ---和上一个循环的区别也仅仅在于这里是从常量池加载1,
41 21: invokevirtual #29; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
42 24: invokevirtual #33; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
43 27: astore_1
44 28: iinc 2, 1
45 31: iload_2
46 32: bipush 10
47 34: if_icmplt 8
可知,真正的性能瓶颈在于每次循环都建了一个StringBuilder对象
所以我们优化一下 :
50 StringBuilder sb = new StringBuilder("1");
51 for (int i=0; i<10; i++) {
52 sb.append("1");
53 }
对应的字节码为:
55 0: new #16; //class java/lang/StringBuilder -- 在循环直接初始化了StringBuilder对象
56 3: dup
57 4: ldc #18; //String 1
58 6: invokespecial #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
59 9: astore_1
60 10: iconst_0
61 11: istore_2
62 12: goto 25
63 15: aload_1
64 16: ldc #18; //String 1
65 18: invokevirtual #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
66 21: pop
67 22: iinc 2, 1
68 25: iload_2
69 26: bipush 10
70 28: if_icmplt 15
Where---String.intern的使用:
我们直接看一个例子来结束String.intern之旅吧:
1 Integer[] DB_DATA = new Integer[10];
2 Random random = new Random(10 * 10000);
3 for (int i = 0; i < DB_DATA.length; i++) {
4 DB_DATA[i] = random.nextInt();
5 }
6 long t = System.currentTimeMillis();
7 for (int i = 0; i < MAX; i++) {
8 arr[i] = new String(String.valueOf(DB_DATA[i % DB_DATA.length])); // --- 每次都要new一个对象
9 // arr[i] = new String(String.valueOf(DB_DATA[i % DB_DATA.length])).intern(); --- 其实虽然这么多字符串,但是类型最多为10个,大部分重复的字符串,大大减少内存
10 }
11
12 System.out.println((System.currentTimeMillis() - t) + "ms");
13 System.gc();
参考链接:
http://www.360doc.com/content/14/0721/16/1073512_396062351.shtml