1. 问题起因

我们项目中缓存模块某个实现采用了ehcache(2.4.3),当项目部署到tomcat中后,对tomcat做停止服务操作(点击eclipse的console红色的停止按钮,奇怪的是有小概率的情况不能复现这个问题??),发现tomcat不能正常停止,报错 appears to have started a thread named [xxx] but has failed to stop it. This is very likely to create a memory leak.java进程不能正常结束,需要手动kill进程。

信息: Stopping service Catalina
2015-9-26 11:40:17 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/hd] 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.
2015-9-26 11:40:17 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [pool-2-thread-1] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 11:40:17 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [net.sf.ehcache.CacheManager@268d15] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 11:40:17 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [org.hibernate.cache.spi.UpdateTimestampsCache.data] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 11:40:17 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [org.hibernate.cache.internal.StandardQueryCache.data] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 11:40:17 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080

2. ehcache 线程不会自动停止问题

分析后,发现是ehcache的三个线程不能自动停止(或者说ehcache未在应用停止/jvm停止的时候将其结束)。

经分析后发现可以在web应用中加listener,在应用destory的时候触发ehcache的释放资源与线程的调用。

3. 一定要加listener吗?

分析ehcache代码后发现,使用系统参数-Dnet.sf.ehcache.enableShutdownHook=true能启用ehcache的jvm shutdown hook。

net.sf.ehcache.CacheManager

/**
* Some caches might be persistent, so we want to add a shutdown hook if that is the
* case, so that the data and index can be written to disk.
*/
private void addShutdownHookIfRequired() { String shutdownHookProperty = System.getProperty(ENABLE_SHUTDOWN_HOOK_PROPERTY);
boolean enabled = PropertyUtil.parseBoolean(shutdownHookProperty);
if (!enabled) {
return;
} else {
LOG.info("The CacheManager shutdown hook is enabled because {} is set to true.", ENABLE_SHUTDOWN_HOOK_PROPERTY); Thread localShutdownHook = new Thread() {
@Override
public void run() {
synchronized (this) {
if (status.equals(Status.STATUS_ALIVE)) {
// clear shutdown hook reference to prevent
// removeShutdownHook to remove it during shutdown
shutdownHook = null;
LOG.info("VM shutting down with the CacheManager still active. Calling shutdown.");
shutdown();
}
}
}
}; Runtime.getRuntime().addShutdownHook(localShutdownHook);
shutdownHook = localShutdownHook;
}
}

jvm shutdown hook注意点:

tomcat自带的shutdown.sh/bat不能给java进程发送退出信号,jvm shutdown hook不能被触发。待调查

需要使用kill $pid,jvm shutdown hook 才能被触发

4. 之前的项目用了hibernate3,二级缓存依赖ehcache,为什么没有这个问题

难道是在什么地方加了destory?

准备demo,验证,存在一样的问题。只是由于hibernate3(3.2.5ga)依赖较低版本的ehcache(1.2.3),所以没有被停掉的线程是hibernate和其他一些部件的。

日志如下:

2015-9-26 10:53:15 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [/hd] 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.
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [Store org.hibernate.cache.UpdateTimestampsCache Spool Thread] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [Store org.hibernate.cache.UpdateTimestampsCache Expiry Thread] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [Store org.hibernate.cache.StandardQueryCache Spool Thread] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [/hd] appears to have started a thread named [Store org.hibernate.cache.StandardQueryCache Expiry Thread] but has failed to stop it. This is very likely to create a memory leak.
2015-9-26 10:53:15 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-8080
2015-9-26 10:53:15 org.apache.catalina.loader.WebappClassLoader loadClass

那以前的项目都上线了,为什么没这个问题?

猜测是:线上用的weblogic,开发时用tomcat 也应该存在这个问题,只不过点击eclipse控制台的停止按钮并不能重现这个问题

5. tomcat检查泄露的代码

org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads()方法中

if (isRequestThread(thread)) {
log.error(sm.getString("webappClassLoader.warnRequestThread",
contextName, thread.getName()));
} else {
log.error(sm.getString("webappClassLoader.warnThread",
contextName, thread.getName()));
}

6. 延伸阅读

http://blog.csdn.net/monkeyking1987/article/details/9182201

