vipx / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Guice + Tomcat potential memory leak #630

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I have just started using Google Guice with my Tomcat webapp, and have noticed 
the following in the catalina.out file whenever the WAR file is undeployed:

----
May 16, 2011 5:37:24 PM org.apache.catalina.startup.HostConfig checkResources 
INFO: Undeploying context [/app]

May 16, 2011 5:37:24 PM org.apache.catalina.loader.WebappClassLoader 
clearReferencesThreads SEVERE: A web application appears to have started a 
thread named [com.google.inject.internal.util.$Finalizer] but has failed to 
stop it. This is very likely to create a memory leak.

May 16, 2011 5:37:24 PM org.apache.catalina.loader.WebappClassLoader 
clearThreadLocalMap SEVERE: A web application created a ThreadLocal with key of 
type [null] (value [com.google.inject.internal.InjectorImpl$1@10ace8d]) and a 
value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@7e9bed]) but 
failed to remove it when the web application was stopped. To prevent a memory 
leak, the ThreadLocal has been forcibly removed.
----

I am not sure what causes this, or how I can stop it from happening.

I have only followed the instructions from here:
    http://code.google.com/docreader/#p=google-guice&s=google-guice&t=ServletModule

... and haven't done anything fancy with it yet. I just have 2 servlets and a 
filter.

This issue is also posted on StackOverflow:
http://stackoverflow.com/questions/6025089/guice-tomcat-potential-memory-leak

Original issue reported on code.google.com by pas...@gmail.com on 17 May 2011 at 4:39

GoogleCodeExporter commented 9 years ago
See issue 288.

Original comment by cgdec...@gmail.com on 17 May 2011 at 4:49

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 17 May 2011 at 5:05

GoogleCodeExporter commented 9 years ago
+sberlin, Could you help me understand why this is invalid?

It appears that InjectorImpl creates a ThreadLocal. Could GuiceFilter or 
GuiceServletContextListener be used to clean it up?

Original comment by tj.rothw...@gmail.com on 10 Nov 2011 at 6:32

GoogleCodeExporter commented 9 years ago
Yeah, I don't understand what is happening in Issue 288, but I guess it's 
related.

I think he meant Duplicate not Invalid.

It was also meant to be fixed in version 2.0... version 3.0 is out and still 
not fixed so go figure. Fingers crossed for 3.1

Original comment by pas...@gmail.com on 10 Nov 2011 at 7:03

GoogleCodeExporter commented 9 years ago
+pas256, Guava doesn't use FRQ anymore which fixes the Thread not stopping. You 
can use sisu-guice w/ guava 10.0.1 or build from the latest guice.

The Thread & ThreadLocal from the initial report are not the same thing.

Original comment by tj.rothw...@gmail.com on 10 Nov 2011 at 7:15

GoogleCodeExporter commented 9 years ago
Issue 288 (the background finalizer thread) will be fixed when the Guava 
dependency is upgraded to r10+

The ThreadLocal reported by Tomcat is separate - this is actually a simple 
Object array of length 1 which is used to manage contexts during injection. The 
array element is guaranteed to be null outside of any call to the injector so 
it won't cause any leak. The ThreadLocal will be reclaimed after the injector 
is no longer referenced. However it may appear to stay around for longer if you 
use reflection to peek into the ThreadLocal map like Tomcat does, due to the 
current JDK ThreadLocal implementation.

Original comment by mccu...@gmail.com on 10 Nov 2011 at 7:25

GoogleCodeExporter commented 9 years ago
Ah thanks for the details.

Original comment by pas...@gmail.com on 10 Nov 2011 at 7:29

GoogleCodeExporter commented 9 years ago
Our Tomcat also complains about this.

Because of this shortcoming in Guice, we cannot use Tomcat Manager to re-deploy 
our software. If we would use Manager, we would eventually run out of PermGen 
space.

Original comment by lauri.le...@gmail.com on 14 Jan 2013 at 2:09