3

java | StringTable 性能调优

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/19/java121/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

StringTable 底层是一个 Hash 表。

读取大批量文件

package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


@Slf4j(topic = "c.Run")
public class Run {

public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/main/resources/words"), "utf-8"))) {
String line = null;
long start = System.nanoTime();
while (true) {
line = reader.readLine();
if (line == null) {
break;
}
}
System.out.println("cost:" + ((System.nanoTime() - start) / 1000000));
}
}

}
cost:27

调整 StringTable 大小

VM 增加 -XX:StringTableSize=20000 -XX:+PrintStringTableStatistics

cost:20
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 18968 = 455232 bytes, avg 24.000
Number of literals : 18968 = 717080 bytes, avg 37.805
Total footprint : = 1332400 bytes
Average bucket size : 0.948
Variance of bucket size : 0.944
Std. dev. of bucket size: 0.971
Maximum bucket size : 7
StringTable statistics:
Number of buckets : 20000 = 160000 bytes, avg 8.000
Number of entries : 2069 = 49656 bytes, avg 24.000
Number of literals : 2069 = 135320 bytes, avg 65.404
Total footprint : = 344976 bytes
Average bucket size : 0.103
Variance of bucket size : 0.103
Std. dev. of bucket size: 0.321
Maximum bucket size : 4

可以发现事件耗时为 20

如果调整大小到 1200,「最小为 1009」输出为 23

所以,如果一个程序中有很多串池数据,可以通过调整大小,来进行性能调优。

如果一个程序需要大量的字符串,并且很多字符串都是重复的,可以通过入池,来提升性能。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK