如何确保提交到 ThreadPoolExecutor 然后取消的 FutureTask 的垃圾回收?

发布于 2024-10-16 13:30:03 字数 2069 浏览 3 评论 0原文

我正在向 ThreadPoolExecutor 提交 Callable 对象,它们似乎保留在内存中。

使用 Eclipse 的 MAT 工具查看堆转储,可以看到 Callable 对象被 FutureTask$Synccallable 变量引用。 FutureTask$SyncFutureTasksync 变量引用。该 FutureTaskFutureTask$Syncthis$0 变量引用。

我已经阅读过有关此内容的内容(此处这里,以及SO),看起来像可调用对象包装在 ThreadPoolExecutor 的 Submit() 上的 FutureTask 永远保留对可调用对象的引用。

我感到困惑的是如何确保 FutureTask 被垃圾收集,这样它就不会继续将可调用对象保存在内存中,并保存可调用对象可能在内存中保存的任何内容?

为了提供有关我的特定情况的更多详细信息,我尝试以允许在需要时取消所有已提交任务的方式实现 ThreadPoolExecutor。我尝试了在 SO 和其他地方找到的几种不同方法,例如完全关闭执行器(使用 shutdown()shutdownNow() 等)并保留一个列表通过 submit() 返回期货的数量,并对所有期货调用取消,然后清除期货列表。理想情况下,我希望不必关闭它,只需 cancel() 并在需要时清除。

所有这些方法似乎都没有什么作用。如果我向池中提交一个可调用对象,它很可能最终会保留下来。

我做错了什么?

谢谢。

编辑:

根据要求,这里是 ThreadPoolExecutor 的构造函数。

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

经过进一步测试,我可以看到,如果我让已提交给 ThreadPoolExecutor 的任务完成,则不会出现泄漏。如果我尝试以任何方式取消它们,例如:

shutdownNow()

或者保存对未来的引用并稍后调用取消:

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

或者通过使用以下方法从队列中删除它们:

getQueue.drainTo(someList)

getQueue.clear()

或 循环保存对 future 的引用并调用:

getQueue.remove(task)

任何这些情况都会导致 FutureTask 如上所述保留下来。

因此,所有这一切的真正问题是如何正确地从 ThreadPoolExecutor 中取消或删除项目,以便 FutureTask 被垃圾收集并且不会永远泄漏?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自控 2024-10-23 13:30:03

根据这篇文章,您可以调用在执行器上清除

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

妞丶爷亲个 2024-10-23 13:30:03

作为解决方法,您可以执行以下操作:

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;
        }
    }
}

As a work around could you 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;
        }
    }
}
霓裳挽歌倾城醉 2024-10-23 13:30:03

我无法得到任何工作,所以我想出了以下解决方案。以下是一个粗略的概述:我在 ThreadPoolExecutor 中创建了一个数组,用于跟踪队列中的可运行程序。然后,当我需要取消队列时,我循环遍历并在每个可运行对象上调用取消方法。就我而言,所有这些可运行对象都是我创建的自定义类,它们的取消方法只是设置一个取消标志。当队列提出下一个要处理的任务时,在可运行对象的运行中,它会看到它被取消并跳过实际的工作。

因此,当它看到它被取消时,所有可运行的东西都会被快速地一一清除。

可能不是最好的解决方案,但它对我有用并且不会泄漏内存。

I couldn't get anything to work so I came up with the following solution. Here is a rough overview: I created an array in the ThreadPoolExecutor that kept track of the runnables that were in the queue. Then when I needed to cancel the queue, I looped through and called a cancel method on each of the runnables. I my case, all of these runnables were a custom class I created and their cancel method simply set a cancelled flag. When the queue brought up the next one to process, in the run of the runnable it would see it was cancelled and skip the actual work.

So all of the runnables then just get flushed out quickly one by one as it sees it was cancelled.

Probably not the greatest solution, but it works for me and it doesn't leak memory.

稚气少女 2024-10-23 13:30:03

请参阅:https://docs.oracle。 com/javase/7/docs/api/java/util/concurrent/Future.html

Future 表示异步计算的结果。如果不能使用 get 方法检索结果,则会发生内存泄漏!

如果您不想使用异步结果,请使用 Runnable 安装 Callable。

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

A Future represents the result of an asynchronous computation. If the result not be retrieved using method get then memory leak happen!

If you don't want to consume asynchronous result, using Runnable install Callable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文