浅谈Java String内幕

 

String字符串在Java应用中使用很是频繁,只有理解了它在虚拟机中的实现机制,才能写出健壮的应用,本文使用的JDK版本为1.8.0_3。html

常量池

Java代码被编译成class文件时,会生成一个常量池(Constant pool)的数据结构,用以保存字面常量和符号引用(类名、方法名、接口名和字段名等)。java

1数组

2数据结构

3app

4性能

5测试

6优化

package com.ctrip.ttd.whywhy;ui

public class Test {  spa

    public static void main(String[] args) { 

        String test = "test"

    

}

很简单的一段代码,经过命令 javap -verbose 查看class文件中 Constant pool 实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Constant pool:

   #1 = Methodref          #4.#13         // java/lang/Object."<init>":()V

   #2 = String             #14            // test

   #3 = Class              #15            // com/ctrip/ttd/whywhy/test

   #4 = Class              #16            // java/lang/Object

   #5 = Utf8               <init>

   #6 = Utf8               ()V

   #7 = Utf8               Code

   #8 = Utf8               LineNumberTable

   #9 = Utf8               main

  #10 = Utf8               ([Ljava/lang/String;)V

  #11 = Utf8               SourceFile

  #12 = Utf8               test.java

  #13 = NameAndType        #5:#6          // "<init>":()V

  #14 = Utf8               test

  #15 = Utf8               com/ctrip/ttd/whywhy/test

  #16 = Utf8               java/lang/Object

经过反编译出来的字节码能够看出字符串 "test" 在常量池中的定义方式:

1

2

#2 = String             #14            // test

#14 = Utf8              test

在main方法字节码指令中,0 ~ 2行对应代码 String test = "test"; 由两部分组成:ldc #2 和 astore_1。

1

2

3

4

5

6

// main方法字节码指令

 public static void main(java.lang.String[]);

   Code:

      0: ldc           #2                  // String test

      2: astore_1

      3: return

一、Test类加载到虚拟机时,”test”字符串在Constant pool中使用符号引用symbol表示,当调用ldc #2 指令时,若是Constant pool中索引 #2 的symbol还未解析,则调用C++底层的StringTable::intern 方法生成char数组,并将引用保存在StringTable和常量池中,当下次调用ldc #2 时,能够直接从Constant pool根据索引 #2获取 “test” 字符串的引用,避免再次到StringTable中查找。

二、astore_1指令将”test”字符串的引用保存在局部变量表中。

常量池的内存分配 在 JDK六、七、8中有不一样的实现:
一、JDK6及以前版本中,常量池的内存在永久代PermGen进行分配,因此常量池会受到PermGen内存大小的限制。
二、JDK7中,常量池的内存在Java堆上进行分配,意味着常量池不受固定大小的限制了。
三、JDK8中,虚拟机团队移除了永久代PermGen。

字符串初始化

字符串能够经过两种方式进行初始化:字面常量和String对象。

字面常量

1

2

3

4

5

6

7

public class StringTest {

    public static void main(String[] args) {

        String a = "java";

        String b = "java";

        String c = "ja" + "va";

    }

}

经过 “javap -c” 命令查看字节码指令实现:

其中ldc指令将int、float和String类型的常量值从常量池中推送到栈顶,因此a和b都指向常量池的”java”字符串。经过指令实现能够发现:变量a、b和c都指向常量池的 “java” 字符串,表达式 “ja” + “va” 在编译期间会把结果值”java”直接赋值给c。

1

2

3

4

5

6

public class StringTest {

    public static void main(String[] args) {

        String a = "java";

        String c = new String("java");

    }

}

这种状况下,a == c 成立么?字节码实现以下:

其中3 ~ 9行指令对应代码 String c = new String("java"); 实现:
一、第3行new指令,在Java堆上为String对象申请内存;
二、第7行ldc指令,尝试从常量池中获取”java”字符串,若是常量池中不存在,则在常量池中新建”java”字符串,并返回;
三、第9行invokespecial指令,调用构造方法,初始化String对象。

其中String对象中使用char数组存储字符串,变量a指向常量池的”java”字符串,变量c指向Java堆的String对象,且该对象的char数组指向常量池的”java”字符串,因此很显然 a != c,以下图所示:

经过 “字面量 + String对象” 进行赋值会发生什么?

1

2

3

4

5

6

7

8

public class StringTest {

