我正在使用 Spring,在 RequestMap
方法中我有如下代码:
@RequestMap
public void someMethod() {
ThreadPoolExecutor executor = Executors.newFixedThreadPool(N);
executor.submit(new Runnable());
executor.submit(new Runnable());
}
然后我不断收到 OOM 错误,即使每个 Runnable
应该在几秒钟内完成。分析堆转储后,我发现有数千个 Thread
对象。
然后我用Executors.newCachedThreadPool
将executor
更改为singlton,这个问题就解决了。
据我了解,方法返回后,没有对线程池的引用,所以应该被垃圾回收,但事实是线程仍在堆上。为什么?
最佳答案
是的,这会泄漏内存。 As it says in the documentation :
An unused
ExecutorService
should be shut down to allow reclamation of its resources.
关闭执行器 ( executor.shutdown()
),或重新使用它。
关于java - spring中创建ThreadPoolExecutor会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49759394/