TLDR Tech / Dev
We Replaced ThreadLocal with Scoped Values in Java 26 — Then Our Memory Leaks Vanished
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.
Read the full story