I am working with Spring, and in a RequestMap method I have code like below:
@RequestMap
public void someMethod() {
ThreadPoolExecutor executor = Executors.newFixedThreadPool(N);
executor.submit(new Runnable());
executor.submit(new Runnable());
}
Then I keep get OOM error even every Runnable should be finished in seconds. After analysing the heap dump, I found there are thousands Thread objects.
Then I changed executor to singlton with Executors.newCachedThreadPool, this problem was fixed.
As far as my understand, after the method returned, there is no reference to the thread pool, so it should be garbage-collected, but the fact is the thread still on the heap. Why?