Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean up ThreadLocals

Does any one have an example how to do this? Are they handled by the garbage collector? I'm using Tomcat 6.

like image 833
Ricardo Avatar asked Oct 06 '10 01:10

Ricardo


2 Answers

The javadoc says this:

"Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).

If your application or (if you are talking about request threads) container uses a thread pool that means that threads don't die. If necessary, you would need to deal with the thread locals yourself. The only clean way to do this is to call the ThreadLocal.remove() method.

There are two reasons you might want to clean up thread locals for threads in a thread pool:

  • to prevent memory (or hypothetically resource) leaks, or
  • to prevent accidental leakage of information from one request to another via thread locals.

Thread local memory leaks should not normally be a major issue with bounded thread pools since any thread locals are likely to get overwritten eventually; i.e. when the thread is reused. However, if you make the mistake of creating a new ThreadLocal instances over and over again (instead of using a static variable to hold a singleton instance), the thread local values won't get overwritten, and will accumulate in each thread's threadlocals map. This could result in a serious leak.


Assuming that you are talking about thread locals that are created / used during a webapp's processing of an HTTP request, then one way to avoid the thread local leaks is to register a ServletRequestListener with your webapp's ServletContext and implement the listener's requestDestroyed method to cleanup the thread locals for the current thread.

Note that in this context you also need to consider the possibility of information leaking from one request to another.

like image 191
Stephen C Avatar answered Sep 27 '22 17:09

Stephen C


Here is some code to clean all thread local variables from the current thread when you do not have a reference to the actual thread local variable. You can also generalize it to cleanup thread local variables for other threads:

    private void cleanThreadLocals() {         try {             // Get a reference to the thread locals table of the current thread             Thread thread = Thread.currentThread();             Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");             threadLocalsField.setAccessible(true);             Object threadLocalTable = threadLocalsField.get(thread);              // Get a reference to the array holding the thread local variables inside the             // ThreadLocalMap of the current thread             Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");             Field tableField = threadLocalMapClass.getDeclaredField("table");             tableField.setAccessible(true);             Object table = tableField.get(threadLocalTable);              // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object             // is a reference to the actual ThreadLocal variable             Field referentField = Reference.class.getDeclaredField("referent");             referentField.setAccessible(true);              for (int i=0; i < Array.getLength(table); i++) {                 // Each entry in the table array of ThreadLocalMap is an Entry object                 // representing the thread local reference and its value                 Object entry = Array.get(table, i);                 if (entry != null) {                     // Get a reference to the thread local object and remove it from the table                     ThreadLocal threadLocal = (ThreadLocal)referentField.get(entry);                     threadLocal.remove();                 }             }         } catch(Exception e) {             // We will tolerate an exception here and just log it             throw new IllegalStateException(e);         }     } 
like image 31
lyaffe Avatar answered Sep 27 '22 17:09

lyaffe