Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure garbage collection of a FutureTask that is submitted to a ThreadPoolExecutor and then cancelled?

I am submitting Callable objects to a ThreadPoolExecutor and they seem to be sticking around in memory.

Looking at the heap dump with the MAT tool for Eclipse see that the Callable objects are being referenced by a FutureTask$Sync's callable variable. That FutureTask$Sync is referenced by a FutureTask's sync variable. That FutureTask is referenced by the FutureTask$Sync's this$0 variable.

I have read around about this (here, here, and on SO) and it seems like the FutureTask that the callable is wrapped in upon the ThreadPoolExecutor's submit() holds a reference to the callable forever.

What I am confused about is how to ensure that the FutureTask gets garbage collected so it doesn't continue to hold the callable in memory, and hold anything the callable might be holding in memory?

Just to give more details about my particular situation, I am trying to implement the ThreadPoolExecutor 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 shutdown(), shutdownNow() etc) and also keeping a list of the futures return by submit() 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 cancel() and clear out when needed.

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.

What am I doing wrong?

Thanks.

Edit:

As requested, here is the constructor for the ThreadPoolExecutor.

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}

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:

shutdownNow()

Or saving a reference to the future and calling cancel on it later:

Future referenceToCancelLater = submit(task);
...
referenceToCancelLater.cancel(false);

Or by removing them from the queue with methods like:

getQueue.drainTo(someList)

or

getQueue.clear()

or Looping through saved references to the futures and calling:

getQueue.remove(task)

Any of those cases causes the FutureTask to stick around as described above.

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?

like image 852
cottonBallPaws Avatar asked Feb 06 '11 23:02

cottonBallPaws


People also ask

How can you make sure an object is garbage collected?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.

How do you ensure that an instance is not garbage collected?

We have three ways to achieve same - 1) Increasing the Heap -Eden space size . 2) Create Singleton class with Static reference . 3) Override finalize() method and never let that object dereference.

Which of the following methods are used in the process of garbage collection?

You can use methods like free() in C, and delete() in C++ to perform Garbage Collection. In Java, garbage collection happens automatically during the lifetime of a program.

Which method is used to invoke the garbage collector to perform the cleanup processing and it is found in System and Runtime classes?

The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes.


1 Answers

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

like image 113
gsingh2011 Avatar answered Sep 20 '22 17:09

gsingh2011