How to solve memory leak problems?

2019-04-11 23:17发布

问题:

I have a problem with some thread and memory leak...In the log of my Tomcat7 I found this lines about my grails application:

SEVERE: The web application [/myApp] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-1] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-2] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-3] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-4] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-5] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-6] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2012 6:02:10 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myApp] appears to have started a thread named [pool-63-thread-7] but has failed to stop it. This is very likely to create a memory leak.

and more of those... The problem is that I think that all those threads are created in a part of the application developed from a 3th party JAR which I don't have the source and I can't modify by myself.

Is there a way to solve it, or at least to understand what is not going well?

thanks

回答1:

The first line indicates a bug in the MySQL JDBC driver. Looks like it's been fixed in 5.1.6 - see http://bugs.mysql.com/bug.php?id=36565 so you could try replacing the jar with the latest from http://dev.mysql.com/downloads/connector/j/

The other lines indicate a ThreadPool that was started, and not stopped when the application stops. This can really only be fixed by modifying the source code of the third-party jar, if that is where the problem is.

If possible, you could try temporarily eliminating the third-party jar to see if the issue goes away.



回答2:

For the ThreadPool problem, the solution I used (and it seems to work) was :

  • I created a ServletContextListener
  • in the contextDestroyed method, by using reflection, I call the close method of the DataSource - in my case, it seems to work with c3p0 and DBCP
  • for c3p0 I think one should also call the static method com.mchange.v2.c3p0.DataSources.destroy (dataSource);