Use of ThreadLocal in HK2 #285
Comments
Reported by @jwells131313 |
@jwells131313 said: $ find . -name "*.java" | xargs grep ThreadLocal |
@jwells131313 said: ./hk2-configuration/persistence/hk2-xml-dom/hk2-config/src/main/java/org/jvnet/hk2/config/provider/internal/ConfigThreadContext.java: private static final ThreadLocal tlc = new ThreadLocal(); |
@jwells131313 said: |
Marked as fixed on Wednesday, January 14th 2015, 8:36:27 am |
morj said: |
@jwells131313 said: 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). Note the "AND". If the ThreadLocal instance is not accessible it can be garbage collected like any other variable. After all, if a ThreadLocal is no longer accessible how would anyone access those objects? |
morj said: |
morj said: |
@jwells131313 said: |
@saden1 said: I'm not sure I fully understand the behavior of HK2 and thread scope inside a web container with shared thread pool. Suppose: 1. I have a thread scoped service called "MyThreadScopedService" What happens after the request is fulfilled? We know that thread T1 is going to go back into the container thread pool so I wonder if a subsequent request R2 is assigned thread T1 will MyResource get a new instance of MyThreadScopedService or will it get the previous instance of MyThreadScopedService? |
@jwells131313 said: https://hk2.java.net/2.5.0-b05/extensibility.html#Operations They are really useful, though sometimes it does take a little time to wrap your brain around them. Once you do you'll see nearly everything as an Operation lol at least that's what one of the guys who some work with them told me |
@saden1 said: Reading this totally made my day. Bravo! Bravo! |
bobzilla42 said: |
bobzilla42 said: |
This issue was imported from java.net JIRA HK2-241 |
HK2 can sometimes be deployed and undeployed in an application server that uses thread pools. If this is done then the use of ThreadLocal variables in HK2 can cause memory leaks (and class leaks etc) in the application server, as the threads created by the appserver will go back into the pool and the ThreadLocals will not be cleaned up.
Therefor the use of ThreadLocal variables should be minimized except where absolutely necessary (for example in PerThread context). Some warning should be given in the PerThread cases that its use in appservers can cause memory leaks.
Affected Versions
[2.2.0]
The text was updated successfully, but these errors were encountered: