On 25 nov. 2011, at 15:58, Christopher Schultz wrote:

> On 11/24/11 4:02 PM, Sylvain Laurent wrote:
>> I don't think this ThreadLocal creates a real leak of classloader. 
>> It would if dayFormat was static.
> 
> IIRC, ThreadLocal essentially puts a key/value pair in a Map in the
> Thread. I dunno what kind of reference it is, but I suspect it's a
> normal, strong reference. That means that the Thread itself retains a
> reference to the instance of the inner class in my servlet. That's
> just not going to become available for collection anytime soon.

Actually, in Sun's implementation (1.5 and 1.6 at least), ThreadLocal are 
implemented with a kind of WeakHashMap in a instance variable of Thread, using 
your ThreadLocal instance as a weak key and the actual value you stored as 
value with a strong reference.
In your example, the reference to the ThreadLocal instance is stored in an 
instance variable of your Servlet. So, when your app is stopped, tomcat 
releases its reference to your Servlet instance so that it can be collected and 
your ThreadLocal instance too.
Since in your case the value that is bound in the ThreadLocal for each thread 
is a JRE class (SimpleDateFormat), it does not reference the webapp 
classloader. The latter can then be collected (provided there are no other 
references pinning it in memory).

Note that I was wrong when I wrote that there would be a leak if dayFormat was 
static : that would only be the case if the value bound in the ThreadLocal was 
an instance of a class that is loaded by the webapp. It's not the case here 
(SimpleDateFormat), so that even with a static dayFormat, the classloader would 
be GCed. 

Sylvain


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to