Spring Tomcat permgen问题

时间:2013-01-07 16:59:44

标签: spring tomcat

您好我的应用程序在tomcat(以及glassfish)上出现了permgen错误,我知道它们是由挂在热部署之间的引用上的ClassLoader引起的。

我想跟踪内存泄漏,但之前没有这样做过,我认为一个好的明星会修复tomcat在重新部署时吐出的以下问题:

SEVERE: The web application [] registered the JDBC 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.
06-Jan-2013 19:47:08 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [] appears to have started a thread named [Resource Destroyer in BasicResourcePool.close()] but has failed to stop it. This is very likely to create a memory leak.

我想我需要杀死一些弹簧销毁处理程序中的这些线程,芽我不知道我怎么会找到销毁它们的句柄?

干杯! NFV

1 个答案:

答案 0 :(得分:3)

我们使用ServletContextListener取消注册JDBC驱动程序。

<强>的web.xml

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
...
  <listener>
    <listener-class>ShutdownListener</listener-class>
  </listener>

<强> ShutdownListener

public class ShutdownListener implements ServletContextListener {
  private final Logger logger = LoggerFactory.getLogger(ShutdownListener.class);

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    logger.info("Context initialized.");
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    deregisterJdbcDrivers();
    // more clean-up tasks here
  }

  private void deregisterJdbcDrivers() {
    final Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
      final Driver driver = drivers.nextElement();
      if (this.getClass().getClassLoader().equals(getClass().getClassLoader())) {
        try {
          DriverManager.deregisterDriver(driver);
          logger.info("Deregistered '{}' JDBC driver.", driver);
        } catch (SQLException e) {
          logger.warn("Failed to deregister '{}' JDBC driver.", driver);
        }
      }
    }
  }
}