尝试了所有垃圾收集器,一百万种不同的设置组合。但结果总是一样的——OutOfMemoryError。
谁能告诉哪个垃圾收集器能够及时删除软引用引用的对象
下面的示例模拟器代码
package com;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
class SoftReferenceCollector {
private static class Data {
public long[] l = new long[100];
}
private List<SoftReference<Data>> list = new ArrayList<>();
public void startFillCollection(int durationInSec) {
long i = 0;
long start = System.currentTimeMillis();
long end = start + durationInSec * 1000;
while (System.currentTimeMillis() < end) {
list.add(new SoftReference<>(new Data()));
++i;
if (i % 10000 == 0) {
sleep(1);
if (i % 1_000_000 == 0)
sleep(1000);
}
}
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}
}
public class Main6 {
public static void main(String[] args) {
SoftReferenceCollector softReferenceCollector = new SoftReferenceCollector();
softReferenceCollector.startFillCollection(11240);
}
}