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

Grace Kwok gykwok at gmail.com
Thu Jan 24 02:23:32 EST 2008


>>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.

Yes..
but in cases like below, "c" is not set to a local variable inside the
Callable but it is indeed referenced.  Nothing prevents us to do this and it
was a suggested pattern...


         Future<V> f = cache.get(arg);
         if (f == null) {
            Callable<V> eval = new Callable<V>() {
               public V call() throws IntrruptedException {
                  return c.compute(arg);
               }
            };

Thanks, Grace


On Jan 23, 2008 7:04 PM, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:

> 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.
>
> --Joe
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080123/5016ca6e/attachment-0001.html 


More information about the Concurrency-interest mailing list