java - Tomcat Guice/JDBC 内存泄漏

标签 java tomcat

由于 Tomcat 中的孤立线程,我遇到了内存泄漏。特别是,Guice 和 JDBC 驱动程序似乎没有关闭线程。

Aug 8, 2012 4:09:19 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.
Aug 8, 2012 4:09:19 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.

我知道这类似于其他问题(例如 this one ),但就我而言,“别担心”的答案是不够的,因为它给我带来了问题。我有 CI 服务器,它会定期更新这个应用程序,并且在 6-10 次重新加载后,CI 服务器会因为 Tomcat 内存不足而挂起。

我需要能够清除这些孤立的线程,这样我才能更可靠地运行我的 CI 服务器。任何帮助将不胜感激!

最佳答案

我刚刚自己处理了这个问题。与其他一些答案相反,我不建议发出 t.stop() 命令。此方法已被弃用,并且有充分的理由。引用 Oracle's reasons这样做。

但是有一个解决方案可以消除此错误,而无需求助于 t.stop()...

@Oso 提供的大部分代码都可以使用,只需替换以下部分

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
for(Thread t:threadArray) {
    if(t.getName().contains("Abandoned connection cleanup thread")) {
        synchronized(t) {
            t.stop(); //don't complain, it works
        }
    }
}

使用MySQL驱动提供的如下方法替换:

try {
    AbandonedConnectionCleanupThread.shutdown();
} catch (InterruptedException e) {
    logger.warn("SEVERE problem cleaning up: " + e.getMessage());
    e.printStackTrace();
}

这应该会正确关闭线程,并且错误应该消失。

关于java - Tomcat Guice/JDBC 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872316/

相关文章:

tomcat - 就 CATALINA_HOME 环境变量而言,让 TomEE 和 Tomcat 在 Windows 上运行

tomcat - 为 Grails 3 组合 Tomcat 和嵌入式 Tomcat JNDI 配置

java - 使用 Timer/ScheduledThreadPool 来减少频繁更改元素的 CPU 负载

java - SWT 中从右到左的 Shell

java - 精确计算正弦和余弦函数

jakarta-ee - 如何从一个 ViewScoped bean 访问另一个?

Java EE webapp 不创建文件/文件夹

tomcat - JRuby : Warbler: Tomcat: Rails helpers not getting base URL with WAR extension suffix

java - Mac 上的 Neo4j : Prevent org. neo4j.server.Bootstrapper 出现在 dock 中

java - 无法反序列化 - Hibernate Spring Boot