StringTable性能调优

如果项目中有很多常量或者字符串,适当调整参数
调整-XX:StringTableSize=桶个数
如:-XX:StringTableSize=1009

在这里插入图片描述
如果存在堆中的字符串比较多,且有很多重复的数据,那么可以使用intern 减少内存占用。

/**
 * 演示 intern 减少内存占用
 * -XX:StringTableSize=200000 -XX:+PrintStringTableStatistics
 * -Xsx500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=200000
 */
public class Demo1_25 {
    
    

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

        List<String> 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.intern());
                }
                System.out.println("cost:" +(System.nanoTime()-start)/1000000);
            }
        }
        System.in.read();
    }
}

猜你喜欢

转载自blog.csdn.net/u014496893/article/details/114578638
今日推荐