How to ensure garbage collection of a FutureTask that is submitted to a ThreadPoolExecutor and then cancelled?

前端 未结 4 1801
梦毁少年i
梦毁少年i 2020-12-29 12:53

I am submitting Callable objects to a ThreadPoolExecutor and they seem to be sticking around in memory.

Looking at the heap dump with the M

4条回答
  •  北海茫月
    2020-12-29 13:21

    As a work around could you do something like:

    class ClearingCallable implements Callable {
        Callable delegate;
        ClearingCallable(Callable delegate) {
            this.delegate = delegate;
        }
    
        T call() {
            try {
                return delegate.call();
            } finally {
                delegate = null;
            }
        }
    }
    

提交回复
热议问题