ubuntu12下 tomcat的版本是7.0.40.0
状况:
发现某个web项目最近往往执行bin/shutdown.sh无法关闭tomcat服务器,必须手动kill进程才行。
该web项目主要执行定时执行大量的http请求 将返回结果处理后存入mongo数据库
因此查看catalina.out日志,发现停止tomcat时抛出很多内存泄漏的警告 以下:
INFO: Stopping service Catalina
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [AsyncAppender-Worker-Thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [ZkClient-EventThread-14-192.168.161.35:2181,192.168.161.36:2181,192.168.161.37:2181] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [localhost-startStop-1-SendThread(192.168.161.37:2181)] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [localhost-startStop-1-EventThread] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-1] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-3] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [MongoCleaner1479337147] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-1] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-3] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [MongoCleaner58644297] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [pool-4-thread-1] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [pool-4-thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [pool-4-thread-3] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [pool-4-thread-4] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
....
SEVERE: The web application [xxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@5a2224fb]) and a value of type [aaaaa] (value [aaaaa@77aa08f7]) 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.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [xxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@5a2224fb]) and a value of type [aaaaa] (value [aaaaa@3c32cd81]) 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.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [xxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@5a2224fb]) and a value of type [aaaaa] (value [aaaaa@2b51ef07]) 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.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [xxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@5a2224fb]) and a value of type [aaaaa] (value [aaaaa@1023cac9]) 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.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [xxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@5a2224fb]) and a value of type [aaaaa] (value [aaaaa@1dd65a66]) 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.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
其中线程名pool-4-thread-xxx引起了注意,应该是一个线程池,查看代码,发现为批量发送HTTP请求有建了一个ThreadPoolExecutor
private static ThreadPoolService threadPoolService = new ThreadPoolService(); private ThreadPoolService() { this.threadPoolExecutor = new ThreadPoolExecutor(100, 150, 500, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy()); // this.completionService = new // ExecutorCompletionService<Object>(this.threadPoolExecutor); }
日志中名叫pool-4-thread-xxx 有100个,很明显,tomcat没能关掉ThreadPoolExecutor的核心线程,因此需要在关闭tomcat前手动关闭,因此在web.xml中新增一个ServletContextListener的实现
@Override public void contextDestroyed(ServletContextEvent sce) { ThreadPoolService.getInstance().shutdownNow(); }
重新尝试 果然所有的
SEVERE: The web application [xxxxx] appears to have started a thread named [pool-4-thread-x] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
没有了,还剩下一些ThreadLocal无法释放以及剩下一些Thread无法释放的警告
查资料
http://stackoverflow.com/questions/5292349/is-this-very-likely-to-create-a-memory-leak-in-tomcat
回复中给出了ThreadLocal的解决方案
public Integer immolate() { int count = 0; try { final Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); final Field inheritableThreadLocalsField = Thread.class.getDeclaredField("inheritableThreadLocals"); inheritableThreadLocalsField.setAccessible(true); for (final Thread thread : Thread.getAllStackTraces().keySet()) { count += clear(threadLocalsField.get(thread)); count += clear(inheritableThreadLocalsField.get(thread)); } logger.info("immolated " + count + " values in ThreadLocals"); } catch (Exception e) { throw new Error("ThreadLocalImmolater.immolate()", e); } return count; } private int clear(final Object threadLocalMap) throws Exception { if (threadLocalMap == null) return 0; int count = 0; final Field tableField = threadLocalMap.getClass().getDeclaredField("table"); tableField.setAccessible(true); final Object table = tableField.get(threadLocalMap); for (int i = 0, length = Array.getLength(table); i < length; ++i) { final Object entry = Array.get(table, i); if (entry != null) { final Object threadLocal = ((WeakReference)entry).get(); if (threadLocal != null) { log(i, threadLocal); Array.set(table, i, null); ++count; } } } return count; }
根据答复中的代码猜想,ThreadLocal的报错 估计是某些线程的ThreadLocal无法释放,为什么无法释放,因为那些线程还没停掉,每个ThreadLocal都是被一个Thread的ThreadMap下以<ThreadLocalObject, Object>的entry形式维护着,
这些entry继承了WeakReference,以上代码应该是将每个thread的threadMap的entry设成null,这样原来的entry没有引用源,作为一个WeakReference会在GC中被清除掉。
事实上也是如此,在将ServletContextListener换成
public void contextDestroyed(ServletContextEvent sce) { ThreadPoolService.getInstance().shutdownNow(); immolate(); }
后,tomcat关闭报错只剩下一些Thread无法停止了
INFO: Stopping service Catalina
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [AsyncAppender-Worker-Thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [ZkClient-EventThread-14-192.168.161.35:2181,192.168.161.36:2181,192.168.161.37:2181] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [localhost-startStop-1-SendThread(192.168.161.37:2181)] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [localhost-startStop-1-EventThread] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-1] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-1-thread-3] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [MongoCleaner1479337147] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-1] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-2] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [cluster-2-thread-3] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [xxxxx] appears to have started a thread named [MongoCleaner58644297] but has failed to stop it. This is very likely to create a memory leak.
Feb 28, 2015 9:45:04 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
这些线程可能来自于各种框架以及各种未知...很难获取到它们去挨个停止,如果根据名称去找然后停止的话,又比较傻...继续查资料
http://wiki.apache.org/tomcat/MemoryLeakProtection 了解到
If a webapp creates a thread, by default its context classloader is set to the one of the parent thread (the thread that created the new thread). In a webapp, this parent thread is one of tomcat worker threads, whose context classloader is set to the webapp classloader when it executes webapp code.
Furthermore, the spawned thread may be executing (or blocked in) some code that involves classes loaded by the webapp, thus preventing the webapp classloader from being collected.
So, if the spawned thread is not properly terminated when the application is stopped, the webapp classloader will leak because of the strong reference held by the spawned thread.
就是说 在tomcat容器里,每个线程的ClassLoader归根结底都被默认设置成webapp ClassLoader。当某些线程无法及时关闭时,webapp classloader就会因为这些线程拥有的强引用,无法正常gc,因此报警。
了解了这个,就有办法了,修改public Integer immolate() method 插入几行
for (final Thread thread : Thread.getAllStackTraces().keySet()) { count += clear(threadLocalsField.get(thread)); count += clear(inheritableThreadLocalsField.get(thread)); if (thread != null) { thread.setContextClassLoader(null); } }
这样这些还没停止的线程的上下文ClassLoader就和webapp ClassLoader无关了,webapp ClassLoader可以正常GC,报错消失。
问题是解决了,但其中很多细节半猜半试出来的,并没有吃透它们,这些还待自己慢慢学习琢磨。
相关推荐
停止tomcat Webapp 用当前日期和时间创建一个数据文件 将其保存到jar文件中 将jar文件复制到已停止的tomcat应用程序的lib文件夹中 对于随机时间(1-5): 启动tomcat Webapp 运行jmeter场景 停止tomcat Webapp ...
- 在弹出的窗口中,需要修改两个关键设置:将`Content Root`指向项目的根目录,而`Web Root`则应指向`src/main/webapp`目录,这是Tomcat默认查找静态资源(包括JSP)的位置。 - 完成这些配置后,IDEA会自动创建...
项目中做文件上传接口时,可以把上传到tomcat的webapp目录下,返回文件的访问url。
可以通过注释掉`/conf/tomcat-users.xml`文件中的用户信息实现此目的,从而防止未经授权的登录尝试。 **5. 自定义SHUTDOWN字符串** 默认情况下,Tomcat提供了一个用于关闭服务器的HTTP接口,该接口通过发送一个...
cas服务器war包,用于搭建cas认证服务器,由于国内网的原因,下载老是超时,故此提供给,cas-server-webapp-tomcat-5.3.14
JDK TOMCAT MYSQL WEBAPP集成环境
cas-server-webapp-tomcat-5.2.2.war
Apache Tomcat是一款开源的、基于Java的Web应用服务器,它主要负责执行Java Servlet和JavaServer Pages(JSP)技术。这个“tomcat安装包zip”是Apache Tomcat的一个特定版本,这里是7.0.76版,这在当时是一个稳定且...
最近给客户部署git,下载了最新的gitblit1.8和最新版本JDK1.8,在执行gitblit.cmd时候,可以运行,但是嵌入到windows服务中就一直报错 试了各种办法之后,最后把jdk1.8的201版本,更换为151的版本,然后就成功了,以...
cas-server-webapp-tomcat-5.3.16.war maven依赖包
在本文中,我们将深入探讨如何使用Apache Tomcat服务器进行Web应用程序(webapp)的登录测试。Tomcat是一款广泛使用的开源Java Servlet容器,它允许开发者部署和运行基于Java的Web应用程序。我们将特别关注如何在...
在 Tomcat 中,不能在 web.xml 或者 webapp 的上下文中配置 Java 系统属性。如果需要设置不同的 Java 系统属性,需要编辑 Tomcat 的启动脚本,并添加"-D"选项。 4. 如何使用 log4j 作为 Tomcat 的日志输出? log4j...
为了下次能快速的下载这个war包,留份以备下次使用。
cas server webapp tomcat-5.3.0.war
cas-server-webapp-tomcat-5.2.1.war cas-server-webapp-tomcat-5.2.1.war
cas服务器war包,用于搭建cas认证服务器,由于国内网的原因,下载老是超时,故此提供给各位码农
标题和描述中提到的"flex java环境中需要在tomcat的webapp中新加的war文件",实际上是指要将包含BlazeDS服务的WAR文件添加到Tomcat服务器的web应用程序结构中。这个WAR文件通常名为`blazeds.war`或`flex-services....
在本教程中,我们将详细介绍如何在STS中配置Tomcat服务器,以便进行本地开发和调试。 首先,我们需要下载并安装Spring Tool Suite。访问Spring官网,找到STS的下载页面,根据操作系统选择相应的版本(Windows、Mac...
绝对成功,自己要用到的环境,千方百计寻找总结出的 亲测有效!
这个存储库有一个名为 deploy.sh 的脚本,它使您能够通过检出 repo、打包和构建工件、清除 tomcat 上以前的 webapps 并优雅地处理 tomcat 启动/停止来在 tomcat 上部署 webapp。 入门 对脚本的编辑 此脚本使用其 ...