http://stackoverflow.com/questions/4899205/tomcat-6-memory-leaks-log-entries

http://stackoverflow.com/questions/11872316/tomcat-guice-jdbc-memory-leak

http://stackoverflow.com/questions/5292349/is-this-very-likely-to-create-a-memory-leak-in-tomcat

tomcat 6.0.44 “has failed to stop it. This is very likely to create a memory leak” 问题调查的更多相关文章

  1. quartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak解决

    01-Jul-2016 07:24:20.218 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 80 ...

  2. but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)

    以下是错误日志信息: 严重: The web application [/news] registered the JDBC driver [com.mysql.jdbc.Driver] but fa ...

  3. quartz集群报错but has failed to stop it. This is very likely to create a memory leak.

    quartz集群报错but has failed to stop it. This is very likely to create a memory leak. 在一台配置1核2G内存的阿里云服务器 ...

  4. 警告: The web application [ROOT] appears to have started a thread named [Thread-48] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:

    1. 问题描述 tomcat跑web项目(其中依赖java项目) 出现大量上述警告 项目起不来 关键字 memory leak 内存泄漏 2. 解决方案 难道是程序写的有问题? 最终 将tomcat ...

  5. The web application [ROOT] appears to have started a thread named [spring.cloud.inetutils] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:

    前景提要:启动SpringBoot项目报错 原因: DeliveryPointServiceFallBack上面没有加 @Component-_-!

  6. 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

    tomcat reload显示错误:SEVERE: The web application [/Interceptor] created a ThreadLocal with key of type ...

  7. tomcat启动部署APP报错:This is very likely to create a memory leak

    This is very likely to create a memory leak的错误,网上很多,原因也是各种各样,这里也仅提供一个解决的思路. 问题描述:启动tomcat时,不能访问部署的AP ...

  8. tomcat报错:This is very likely to create a memory leak问题解决

    tomcat memory leak解决方案 这种问题在开发中经常会碰到的,看看前辈的总结经验 Tomcat内存溢出的原因  在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一 ...

  9. 大神的---解决tomcat内存溢出问题----tomcat报错:This is very likely to create a memory leak问题解决

    tomcat memory leak解决方案 这种问题在开发中经常会碰到的,看看前辈的总结经验 Tomcat内存溢出的原因  在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一 ...

随机推荐

  1. MySQL 创建用户 与 授权

    例,需要给 121.52.215.100 连接添加一个用户 dee,密码是 123456,他只能对数据库 vshop 有 select 权限: CREATE USER '; GRANT SELECT ...

  2. 20145317彭垚 java课程总结

    课程总结 每周读书笔记链接汇总 第一周http://www.cnblogs.com/5317p/p/5248481.html 第二周http://www.cnblogs.com/5317p/p/527 ...

  3. php-fpm

    install php-fpm # Ubuntu sudo apt-get install python-software-properties; sudo add-apt-repository pp ...

  4. 读书笔记——《图解TCP/IP》(1/4)

    读书笔记——<图解TCP/IP>(1/4) 经典摘抄 第一章 网络基础知识 1.独立模式:计算机未连接到网络,各自独立使用的方式. 2.广域网 WAN 局域网 LAN 城域网 MAN 3. ...

  5. Python的运行

    1.在命令行中运行 2.使用shell(IDLE) 3.新建.py脚本 只要是编辑器都可以 4.脚本在IDLE中运行 5.在windows下的cmd下运行脚本

  6. hadoop-2.7.3 在windows环境下安装(无需Cygwin)

    http://blog.csdn.net/kokjuis/article/details/53537029

  7. IOS本地通知:UILocalNotification使用记录

    第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...

  8. RESTFul Web Api 服务框架(一)

    简介: 基于 REST 的 Web 服务日益成为后端企业服务集成的首选,因为它比 SOAP 更加简单.这篇文章介绍了一 个简单的可扩展框架,使用Asp.net Web Api作为 REST 服务的实现 ...

  9. H5 -- 本地存储计数器的值 和前端校验用户

    1. 存储计数器的值 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...

  10. Spring中的@response和@request注解

    @response 标注对象返回的格式为json文本 @requestBody将json对象转换为对应的java类