栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JVM专题(八)-StringTable调优

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JVM专题(八)-StringTable调优

文章目录
  • 1.增加桶的个数
  • 2.考虑是否将字符串对象放入池中,即用intern

1.增加桶的个数

因为StringTable(串池)底层是hashmap,实现了去重的功能,所以它的性能跟桶的个数(链表的节点数息息相关),桶数越多,性能越强,所以,当我们数据量较大的时候,适当增加桶的个数,能有效的提高效率。

相关命令

-Xms500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=20000

演示代码

public class Demo1_24 {

    public static void main(String[] args) throws IOException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("linux.words"), "utf-8"))) {
            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);
        }
    }
}

由于入池时候会先去查找StringTable中有无这个字符串,hash表的的寻找,桶个数越多越快。

执行耗时:

当桶个数为1009时,耗费5141毫秒

2.考虑是否将字符串对象放入池中,即用intern

相关命令

-Xms500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=20000

演示代码

public class Demo1_25 {

    public static void main(String[] args) throws IOException {

        List address = new ArrayList<>();
        System.in.read();
        for (int i = 0; i < 10; i++) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("linux.words"), "utf-8"))) {
                String line = null;
                long start = System.nanoTime();
                while (true) {
                    line = reader.readLine();
                    if(line == null) {
                        break;
                    }
                    address.add(line);
                }
                System.out.println("cost:" +(System.nanoTime()-start)/1000000);
            }
        }
        System.in.read();
    }
}


内存占用情况:

入池操作:

可以通过intern方法减少重复入池,保证相同的地址在StringTable中只存储一份

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820415.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号