ThreadLocal memory leak (ThreadLocal memory leak) The biggest complaint is memory leaks caused by weak references.

Why memory leaks

1. When is a ThreadLocal collected

A ThreadLocal is referenced as a weak reference to an Entry Key in a ThreadLocalMap. Will ThreadLocal be reclaimed when GC occurs?

This is the point of reference. Four references in Java. When an object is strongly referenced (reachable here). The garbage collector won’t collect him.

For example,

ThreadLocal threadlocal1 = new ThreadLocal();
threadlocal1.set("Test");
Copy the code

ThreadLocal objects now have two kinds of indexes to point to.

  • Strong references:threadlocal1Corresponding to the figure [1]
  • Weak references:The Key (WeakReference (threadlocal1))Corresponding to [2] in the figure

So when GC occurs, ThreadLocal objects in the heap are not reclaimed.

But when threadLocal1 =null; When a strong reference is broken, the ThreadLocal object has only one weak reference, and when GC occurs, the ThreadLocal object is reclaimed and the Entry becomes a null Entry. Also called dirty Entry

Features are:

  • Key is null and value cannot be accessed by the application because we have no reference to it
  • Thread Ref -> Thread -> ThreaLocalMap -> Entry -> valueChain exists, current thread does not terminate (Like thread pools), but cannot be used and becomes dirty data, causing a memory leak.

It looks like a memory leak caused by soft references.


2. Why weak references

That’s strong reference analysis instead: ThreadLocal objects are pointed to by two strong references

  • Strong reference: threadlocal1
  • Strong reference: entry.key

When we disconnect the strong reference threadLocal1 from the program. ThreadLocal objects are still pointed to by a strong reference to entry. key, and are not recycled. As a result, both ThreadLocal objects and values become dirty data.


Comparing the two cases: no matter soft or strong references, memory leakage can occur, while weak references reduce the extent of memory leakage

An Entry with a weak reference will have a null key, which can identify the unused data and clean up the data. Weak reference improves the security of ThreadLocal.

In fact, when the get(),set(), and reomve() methods of ThreadLocal are called, the null Key values of all entries in ThreadLocalMap will be cleared, and the entire Entry will be set to NULL, which will facilitate the next memory collection.

3. The real cause of the leak

So: The real cause of the ThreadLocal memory leak:

  1. ThreadLocalMapThe lifetime of Thread is as long as that of Thread, and if Thread pools, such as Thread pools, do not retire, memory leaks may occur
  2. If the usage is not cleared in time, it will not be called againGet (), set(), remove()Clean up dirty entries

This is the real cause of memory leaks.

Conclusion:

Remove immediately after use, is the most authentic way to use.

If there are any mistakes, please let us know and study together.

Reference:

  • www.jianshu.com/p/a1cd61fa2…
  • www.jianshu.com/p/dde92ec37…
  • www.jianshu.com/p/964fbc301…