TLDR Dev · ARTICLES & TUTORIALS
WE REPLACED THREADLOCAL WITH SCOPED VALUES IN JAVA 26 — THEN OUR MEMORY LEAKS VANISHED
Summary from the TLDR Dev newsletter
A Java user-auth service experienced frequent Out Of Memory (OOM) crashes due to memory leaks after migrating to Virtual Threads. The issue was from legacy code using `ThreadLocal`, which inefficiently copied large `SecurityContext` objects into numerous short-lived virtual threads, causing excessive heap memory consumption. To resolve this, the team migrated from `ThreadLocal` to `ScopedValue`, a new Java 26 feature designed for immutable, zero-copy data sharing across threads and their children.