lundi 29 février 2016

Which is the best way to remove references of Thread Local objects

I have a static ThreadLocal object in my web application, where I am initializing with some default value in a web servlet filter class before processing the request. In the application, several other classes (like processors) uses and update its value. I have read in some articles that objects created with ThreadLocal are still associated with threads though the request process is completed as threads are maintained in a pool managed by container. This leads to memory leaks if the objects are not removed from ThreadLocal variable.

I have two questions in this context.

  1. Among the below options, which one is the best and proper way to remove its reference to prevent memory leaks.

    ThreadLocal obj;
    1) obj.remove(); // remove the contents of ThreadLocal.
    2) obj.set(""); // set some default value
    3) obj.set(null); // set null
    
    
  2. Are all options make the object eligible for garbage collection that is created in the processors.




Aucun commentaire:

Enregistrer un commentaire