0%

java | StringTable 垃圾回收

StringTable 是会存在垃圾回收的。

使用 VM 参数 -Xmx20m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc 打印垃圾回收。

初始代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.redisc;

import lombok.extern.slf4j.Slf4j;

/**
* 演示 StringTable 垃圾回收
* -Xmx30m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
*/

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

public static void main(String[] args) {
int i = 0;
try {
} catch (Throwable e) {
e.printStackTrace();
} finally {
System.out.println(i);
}
}

}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
Heap
PSYoungGen total 9216K, used 6087K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
eden space 8192K, 74% used [0x00000007bf600000,0x00000007bfbf1c88,0x00000007bfe00000)
from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 20480K, used 0K [0x00000007be200000, 0x00000007bf600000, 0x00000007bf600000)
object space 20480K, 0% used [0x00000007be200000,0x00000007be200000,0x00000007bf600000)
Metaspace used 4889K, capacity 5064K, committed 5248K, reserved 1056768K
class space used 532K, capacity 564K, committed 640K, reserved 1048576K
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 18966 = 455184 bytes, avg 24.000
Number of literals : 18966 = 717032 bytes, avg 37.806
Total footprint : = 1332304 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 : 60013 = 480104 bytes, avg 8.000
Number of entries : 2057 = 49368 bytes, avg 24.000
Number of literals : 2057 = 134528 bytes, avg 65.400
Total footprint : = 664000 bytes
Average bucket size : 0.034
Variance of bucket size : 0.034
Std. dev. of bucket size: 0.185
Maximum bucket size : 2

里面有一个

1
2
Number of entries       :      2057 =     49080 bytes, avg  24.000
Number of literals : 2057 = 133688 bytes, avg 65.373

就是 StringTable 里面的长度

增加 StringTable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.redisc;

import lombok.extern.slf4j.Slf4j;

/**
* 演示 StringTable 垃圾回收
* -Xmx30m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
*/

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

public static void main(String[] args) {
int i = 0;
try {
for (int j = 0; j < 100; j++) {
String.valueOf(j).intern();
i++;
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
System.out.println(i);
}
}

}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
100
Heap
PSYoungGen total 9216K, used 6009K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
eden space 8192K, 73% used [0x00000007bf600000,0x00000007bfbde578,0x00000007bfe00000)
from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 20480K, used 0K [0x00000007be200000, 0x00000007bf600000, 0x00000007bf600000)
object space 20480K, 0% used [0x00000007be200000,0x00000007be200000,0x00000007bf600000)
Metaspace used 4894K, capacity 5068K, committed 5248K, reserved 1056768K
class space used 532K, capacity 564K, committed 640K, reserved 1048576K
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 18967 = 455208 bytes, avg 24.000
Number of literals : 18967 = 717048 bytes, avg 37.805
Total footprint : = 1332344 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 : 60013 = 480104 bytes, avg 8.000
Number of entries : 2145 = 51480 bytes, avg 24.000
Number of literals : 2145 = 138760 bytes, avg 64.690
Total footprint : = 670344 bytes
Average bucket size : 0.036
Variance of bucket size : 0.036
Std. dev. of bucket size: 0.189
Maximum bucket size : 2

可以看出之前的地方变成了

1
2
3
4
Number of buckets       :     60013 =    480104 bytes, avg   8.000
Number of entries : 2145 = 51480 bytes, avg 24.000
Number of literals : 2145 = 138760 bytes, avg 64.690
Total footprint : = 670344 bytes

比之前多了接近 100 个。

大规模 StringTable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.redisc;

import lombok.extern.slf4j.Slf4j;

/**
* 演示 StringTable 垃圾回收
* -Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
*/

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

public static void main(String[] args) {
int i = 0;
try {
for (int j = 0; j < 200000; j++) {
String.valueOf(j).intern();
i++;
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
System.out.println(i);
}
}

}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[GC (Allocation Failure) [PSYoungGen: 8192K->992K(9216K)] 8192K->1345K(29696K), 0.0017253 secs] [Times: user=0.01 sys=0.01, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 9184K->1024K(9216K)] 9537K->1385K(29696K), 0.0030589 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
200000
Heap
PSYoungGen total 9216K, used 1427K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
eden space 8192K, 4% used [0x00000007bf600000,0x00000007bf664cb0,0x00000007bfe00000)
from space 1024K, 100% used [0x00000007bff00000,0x00000007c0000000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 20480K, used 361K [0x00000007be200000, 0x00000007bf600000, 0x00000007bf600000)
object space 20480K, 1% used [0x00000007be200000,0x00000007be25a4d0,0x00000007bf600000)
Metaspace used 4927K, capacity 5068K, committed 5248K, reserved 1056768K
class space used 533K, capacity 564K, committed 640K, reserved 1048576K
SymbolTable statistics:
Number of buckets : 20011 = 160088 bytes, avg 8.000
Number of entries : 19047 = 457128 bytes, avg 24.000
Number of literals : 19047 = 719560 bytes, avg 37.778
Total footprint : = 1336776 bytes
Average bucket size : 0.952
Variance of bucket size : 0.948
Std. dev. of bucket size: 0.974
Maximum bucket size : 7
StringTable statistics:
Number of buckets : 60013 = 480104 bytes, avg 8.000
Number of entries : 7437 = 178488 bytes, avg 24.000
Number of literals : 7437 = 436696 bytes, avg 58.719
Total footprint : = 1095288 bytes
Average bucket size : 0.124
Variance of bucket size : 0.127
Std. dev. of bucket size: 0.356
Maximum bucket size : 3

StringTable 的数量是

1
2
3
4
Number of buckets       :     60013 =    480104 bytes, avg   8.000
Number of entries : 7437 = 178488 bytes, avg 24.000
Number of literals : 7437 = 436696 bytes, avg 58.719
Total footprint : = 1095288 bytes

远小于 200000 的数量,这是因为开头就进行了垃圾回收

1
2
[GC (Allocation Failure) [PSYoungGen: 8192K->992K(9216K)] 8192K->1345K(29696K), 0.0017253 secs] [Times: user=0.01 sys=0.01, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 9184K->1024K(9216K)] 9537K->1385K(29696K), 0.0030589 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
请我喝杯咖啡吧~