
Digiaru started this conversation 4 days ago.
Memory Leak Caused by Unremoved ThreadLocal Variables in Thread Pools
In my web app, I use ThreadLocal to store request metadata per thread. Over time, memory use steadily grows—even though threads finish processing. What's wrong?
Kar
Posted 4 days ago
With thread pooling, ThreadLocal values persist across requests until explicitly removed. If not cleared, these references accumulate, leading to memory leaks and eventually OutOfMemoryError ([turn0search5]turn0search13]turn0search1]). Fixes: • Always call .remove() in a finally block after request handling. • Avoid storing large objects per request in ThreadLocal. • Use request-scoped or dependency injection for per-request state instead.