JVM中StringTable概述及调优

JVMStringTable

三、特性

  • 常量池中的字符串仅是符号,第一次用到时才变为对象
  • 利用串池的机制,来避免重复创建字符串对象
  • 字符串变量拼接的原理是StringBuilder(1.8)
  • 字符串常量拼接的原理是编译器优化
  • 可以使用intern方法,主动将串池中还没有的字符串对象放入串池

二、JVM编译期优化

  • StringTablehashtable结构,不能扩容

  • public static void main(String[] args) {
    	String s1 = "a";
    	String s2 = "b";
    	String s3 = "ab";
        //new StringBuilder().append("a").append("b").toString() new String("ab")
    	String s4 = s1 + s2;
    	String s5 = "a" + "b";
    }
    
  • ldc #2 会把a符号变为"a"字符串对象
    ldc #3 会把b符号变为"b"字符串对象
    ldc #4 会把ab符号变为"ab"字符串对象
    
  • 当执行String s4 = s1 + s2;时,相当于执行new String("ab")

  • 当在执行String s5 = "a" + "b";时,编译器会自动优化,同String s3 = "ab";同样

三、字符串延迟加载

  • jvm只有在执行到string创建的行时才会创建对象
  • 创建好的相同字符串不会重复创建
        String s1 = "1";//字符串对象:3153
        String s2 = "2";
        String s3 = "3";
        String s4 = "4";
        String s5 = "5";
        String s6 = "6";

        String s11 = "1";//字符串对象:3159
        String s21 = "2";
        String s31 = "3";
        String s41 = "4";
        String s51 = "5";
        String s61 = "6";//字符串对象:3159

四、intern()

  • 作用:将指定字符串尝试放入StringTable

  • 语法:字符串对象.intern()

  • 示例:

//在堆中创建一个string对象
String s = new String("a") + new String("b");
//将此string对象中的值尝试放入StringTable,并将串池中的对象返回,
String s1 = s.intern();
System.out.println(s1 == "ab"); // true
System.out.println(s == "ab"); // true

注意:当串池中没有intern尝试放入的字符串时,会将字符串放入串池,此时s也变为了串池中的对象

在1.6中会将s复制一份放入串池中,并不会改变s的位置

//在串池中添加"ab"
String x = "ab";
//在堆中创建一个string对象
String s = new String("a") + new String("b");
//将此string对象中的值尝试放入StringTable,并将串池中的对象返回(并没有成功放入)
System.out.println(s1 == x); // true
System.out.println(s == x); // false

注意:当串池中已经存在intern尝试放入的字符串时,则不会将字符串放入串池,此时s还是堆中的对象,所以s不等于x

五、StringTable调优

  • 使用-XX:StringTableSize=大小参数增加桶的数量使StringTable性能增加
  • 实际使用:
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("f:\\test.txt"))));
            String line = null;
            long start = System.nanoTime();
            while (true) {
                line = reader.readLine();
                if (line == null) {
                    break;
                }
                line.intern();
            }
            System.out.println("cost:" + (System.nanoTime() - start) / 1000000);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  • 通过读取文件将文件中的每一行逐行加入到StringTable中,修改桶的大小来测试所需要的时间(文件为8145行)
StringTableSizeTime
128172 ms
1024116 ms
409687 ms

入池去重,以及对性能的优化

  • 源码
    public static void main(String[] args) {
        try {
            List<String> list = new ArrayList<>();
            System.in.read();
            for (int i = 0; i < 30; i++) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("f:\\test.txt"))));
                String line = null;
                long start = System.nanoTime();
                while (true) {
                    line = reader.readLine();
                    if (line == null) {
                        break;
                    }
                    //不入池
                    list.add(line);
                    //入池
                    //list.add(line.intern());
                }
                System.out.println("cost:" + (System.nanoTime() - start) / 1000000);
            }
            System.in.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 不入池

在这里插入图片描述

  • 入池

在这里插入图片描述

string对象大小从6m降到500k,byte数组从16m降到1m

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值