Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to ensure garbage collection of a FutureTask that is submitted to a ThreadPoolExecutor and then cancelled?
    text
    copied!<p>I am submitting <code>Callable</code> objects to a <code>ThreadPoolExecutor</code> and they seem to be sticking around in memory.</p> <p>Looking at the heap dump with the MAT tool for Eclipse see that the <code>Callable</code> objects are being referenced by a <code>FutureTask$Sync</code>'s <strong>callable</strong> variable. That <code>FutureTask$Sync</code> is referenced by a <code>FutureTask</code>'s <strong>sync</strong> variable. That <code>FutureTask</code> is referenced by the <code>FutureTask$Sync</code>'s <strong>this$0</strong> variable.</p> <p>I have read around about this (<a href="http://stochastyk.blogspot.com/2006/12/memory-issues-using-java.html" rel="noreferrer">here</a>, <a href="http://markmail.org/message/3r3z6oxjkkiimjn6#query:+page:1+mid:45ouqntsrs72hyn6+state:results" rel="noreferrer">here</a>, and on SO) and it seems like the <code>FutureTask</code> that the callable is wrapped in upon the <code>ThreadPoolExecutor</code>'s submit() holds a reference to the callable forever.</p> <p>What I am confused about is how to ensure that the <code>FutureTask</code> gets garbage collected so it doesn't continue to hold the callable in memory, and hold anything the callable might be holding in memory?</p> <p>Just to give more details about my particular situation, I am trying to implement the <code>ThreadPoolExecutor</code> in a way that allows all of the submitted tasks to be canceled if needed. I have tried several different methods I found on SO and elsewhere, such as completely shutting the executor down (with <code>shutdown()</code>, <code>shutdownNow()</code> etc) and also keeping a list of the futures return by <code>submit()</code> and calling cancel on all them and then clearing the list of futures. Ideally I would like not to have to shut it down, and just <code>cancel()</code> and clear out when needed.</p> <p>All of these methods don't seem to make a difference. If I submit a callable to the pool, there is a good chance it will end up sticking around.</p> <p>What am I doing wrong?</p> <p>Thanks.</p> <p><strong>Edit:</strong></p> <p>As requested, here is the constructor for the ThreadPoolExecutor.</p> <pre><code>public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } </code></pre> <p>After further testing I can see that if I let the tasks that have been submitted to the ThreadPoolExecutor finish, then there is no leak. If I try to cancel them in anyway such as:</p> <pre><code>shutdownNow() </code></pre> <p>Or saving a reference to the future and calling cancel on it later:</p> <pre><code>Future referenceToCancelLater = submit(task); ... referenceToCancelLater.cancel(false); </code></pre> <p>Or by removing them from the queue with methods like:</p> <pre><code>getQueue.drainTo(someList) </code></pre> <p>or</p> <pre><code>getQueue.clear() </code></pre> <p>or Looping through saved references to the futures and calling:</p> <pre><code>getQueue.remove(task) </code></pre> <p>Any of those cases causes the FutureTask to stick around as described above.</p> <p>So the real question in all of this is how to I properly cancel or remove items from a ThreadPoolExecutor so that the FutureTask is garbage collected and not leaked forever?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload