1

嗨,我的应用程序在 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.

我想我需要在一些 spring 销毁处理程序中杀死这些线程,但我不确定如何找到销毁它们的句柄?

干杯! NFV

4

1 回答 1

3

我们使用 aServletContextListener来取消注册 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>

关机监听器

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);
        }
      }
    }
  }
}
于 2013-01-07T19:21:34.153 回答