[concurrency-interest] Why does FutureTask hold reference to the Callable object forever?

Joe Bowbeer joe.bowbeer at gmail.com
Wed Jan 23 22:04:08 EST 2008

On Jan 23, 2008 4:47 PM, Kwok, Grace (MSCIBARRA) wrote:
>       After a FutureTask is run and result is set to a local variable
> internally inside FutureTask, I would think that the Callable object should
> not be needed anymore.   I would think that it would be best the reference
> to the Callable object be set to null.
>       Without nullifying the reference to the Callable object, it seems to
> me that this can cause memory leak.
>       When implementing cache using the Memoizer pattern, the cache becomes
> a map that not only holds strong references to the resulting values, but
> also strong references to whatever that was needed for the computation.
>       Any thought on this?  A simple way to nullify the reference to the
> callable inside FutureTask after "run" would be appreciated.

I follow your reasoning.

Note that FutureTask is also used for tasks that are scheduled to
execute repeatedly.  See the "runAndReset" method.  That's one reason,
in addition to ease of implementation, for retaining the reference to
the Callable.

A workaround for memoize-like applications is to have the Callable
release its references after it is called, or to extend FutureTask's
"done" method to notifyl the Callable to release its resources.


More information about the Concurrency-interest mailing list