tag:blogger.com,1999:blog-8712770457197348465.post5064686592516793801..comments2024-03-29T05:54:46.190-07:00Comments on Javarevisited: ThreadLocal Memory Leak in Java web application - Tomcatjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-8712770457197348465.post-1985206974319713952016-07-27T08:13:52.262-07:002016-07-27T08:13:52.262-07:00When you use static ThreadLocal for SimpleDateForm...When you use static ThreadLocal for SimpleDateFormat you can still get a class loader leak if you use double brace initialization for your SDF. For example in init method if you write: return new SimpleDateFormat(){{applyPattern("dd/MM/yyyy")}}; you will get class loader leak because you created an anonymous class that has a reference to your class loader. <br />However in that case youSrdjanhttps://www.blogger.com/profile/14675886466046086122noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63631445802687362332016-03-03T05:15:03.469-08:002016-03-03T05:15:03.469-08:00@Sriniva, yes, remove() is the right way to remove...@Sriniva, yes, remove() is the right way to remove object from thread local but when you set null, the object should be freed which can hold the classloader and other stuff, but I have not tried that. May be it's not allowed and throw NPE somewhere.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73814232462340274902016-02-29T21:49:41.935-08:002016-02-29T21:49:41.935-08:00What is the right way to remove the thread local o...What is the right way to remove the thread local object from the thread ?I see that ThreadLocal.remove() method is used in many posts. If ThreadLocal value is set to null or some default value like ThreadLocal.set(""), then also objects created in the application processors are GCed right ?kswaughshttps://www.blogger.com/profile/03520457393240230146noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-88005970046392582032015-09-20T04:19:58.636-07:002015-09-20T04:19:58.636-07:00Now this makes sense, threads in the pool managed...Now this makes sense, threads in the pool managed by container doesn't gets destroyed when application reloads (Only destroyed when container re-starts again), this causes thread local variable to be still present, since worker threads are still present so as its associate Objects, which it is referencing to. and when you keep on reloading application again and again, every time new thread Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61722705796174045052014-01-13T22:28:11.619-08:002014-01-13T22:28:11.619-08:00If your server is pooling the threads then in that...If your server is pooling the threads then in that case it will create the memory leak. Basically after serving your request, thread will be returned to the pool(not dead) . So, the thread local object will be associated with the thread even after the utility of it. So, best way is to remove the thread local object from the thread as soon as you are finished with it. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70871723360853276022013-11-14T19:10:12.122-08:002013-11-14T19:10:12.122-08:00Just to add on what Javin said, Yes this is true t...Just to add on what Javin said, Yes this is true that threadlocal variables can prevent a class and subsequently classloader which is loaded them from being garbage collected, but this will only create issue if you store application classes in ThreadLocal, which is loaded by web-app classloader. If you store classes from JDK e.g. SimpleDateFormat, it will not create memory leak in Java web Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1157835128075083852013-01-30T05:19:51.333-08:002013-01-30T05:19:51.333-08:00@Anonymous, yes if you remove ThreadLocal at every...@Anonymous, yes if you remove ThreadLocal at every request that it won't cause memory leak. Memory leak caused when you forget to remove ThreadLocal variable once done.Javin @ What is Inheritance in Javahttp://javarevisited.blogspot.sg/2012/10/what-is-inheritance-in-java-and-oops-programming.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-91203125589692721082013-01-30T04:26:34.988-08:002013-01-30T04:26:34.988-08:00"are not garbage collected until Thread is Al..."are not garbage collected until Thread is Alive"<br />Don't you mean "Dead"<br /><br />Regards<br />RenAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1258119678512949302013-01-29T22:57:57.993-08:002013-01-29T22:57:57.993-08:00Could you explain why it causes the memory leak ? ...Could you explain why it causes the memory leak ? The variable is removed from the ThreadLocal at evey request, so it should prevent memory leaking. Anonymousnoreply@blogger.com