ThreadLocalis implemented as a weak hash map. Each thread has a map instance. The map keys are weak references to the
ThreadLocalinstances themselves. The map values are the thread local values. Just like with
WeakHashMapinstances, if your value somehow holds a strong reference to the
ThreadLocalkey, the garbage collector can't reclaim either until you explicitly set the value to null or even better call
ThreadLocalinstance. This bit me in the ass today when I mixed
ThreadLocalwith nested classes, instances of which have an implicit strong reference to their nesting instance, and failed to properly clean up afterwards. "Implicit" is synonymous with "easy to forget about." Oops.