Quick revision
The short answer
Every call to build() creates a new pool of 8 threads and never shuts it down. The threads of a fixed pool are non-daemon core threads that never time out by default, so they wait for work forever, and because live threads reference their pool, the garbage collector can never reclaim it. The fix is one shared, named executor created once as a Spring bean and closed with the application; if a per-call executor is truly needed, it must be closed with try-with-resources on Java 19 and later, or shut down in a finally block. A metric on live thread count with an alert catches the next leak early.
What to remember
In this answer, we traced a growing thread count from the heap dump and thread dump to an executor created on every call to build(). Its threads are non-daemon core threads that wait forever and keep the pool reachable, so without shutdown neither ever goes away. We fixed it with one shared, named executor bean closed with the application, showed the correct per-call pattern with try-with-resources on Java 19 and later or shutdown in finally before that, and added a live-thread-count metric with an alert and a static check to catch the next one.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Every call to build() creates a new pool of 8 threads and never shuts it down. The threads of a fixed pool are non-daemon core threads that never time out by default, so they wait for work forever, and because live threads reference their pool, the garbage collector can never reclaim it. The fix is one shared, named executor created once as a Spring bean and closed with the application; if a per-call executor is truly needed, it must be closed with try-with-resources on Java 19 and later, or shut down in a finally block. A metric on live thread count with an alert catches the next leak early.
Why this question is asked
Thread leaks are slow-burning failures. The service runs fine after a deploy, then hours or days later it hits the operating system's thread limit, the container's memory limit, or fails with 'unable to create native thread'. Interviewers like this debugging question because the cause is a single line that looks completely normal, and the explanation needs a clear picture of how pool threads live and die. In this answer, we will follow the evidence from the symptom to the line of code, explain why the threads never go away, fix it with a shared executor, look at the correct per-call pattern, and add monitoring so the next leak is caught in minutes rather than days.
1Following the Evidence
The symptom is a thread count that grows steadily, several hundred per hour. A heap dump shows many ThreadPoolExecutor instances, far more than the handful an application normally has. The next tool is a thread dump, taken with jcmd <pid> Thread.print or from the Actuator threaddump endpoint. It shows the pattern clearly: hundreds of threads named pool-412-thread-1 to pool-412-thread-8, pool-413-thread-1 and so on, all in WAITING state inside LinkedBlockingQueue.take(). The default thread names count pools upward, so a pool number in the hundreds is itself a sign that pools are being created repeatedly.
That narrows the search to code that calls Executors.newFixedThreadPool outside of startup, which leads to the build() method. If build() is called 50 times an hour, each call leaving 8 threads behind, that is 400 new threads an hour, matching the growth we saw.
- Thread count grows steadilyseveral hundred per hour
- Heap dumpmany
ThreadPoolExecutorinstances - Thread dumppool-412-thread-1..8 WAITING in
take() - Code search
newFixedThreadPoolinsidebuild() - Confirm the rate50 calls per hour x 8 threads = 400
5 steps in order. Play them, or step through them one at a time.
2Why the Threads Never Go Away
A fixed pool's threads are core threads, and by default core threads never time out: when there is no work, each one blocks in queue.take() waiting for the next task, for as long as the pool is not shut down. The default thread factory also creates non-daemon threads, which means the JVM treats them as real work and will not even exit while they are alive.
Notice what that means for garbage collection. When build() returns, nothing in our code refers to the pool any more, so it looks like garbage. But each worker thread is alive, live threads are roots for the garbage collector, and each worker holds a reference to its pool, because it needs the queue to take tasks from. So the pool is reachable from its own threads, and the threads only end if the pool is shut down. Nobody ever calls shutdown, so both live forever.
3Fix 1: One Shared, Named Executor
The right shape for most applications is a single executor for this kind of work, created once at startup, injected where it is needed and shut down when the application stops. In Spring that is a bean. Naming its threads means a future thread dump says 'report-3' instead of 'pool-412-thread-3', and a bounded queue makes overload visible.
Spring infers a close or shutdown method on beans by default, so the explicit destroyMethod mostly documents intent. Spring Boot also auto-configures a ThreadPoolTaskExecutor named applicationTaskExecutor, which can serve if one general-purpose pool is enough. Either way, the thread count now stays at 8 for this work, however often build() is called.
4Fix 2: A Per-Call Executor That Cleans Up
Occasionally a per-call executor makes sense, for example a short-lived batch tool or a method that needs its own isolated threads. Then it must always be shut down, including when the work throws. Since Java 19, ExecutorService implements AutoCloseable, and its close() method shuts the executor down and waits for submitted tasks to finish, so try-with-resources handles every path.
Notice the choice of executor in the first version. On Java 21 and later, a virtual-thread-per-task executor is so cheap to create that a per-call executor is reasonable for I/O-bound work, and try-with-resources gives it a clear lifetime. A platform-thread pool per call, even when closed correctly, still pays to create and destroy 8 operating-system threads on every request.
5Watching Thread Counts So It Does Not Happen Again
This leak ran for hours before anyone noticed because nothing was watching the number of threads. Spring Boot with Micrometer already publishes JVM thread metrics, including the live thread count as jvm.threads.live; check the names in your Micrometer version. An alert on that metric, either an absolute ceiling such as more than 1,000 threads or a steady increase over several hours, would have caught this within the first hour. Micrometer can also instrument executors themselves, reporting pool size, active threads and queue length per named executor, which makes leaks and saturation visible per pool.
A static check helps too: a rule in code review or an ArchUnit test that flags calls to Executors factory methods outside configuration classes catches the pattern before it ships.
6Follow-up questions
Would making the threads daemon threads fix the leak?
No. Daemon threads do not stop the JVM from exiting, but they still live, wait and hold memory until the pool is shut down. The leak would continue at exactly the same rate.
Would allowCoreThreadTimeOut(true) fix it?
It would hide it: idle threads would end after the keep-alive time, so the count would stop growing, but we would still create a pool and its threads on every call. Creating one shared executor is the real fix.
Are there other classes that leak threads the same way?
Yes. Anything that owns threads and is created per call: scheduled executors, HTTP clients with their own selector threads, Kafka consumers or producers, and some database pools. The rule is the same: create them once, share them, and close them with the application.
7Conclusion
In this answer, we traced a growing thread count from the heap dump and thread dump to an executor created on every call to build(). Its threads are non-daemon core threads that wait forever and keep the pool reachable, so without shutdown neither ever goes away. We fixed it with one shared, named executor bean closed with the application, showed the correct per-call pattern with try-with-resources on Java 19 and later or shutdown in finally before that, and added a live-thread-count metric with an alert and a static check to catch the next one.
Open with
The bug is that build creates a new fixed pool of 8 threads on every call and never shuts it down. A fixed pool's threads are core threads, which don't time out by default, so when the work is done they just block in the queue's take waiting for more, forever.
Then, if they want more
They're also non-daemon. And the pool can't be garbage collected, because those live threads are GC roots and each worker references its pool, so the pool stays reachable even though our code dropped it. If build runs 50 times an hour, that's 400 leaked threads an hour, which matches.
In a thread dump I'd expect hundreds of threads named pool-something-thread-1 to 8 waiting in LinkedBlockingQueue take, and the high pool numbers alone tell you pools are being created repeatedly.
The fix is to create one executor for this work at startup, as a Spring bean with named threads and a bounded queue, inject it, and let Spring close it on shutdown.
If I really need a per-call executor, I'd use try-with-resources, since ExecutorService is AutoCloseable from Java 19 and close shuts it down and waits; on Java 21 a virtual-thread-per-task executor makes that cheap. Before 19, shutdown in a finally block.
Then I'd add an alert on jvm threads live, both an absolute limit and a steady upward trend, and a static check that flags Executors factory calls outside configuration code.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 19, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Thread count on a service grows by several hundred per hour until it hits limits. A heap...": https://scrivio-interview-library.netlify.app/java/executors/14aa207fb7/