gpt4 book ai didi

java - Tomcat8内存泄漏

转载 作者:IT老高 更新时间:2023-10-28 21:01:20 26 4
gpt4 key购买 nike

当我尝试在 Java 8 上停止 tomcat8 时,出现一些内存泄漏错误:

org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] 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. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
23-Jan-2015 08:18:10.202 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [pool-5-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread


23-Jan-2015 08:18:10.205 SEVERE [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [com.util.ThreadLocalProperties$1] (value [com.util.ThreadLocalProperties$1@2fafda6e]) and a value of type [java.util.Properties] (value [{}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

ThreadLocalProperties 类是:

public class ThreadLocalProperties extends Properties {

private static final long serialVersionUID = -4260632266508618756L;
private final ThreadLocal<Properties> localProperties = new ThreadLocal<Properties>() {
@Override
protected Properties initialValue() {
return new Properties();
}
};

public ThreadLocalProperties(Properties properties) {
super(properties);
}

@Override
public String getProperty(String key) {
String localValue = localProperties.get().getProperty(key);
return localValue == null ? super.getProperty(key) : localValue;
}

@Override
public Object setProperty(String key, String value) {
return localProperties.get().setProperty(key, value);
}

public ThreadLocal<Properties> getThreadLocal() {
return localProperties;
}
}

然后我像这样开始和停止它:

@WebListener()
public class GeneralListener implements ServletContextListener {

ThreadLocalProperties threadLocalProperties = new ThreadLocalProperties(System.getProperties());

@Override
public void contextDestroyed(ServletContextEvent arg0) {
threadLocalProperties.getThreadLocal().remove();
}

@Override
public void contextInitialized(ServletContextEvent arg0) {
System.setProperties(threadLocalProperties);
}
}

为什么我会得到所有这些内存泄漏错误?同样,当我运行关机时,它并没有关闭它,我必须手动终止该进程。

怎么了?

最佳答案

没有什么可担心的。这是当它检测到应用程序已启动它自己的 Thread 或创建了 ThreadLocal 时,tomcat 输出的标准消息。如果您在关闭时终止线程并在不再需要 threadlocals 时删除它们,则不会有问题。

Why would I get all this memory leaks errors? as well, when I run shutdown, it doesn't shut it down, I have to manually kill the process.

我见过这种行为,当应用程序启动 ScheduledExecutor 时(但任何其他 Thread/TheadPool 都会发生这种情况)并且没有在 contextDestroyed 上关闭它。因此,请检查您是否在应用程序/服务器停止时关闭线程。

现在谈谈你的具体问题为什么服务器没有停止:JDBC 驱动程序在 JVM 中注册为单例,并与所有 Web 应用程序共享。更多信息 here.解决问题的最佳方法是将 MySQL 驱动程序移动到 Tomcat 的 /lib 文件夹中。如果你做不到,你可以试试this但它更像是一种 hack,而不是真正的解决方案。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com