JVM 字符串常量池中的垃圾回收
Java JVM 大约 2253 字字符串常量池
字符串常量池也存在垃圾回收,当Full GC
时,垃圾收集器同样会收集常量池中的String
常量字符串。
示例
添加-XX:+PrintStringTableStatistics
打印StringTable
的统计信息。
创建100
个字符串。
/**
* -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
*/
public class StringTableStatDemo {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
("aaa" + i).intern();
}
}
}
输出:
StringTable statistics:
Number of buckets : 1009 = 4036 bytes, avg 4.000
Number of entries : 971 = 11652 bytes, avg 12.000
Number of literals : 971 = 50992 bytes, avg 52.515
Total footprint : = 66680 bytes
Average bucket size : 0.962
Variance of bucket size : 0.902
Std. dev. of bucket size: 0.950
Maximum bucket size : 5
创建10000
个字符串。
/**
* -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
*/
public class StringTableStatDemo {
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
("aaa" + i).intern();
}
}
}
输出:
StringTable statistics:
Number of buckets : 1009 = 4036 bytes, avg 4.000
Number of entries : 10910 = 130920 bytes, avg 12.000
Number of literals : 10910 = 521008 bytes, avg 47.755
Total footprint : = 655964 bytes
Average bucket size : 10.813
Variance of bucket size : 4.753
Std. dev. of bucket size: 2.180
Maximum bucket size : 19
创建1000000
个字符串。
/**
* -XX:+PrintStringTableStatistics -XX:+PrintGCDetails
*/
public class StringTableStatDemo {
public static void main(String[] args) {
for (int i = 0; i < 1000000; i++) {
("aaa" + i).intern();
}
}
}
输出:
StringTable statistics:
Number of buckets : 1009 = 4036 bytes, avg 4.000
Number of entries : 46405 = 556860 bytes, avg 12.000
Number of literals : 46405 = 2232632 bytes, avg 48.112
Total footprint : = 2793528 bytes
Average bucket size : 45.991
Variance of bucket size : 29.107
Std. dev. of bucket size: 5.395
Maximum bucket size : 57
现象
统计信息中的Number of entries
和Number of literals
表示字符串的数量,当创建过多的字符串发生Full GC
后,字符串常量池中的字符串也被回收了一部分。
阅读 3919 · 发布于 2021-08-17
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Android ListView 隐藏分割线阅读 2569
-
SpringMVC 请求体接收任意格式参数阅读 1090
-
Linux 命令之解压缩阅读 2368
-
Spring Boot RabbitMQ Execution of Rabbit message listener failed阅读 8113
-
PostgreSQL update from 根据 A 表更新 B 表阅读 3744
-
算法每日一题20190623:最长公共前缀阅读 2546
-
Maven pom.xml 中设置 repository 不起作用的解决方法阅读 2340
-
Java 判断字符串是否是数字阅读 350
-
IDEA Debug 异常断点阅读 1203
-
GitHub 访问慢解决方案阅读 2584