StringTable performance tuning

If there are many constants or strings in the project, adjust the parameters appropriately.
Adjust -XX: StringTableSize = the number of buckets,
such as: -XX: StringTableSize=1009

Insert picture description here
If there are many strings in the heap and there are many duplicate data, you can use intern to reduce memory usage.

/**
 * 演示 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();
    }
}

Guess you like

Origin blog.csdn.net/u014496893/article/details/114578638