    public static void main(String[] args) {

        String a = "hello ";

        String b = "world";

        String c = a + b;

        String d = "hello world";

    }

}

这种状况下,c == d成立么?字节码实现以下:

其中6 ~ 21行指令对应代码 String c = a + b; 实现:
一、第6行new指令,在Java堆上为StringBuilder对象申请内存;
二、第10行invokespecial指令,调用构造方法,初始化StringBuilder对象;
三、第1四、18行invokespecial指令,调用append方法,添加a和b字符串;
四、第21行invokespecial指令,调用toString方法,生成String对象。

经过指令实现能够发现,字符串变量的链接动做,在编译阶段会被转化成StringBuilder的append操做,变量c最终指向Java堆上新建String对象,变量d指向常量池的”hello world”字符串,因此 c != d。

不过有种特殊状况,当final修饰的变量发生链接动做时,虚拟机会进行优化,将表达式结果直接赋值给目标变量:

1

2

3

4

5

6

7

8

public class StringTest {

    public static void main(String[] args) {

        final String a = "hello ";

        final String b = "world";

        String c = a + b;

        String d = "hello world";

    }

}

指令实现以下:

 

String.intern()原理

String.intern()是一个Native方法,底层调用C++的 StringTable::intern 方法,源码注释:当调用 intern 方法时,若是常量池中已经该字符串,则返回池中的字符串;不然将此字符串添加到常量池中,并返回字符串的引用。

1

2

3

4

5

6

7

8

9

10

package com.ctrip.ttd.whywhy;

class Test {

    public static void main(String args[]) {

        String s1 = new StringBuilder().append("String").append("Test").toString();

        System.out.println(s1.intern() == s1);

 

        String s2 = new StringBuilder().append("ja").append("va").toString();

        System.out.println(s2.intern() == s2);

    }

}

在 JDK6 和 JDK7 中结果不同:

一、JDK6的执行结果:false false
对于这个结果很好理解。在JDK6中,常量池在永久代分配内存,永久代和Java堆的内存是物理隔离的,执行intern方法时,若是常量池不存在该字符串,虚拟机会在常量池中复制该字符串,并返回引用,因此须要谨慎使用intern方法,避免常量池中字符串过多,致使性能变慢,甚至发生PermGen内存溢出。

二、JDK7的执行结果:true false
对于这个结果就有点懵了。在JDK7中,常量池已经在Java堆上分配内存,执行intern方法时,若是常量池已经存在该字符串,则直接返回字符串引用,不然复制该字符串对象的引用到常量池中并返回,因此在JDK7中,能够从新考虑使用intern方法,减小String对象所占的内存空间。

对于变量s1,常量池中没有 “StringTest” 字符串,s1.intern() 和 s1都是指向Java对象上的String对象。
对于变量s2,常量池中一开始就已经存在 “java” 字符串,因此 s2.intern() 返回常量池中 “java” 字符串的引用。

String.intern()性能

常量池底层使用StringTable数据结构保存字符串引用,实现和HashMap相似,根据字符串的hashcode定位到对应的数组,遍历链表查找字符串,当字符串比较多时,会下降查询效率。

在JDK6中,因为常量池在PermGen中,受到内存大小的限制,不建议使用该方法。
在JDK七、8中,能够经过-XX:StringTableSize参数StringTable大小,下面经过几个测试用例看看intern方法的性能。

1

2

3

4

5

6

7

8

9

10

11

12

13

public class StringTest {

    public static void main(String[] args) {

        System.out.println(cost(1000000));

    }

 

    public static long cost(int num) {

        long start = System.currentTimeMillis();

        for (int i = 0; i < num; i++) {

            String.valueOf(i).intern();

        }

        return System.currentTimeMillis() - start;

    }

}

执行一百万次intern()方法,不一样StringTableSize的耗时状况以下:
一、-XX:StringTableSize=1009, 平均耗时23000ms;
二、-XX:StringTableSize=10009, 平均耗时2200ms;
三、-XX:StringTableSize=100009, 平均耗时200ms;
四、默认状况下,平均耗时400ms;

在默认StringTableSize下,执行不一样次intern()方法的耗时状况以下:
一、一万次,平均耗时5ms;
二、十万次,平均耗时25ms;
三、五十万次,平均耗时130ms;
四、一百万次,平均耗时400ms;
五、五百万次,平均耗时5000ms;
六、一千万次,平均耗时15000ms;

从这些测试数据能够看出,尽管在Java 7以上对intern()作了细致的优化,但其耗时仍然很显著,若是无限制的使用intern()方法,将致使系统性能降低,不过能够将有限值的字符串放入常量池,提升内存利用率,因此intern()方法是一把双刃剑。

相关文章
相关标签/搜索