I am sending Callable objects to ThreadPoolExecutor and they seem to stick out in memory.
After looking at a bunch of heaps using the MAT tool for Eclipse, look at the Callable objects referenced by the FutureTask$Sync variable. This FutureTask$Sync refers to the FutureTask sync variable. This FutureTask refers to FutureTask$Sync this variable is $ 0 .
I read about it ( here , here, and on SO), and it seems that FutureTask , that the called, enclosed in ThreadPoolExecutor submit (), contains a link to the called forever.
What I'm confused about is how to ensure that FutureTask collects garbage so that it does not continue to hold the called in memory and hold everything that the called can hold in memory?
To give more detailed information about my specific situation, I am trying to implement ThreadPoolExecutor in such a way that, if necessary, all canceled tasks are canceled. I tried several different methods that I found in SO and elsewhere, for example, completely disabling the executor (using shutdown() , shutdownNow() , etc.), and also saving the list of futures return on submit() and calling cancellation for all of them and then clearing the list of futures. Ideally, I would not want to close it, but just cancel() and remove it if necessary.
All of these methods seem to have no meaning. If I send the called to the pool, there is a good chance that he will eventually stick around.
What am I doing wrong?
Thank.
Edit:
As requested, here is the ThreadPoolExecutor constructor.
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); }
After further testing, I see that if I solve the tasks that were sent to ThreadPoolExecutor, then there is no leak. If I try to cancel them anyway, for example:
shutdownNow()
Or save a link to the future and later call it canceled:
Future referenceToCancelLater = submit(task); ... referenceToCancelLater.cancel(false);
Or removing them from the queue using methods such as:
getQueue.drainTo(someList)
or
getQueue.clear()
or Quoting through saved futures and call links:
getQueue.remove(task)
Any of these cases causes FutureTask to stick as described above.
So, the real question in all of this is how to properly undo or delete items from ThreadPoolExecutor so that FutureTask collects garbage and does not leak forever?