关于tomcat总是memory leak问题

tomcat的catalina.out日志文件里总是输出

异常如下:

 2012-2-9 17:43:12 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/codeMarket] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2012-2-9 17:43:12 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/codeMarket] registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
2012-2-9 17:43:12 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/codeMarket] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.
2012-2-9 17:43:12 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap
严重: The web application [/codeMarket] created a ThreadLocal with key of type [null] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@e1666]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@e0ada6]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
2012-2-9 17:43:12 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap
严重: The web application [/codeMarket] created a ThreadLocal with key of type [null] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@a8a314]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@16ab2e8]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

http://blog.sina.com.cn/s/blog_4550f3ca0101byg1.html

解决方法一:

重写org.apache.commons.dbcp.BasicDataSource  的 close()方法

(继承某datasource,写drivermanager 关闭方法)

在应用程序关闭里手动注销JDBC驱动

解决方法二:

写一个应用程序上下文监听器,在contextDestroyed方法中加入如下代码:

DriverManager.deregisterDriver(DriverManager.getDriver(url));

如果你不知道url,那就这样:

Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
	Driver driver = drivers.nextElement();
	try {
		DriverManager.deregisterDriver(driver);
		Logger.getLogger(
				this.class.getName())
				.log(Level.INFO,
						String.format("deregistering jdbc driver: %s",
								driver), driver);
	} catch (SQLException e) {
		Logger.getLogger(
				this.class.getName())
				.log(Level.SEVERE,
						String.format("Error deregistering driver %s",
								driver), e);
	}
}

BasicDataSource's method close() doesn't deregister JDBC driver. This causes permgen memory leaks in web server environments, during context reloads. For example, using Tomcat 6.0.26 with Spring, and BasicDataSource declared in Spring context, there is a message printed at web application reload:

SEVERE: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

I was able to fix it by overriding close method this way:

public class XBasicDataSource extends BasicDataSource {
    @Override
    public synchronized void close() throws SQLException {
        DriverManager.deregisterDriver(DriverManager.getDriver(url));
        super.close();
    }
}

来源:https://issues.apache.org/jira/browse/DBCP-332

关于第二个问题:

created a ThreadLocal with key of type。。。。。This is very likely to create a memory leak.

http://blog.csdn.net/zhuhezan/article/details/6882089

但是有一点,关闭后,只是不检查了。问题还是存在那的。怎么感觉像 掩耳盗铃

猜你喜欢

转载自wjlvivid.iteye.com/blog/1782866