ThreadLocal Resource Leak and WeakReference

后端 未结 7 1567
清酒与你
清酒与你 2020-12-30 03:12

My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (al

相关标签:
7条回答
  • 2020-12-30 03:39

    I realise this isn't strictly an answer to your question but as a general rule I won't suggest using a ThreadLocal in situration where there isn't a clear tear down at the end of a request/interaction. The classic is doing this sort of thing in a servlet container, at first glance it seems fine, but since the threads are pooled it becomes a issue with the ThreadLocal hanging onto the resource even after each request has been processed.

    Suggestions:

    1. Use a filter of similar wrapper for each interaction to clear the ThreadLocal at the end of each interaction
    2. You could use a alternative to SimpleDateFormat like FastDateFormat from commons-lang or Joda as somebody has already suggested
    3. Just create a new SimpleDateFormat every time you need it. Seems wasteful I know, but in most applications you just won't notice the difference
    0 讨论(0)
  • 2020-12-30 03:39

    In your example there should be no problem using a ThreadLocal at all.

    Thread locals (and singletons also!) become a problem when thread locals are set to instances loaded by a classloader to be unloaded later. A typical situation in a servlet container (like tomcat):

    1. A webapp set a thread local during request processing.
    2. The thread is managed by the container.
    3. The webapp should be undeployed.
    4. The classloader of the webapp cannot be garbaged, because there is a reference left: from the thread local to the instance to its class to its classloader.

    The same with singletons (java.sql.DriverManger and JDBC drivers offered by the webapp).

    So avoid such things in particular in environments not under your full control!

    0 讨论(0)
  • 2020-12-30 03:42

    There shouldn't be such a problem.

    A thread's ThreadLocal reference is defined to exist only as long as the corresponding thread is alive (see the javadoc)-- or put another way, once the thread is not alive, if the ThreadLocal was the only reference to that object, then the object becomes eligible for garbage collection.

    So either you've found a genuine bug and should be reporting it, or you're doing something else wrong!

    0 讨论(0)
  • 2020-12-30 03:43

    clean the thread local after using it, add servlet filter to do so:

    protected ThreadLocal myThreadLocal = new ThreadLocal();
    
    public void doFilter (ServletRequest req, ServletResponse res, chain) throws IOException, ServletException {
        try {
            // Set ThreadLocal (eg. to store heavy computation result done only once per request)
            myThreadLocal.set(context());
    
            chain.doFilter(req, res);
        } finally {
            // Important : cleanup ThreaLocal to prevent memory leak
            userIsSmartTL.remove();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 03:49

    ThreadLocal uses a WeakReference internally. If the ThreadLocal is not strongly referenced, it will be garbage-collected, even though various threads have values stored via that ThreadLocal.

    Additionally, ThreadLocal values are actually stored in the Thread; if a thread dies, all of the values associated with that thread through a ThreadLocal are collected.

    If you have a ThreadLocal as a final class member, that's a strong reference, and it cannot be collected until the class is unloaded. But this is how any class member works, and isn't considered a memory leak.


    Update: The cited problem only comes into play when the value stored in a ThreadLocal strongly references that ThreadLocal—sort of a circular reference.

    In this case, the value (a SimpleDateFormat), has no backwards reference to the ThreadLocal. There's no memory leak in this code.

    0 讨论(0)
  • 2020-12-30 04:03

    I'm guessing you're jumping through these hoops since SimpleDateFormat is not thread-safe.

    Whilst I'm aware I'm not solving your problem above, can I suggest you look at Joda for your date/time work ? Joda has a thread-safe date/time formatting mechanism. You won't be wasting your time learning the Joda API either, as it's the foundation for the new standard date/time API proposal.

    0 讨论(0)
提交回复
热议问题