Quantcast
Channel: User user1944408 - Stack Overflow
Viewing all articles
Browse latest Browse all 26

Answer by user1944408 for ThreadLocal garbage collection

$
0
0

Object b will not be subject for garbage collection if it somehow refers to your Test class. It can happen without your intention. For example if you have a code like this:

public class Test {    private static final ThreadLocal<Set<Integer>> a =     new ThreadLocal<Set<Integer>>(){            @Override public Set<Integer> initialValue(){                return new HashSet<Integer>(){{add(5);}};            }    };}

The double brace initialization {{add(5);}} will create an anonymous class which refers to your Test class so this object will never be garbage collected even if you don't have reference to your Test class anymore. If that Test class is used in a web app then it will refer to its class loader which will prevent all other classes to be GCed.

Moreover, if your b object is a simple object it will not be immediately subject for GC. Only when ThreadLocal.ThreadLocalMap in Thread class is resized you will have your object b subject for GC.

However I created a solution for this problem so when you redeploy your web app you will never have class loader leaks.


Viewing all articles
Browse latest Browse all 26

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>