How to ensure garbage collection in FutureTask, which is sent to ThreadPoolExecutor and then canceled?

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?

+9
java memory-leaks callable executor futuretask
Feb 06 '11 at 23:50
source share
4 answers

I could not get it to work, so I came up with the following solution. Here's a rough overview: I created an array in ThreadPoolExecutor that kept track of executable files that were in the queue. Then, when I needed to cancel the queue, I went in cycles and called the cancel method for each of the runnables. In my case, all these runnables were a special class that I created, and their undo method just set the unchecked flag. When the queue has started the next process, during the runnable process, it will see that it has been canceled and skipped the actual work.

Thus, all runnables then simply quickly fail one by one, as he sees that it has been canceled.

This is probably not the best solution, but it works for me, and it is not a memory leak.

+1
Mar 07 2018-11-11T00:
source share

According to this post , you can call purge on the performer.

+4
Dec 16
source share

As a job, you could do something like:

 class ClearingCallable<T> implements Callable<T> { Callable<T> delegate; ClearingCallable(Callable<T> delegate) { this.delegate = delegate; } T call() { try { return delegate.call(); } finally { delegate = null; } } } 
+1
07 Feb 2018-11-11T00:
source share

Refer to: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html

A The future is the result of asynchronous computing. If the result is not obtained using the get method, a memory leak will occur.

If you do not want to use an asynchronous result, use Runnable install Callable.

0
Jul 05 '16 at 2:11
source share



All Articles