我基于Java的CompletableFuture构建了一个任务链,它可能会非常长。我的问题是CompletableFuture中的每个任务都是一个内部类UniCompletion
,并且它拥有对源CompletableFuture的引用,因此无法对完成的CompletableFuture进行垃圾回收。有没有办法避免内存泄漏呢?
这里有一段代码可用于重现此错误:
public static void main(String... args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
AtomicReference<CompletableFuture<Integer>> future = new AtomicReference<>(CompletableFuture.completedFuture(0));
IntStream.range(0, 100000000).forEach(i -> future.set(future.get().thenApplyAsync(ii -> ii + 1, executor)));
future.get().get();
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
当我使用关注程序时,
import java.lang.ref.Cleaner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.LockSupport;
public class CfGc {
static final Cleaner CLEANER = Cleaner.create();
static CompletableFuture<Integer> next(CompletableFuture<Integer> f) {
Object[] status = { "not completed" };
CLEANER.register(f, () -> System.out.println(status[0]+" future collected"));
return f.whenComplete((i,t) -> {
status[0] = t != null? t: i;
LockSupport.parkNanos(500_000_000);
System.out.println(status[0]+" completed, running gc()");
System.gc();
LockSupport.parkNanos(5_000_000);
System.out.println(status[0]+" completed, gc() ran\n");
}).thenApply(i -> i + 1);
}
public static void main(String[] args) {
CompletableFuture<Integer> s = new CompletableFuture<>(), f = s;
for(int i = 0; i < 6; i++) f = next(f);
s.complete(1);
}
}
它始终在我的机器上打印
1 completed, running gc()
1 completed, gc() ran
2 completed, running gc()
2 completed, gc() ran
3 completed, running gc()
2 future collected
3 completed, gc() ran
4 completed, running gc()
3 future collected
4 completed, gc() ran
5 completed, running gc()
4 future collected
5 completed, gc() ran
6 completed, running gc()
5 future collected
6 completed, gc() ran
这表明,在评估下一阶段时,未来是可以实现的,但在下一阶段的评估中,则是不可实现的。直到最后一个阶段,整个链都没有被引用过。
[只有第一个未来保持可实现,这在链的顺序评估中是不可避免的,因为一切都发生在从第一个未来的complete
方法调用的main
方法中。当我们将程序更改为
static final Cleaner CLEANER = Cleaner.create();
static CompletableFuture<Integer> next(CompletableFuture<Integer> f) {
Object[] status = { "not completed" };
CLEANER.register(f, () -> System.out.println(status[0]+" future collected"));
return f.whenComplete((i,t) -> {
status[0] = t != null? t: i;
LockSupport.parkNanos(500_000_000);
System.out.println(status[0]+" completed, running gc()");
System.gc();
LockSupport.parkNanos(5_000_000);
System.out.println(status[0]+" completed, gc() ran\n");
}).thenApplyAsync(i -> i + 1);
}
public static void main(String[] args) {
CompletableFuture<Integer> s = new CompletableFuture<>(), f = s;
for(int i = 0; i < 6; i++) f = next(f);
s.complete(1);
s = null;
f.join();
}
打印
1 completed, running gc()
1 completed, gc() ran
2 completed, running gc()
1 future collected
2 completed, gc() ran
3 completed, running gc()
2 future collected
3 completed, gc() ran
4 completed, running gc()
3 future collected
4 completed, gc() ran
5 completed, running gc()
4 future collected
5 completed, gc() ran
6 completed, running gc()
5 future collected
6 completed, gc() ran
在我的机器上,表明在完成过程中未从堆栈帧引用原始的未来时,也可以收集垃圾。
使用时也是如此
public static void main(String[] args) {
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1);
for(int i = 0; i < 6; i++) f = next(f);
f.join();
}
不管next
方法使用thenApply
还是thenApplyAsync
。>