浅谈Java String内幕

 

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

常量池

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

1

2

3

4

5

6

package com.ctrip.ttd.whywhy;

public class Test { 

    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

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

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

常量池的内存分配 在 JDK6、7、8中有不同的实现:
1、JDK6及之前版本中,常量池的内存在永久代PermGen进行分配,所以常量池会受到PermGen内存大小的限制。
2、JDK7中,常量池的内存在Java堆上进行分配,意味着常量池不受固定大小的限制了。
3、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"); 实现:
1、第3行new指令,在Java堆上为String对象申请内存;
2、第7行ldc指令,尝试从常量池中获取”java”字符串,如果常量池中不存在,则在常量池中新建”java”字符串,并返回;
3、第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; 实现:
1、第6行new指令,在Java堆上为StringBuilder对象申请内存;
2、第10行invokespecial指令,调用构造方法,初始化StringBuilder对象;
3、第14、18行invokespecial指令,调用append方法,添加a和b字符串;
4、第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 中结果不一样:

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

2、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中,受到内存大小的限制,不建议使用该方法。
在JDK7、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的耗时情况如下:
1、-XX:StringTableSize=1009, 平均耗时23000ms;
2、-XX:StringTableSize=10009, 平均耗时2200ms;
3、-XX:StringTableSize=100009, 平均耗时200ms;
4、默认情况下,平均耗时400ms;

在默认StringTableSize下,执行不同次intern()方法的耗时情况如下:
1、一万次,平均耗时5ms;
2、十万次,平均耗时25ms;
3、五十万次,平均耗时130ms;
4、一百万次,平均耗时400ms;
5、五百万次,平均耗时5000ms;
6、一千万次,平均耗时15000ms;

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

 

转载于:https://my.oschina.net/u/729210/blog/755067

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值