Java-G1不处理软引用
作者:互联网
这是我简单的gc测试:
public class Main {
static class Data {
public long[] l = new long[100];
}
static List<SoftReference<Data>> list = new ArrayList<>();
public static void main(String[] args) {
long i = 0;
while (true) {
list.add(new SoftReference<>(new Data()));
++i;
if (i % 1000 == 0) {
sleep(1);
if (i % 1_000_000 == 0)
sleep(1000);
}
}
}
static void sleep(long millis) {
try { Thread.sleep(millis); } catch (InterruptedException ignored) {}
}
}
使用这些参数(已启用G1):
java -Xmx2G -Xms2G -XX:MaxPermSize=128m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
-XX:+PrintAdaptiveSizePolicy -Xloggc:jvm.log -XX:+UseG1GC
-XX:InitiatingHeapOccupancyPercent=5 Main
我grep输出:
grep -E "(Full|GC cleanup)" jvm.log
并得到这样的东西:
0.564: [GC cleanup 277M->277M(2048M), 0.0009922 secs]
0.879: [GC cleanup 443M->442M(2048M), 0.0009396 secs]
1.676: [GC cleanup 859M->856M(2048M), 0.0008681 secs]
3.530: [GC cleanup 1324M->1320M(2048M), 0.0012422 secs]
4.838: [GC cleanup 1711M->1707M(2048M), 0.0010601 secs]
6.334: [Full GC 2047M->102M(2048M), 1.2659685 secs]
8.322: [GC cleanup 534M->534M(2048M), 0.0009528 secs]
11.250: [GC cleanup 1460M->1450M(2048M), 0.0011207 secs]
13.499: [Full GC 2046M->512M(2048M), 1.3534848 secs]
似乎在ParallelGc的完整收集期间收集了软引用,而并发收集几乎没有用. VisualVm的堆转储也证明了该版本.
我会错过什么还是G1中的错误?
在1.7.0_51-b13和1.8.0_45-b15 x64上检查.
解决方法:
也许您对弱引用感到困惑?
除非发现GC承受严重的内存压力,否则不会强迫其收集软引用.
有关更多信息,请参见here.
特别要注意文档中的以下引用:
Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand.
该文档提供的唯一真实保证如下:
All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError.
标签:g1gc,java,garbage-collection 来源: https://codeday.me/bug/20191028/1950785.html