Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardJavaThreadLocal lifecycle leaksSingle-choice MCQ

Why does a ThreadLocal whose value transitively references its own ThreadLocal key still risk a memory leak in a long-lived thread, despite ThreadLocalMap using WeakReference keys?

static final ThreadLocal<byte[]> TL = new ThreadLocal<>(); // in a pooled (long-lived) worker thread: TL.set(new byte[10_000_000]); // ... request ends, but remove() is never called