先看一段代码:
package com.tech.constantpool;
/**
* 演示StringTable垃圾回收
* jvm参数:设置堆空间大小为10M;打印串池统计信息;打印GC信息
* -Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
*
* @author lw
* @since 2021/10/29
*/
public class Demo_6 {
public static void main(String[] args) {
int c = 0;
try {
// for(int i=0;i<10000;i++){
// String.valueOf(i).intern();
// ++c;
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(c);
}
}
}
在IDE配置JVM参数
-Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
运行结果:
"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" -Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc "-javaagent:D:\Tech\resource\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=58788:D:\Tech\resource\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_251\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\rt.jar;D:\Tech\code\tech-jvm\target\classes;C:\Users\lw\.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.4\jackson-annotations-2.12.4.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.12.4\jackson-core-2.12.4.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.12.4\jackson-databind-2.12.4.jar" com.tech.constantpool.Demo_6
0
Heap
PSYoungGen total 2560K, used 1851K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 90% used [0x00000000ffd00000,0x00000000ffeced48,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 7168K, used 0K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 0% used [0x00000000ff600000,0x00000000ff600000,0x00000000ffd00000)
Metaspace used 3248K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 352K, capacity 388K, committed 512K, reserved 1048576K
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 13382 = 321168 bytes, avg 24.000
Number of literals : 13382 = 570928 bytes, avg 42.664
Total footprint : = 1052184 bytes
Average bucket size : 0.669
Variance of bucket size : 0.670
Std. dev. of bucket size: 0.819
Maximum bucket size : 6
StringTable statistics:
Number of buckets : 60013 = 480104 bytes, avg 8.000
Number of entries : 1773 = 42552 bytes, avg 24.000
Number of literals : 1773 = 158616 bytes, avg 89.462
Total footprint : = 681272 bytes
Average bucket size : 0.030
Variance of bucket size : 0.030
Std. dev. of bucket size: 0.172
Maximum bucket size : 3
Process finished with exit code 0
从上往下依次为:堆信息、符号表统计信息、串池统计信息,如果出现过gc,则最上面会打印gc信息。
此时并没有往串池添加10000个字符串,所以没有出现垃圾回收,串池中entries数量为1773
放开注释的代码,把10000个字符串加入到串池
for (int i = 0; i < 10000; i++) {
String.valueOf(i).intern();
++c;
}
打印日志:
"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" -Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc "-javaagent:D:\Tech\resource\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=64123:D:\Tech\resource\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_251\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\rt.jar;D:\Tech\code\tech-jvm\target\classes;C:\Users\lw\.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.4\jackson-annotations-2.12.4.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.12.4\jackson-core-2.12.4.jar;C:\Users\lw\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.12.4\jackson-databind-2.12.4.jar" com.tech.constantpool.Demo_6
[GC (Allocation Failure) [PSYoungGen: 2048K->488K(2560K)] 2048K->691K(9728K), 0.0008035 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
10000
Heap
PSYoungGen total 2560K, used 814K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 15% used [0x00000000ffd00000,0x00000000ffd519c0,0x00000000fff00000)
from space 512K, 95% used [0x00000000fff00000,0x00000000fff7a020,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 203K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 2% used [0x00000000ff600000,0x00000000ff632d70,0x00000000ffd00000)
Metaspace used 3279K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 357K, capacity 388K, committed 512K, reserved 1048576K
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 13408 = 321792 bytes, avg 24.000
Number of literals : 13408 = 571744 bytes, avg 42.642
Total footprint : = 1053624 bytes
Average bucket size : 0.670
Variance of bucket size : 0.672
Std. dev. of bucket size: 0.819
Maximum bucket size : 6
StringTable statistics:
Number of buckets : 60013 = 480104 bytes, avg 8.000
Number of entries : 6832 = 163968 bytes, avg 24.000
Number of literals : 6832 = 401544 bytes, avg 58.774
Total footprint : = 1045616 bytes
Average bucket size : 0.114
Variance of bucket size : 0.115
Std. dev. of bucket size: 0.340
Maximum bucket size : 3
Process finished with exit code 0
出现了GC日志,entries的数量是6832个,说明串池中进行了垃圾回收,释放串池中一些没有引用的字符串。
|