Why Repeated Custom ThreadPools Trigger Java OOM and How to Fix It

Creating a new ThreadPoolExecutor with 10 core threads, 50 maximum threads, and a 2‑second keep‑alive for each request prevents core threads from being reclaimed, quickly exhausting native thread resources and causing an OutOfMemoryError.

JavaEdge
JavaEdge
JavaEdge
Why Repeated Custom ThreadPools Trigger Java OOM and How to Fix It

If a custom ThreadPoolExecutor is instantiated for every test request with 10 core threads, a maximum of 50 threads, and a 2‑second keep‑alive time, the pool will not be reclaimed and will eventually cause an OutOfMemoryError.

Even though the pool is custom, core threads are never reclaimed. Each request creates a new pool with ten core threads; as request volume grows, thousands of threads accumulate, exhausting the JVM’s native thread limit.

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread

The reason the ThreadPoolExecutor cannot be garbage‑collected is that its worker threads are inner classes ( Worker ) that hold an implicit reference to the executor. As long as any worker thread is alive (i.e., still running), the executor remains reachable, preventing reclamation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaThreadPoolOOMThreadPoolExecutor
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.