web应用借助于结构:spring mvc + quartz结构,部署到tomcat容器时,shutdown时的error信息:

appears to have started a thread named [schedulerFactoryBean_Worker-1] but has failed to stop it. This is very likely to create a memory leak

quartz的thread终止的确保,加了个servletListener,工作就是显示的使用shutdown方法:

@Override
public void contextDestroyed(ServletContextEvent sce) {
LogAgent.debug(LoggerEnum.SYS_INFO, "QUARTZ.context destroy start.");
WebApplicationContext webApplicationContext = (WebApplicationContext) sce.getServletContext()
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); org.springframework.scheduling.quartz.SchedulerFactoryBean fact = (org.springframework.scheduling.quartz.SchedulerFactoryBean) webApplicationContext
.getBean("schedulerFactoryBean");
if (fact != null) {
try {
LogAgent.debug(LoggerEnum.SYS_INFO, "shedule shutdown start.");
fact.getScheduler().shutdown();
LogAgent.debug(LoggerEnum.SYS_INFO, "shedule shutdown end.");
} catch (SchedulerException e) {
LogAgent.error(LoggerEnum.ERROR_INFO, e);
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LogAgent.error(LoggerEnum.ERROR_INFO, e);
}
LogAgent.debug(LoggerEnum.SYS_INFO, "QUARTZ.context destroy end.");
}

接着的提示信息:

created a ThreadLocal with key of type [org.apache.commons.lang.builder.ReflectionToStringBuilder$1] (value [org.apache.commons.lang.builder.ReflectionToStringBuilder$1@1a08777c]) and a value of type [java.util.HashSet] (value [[]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

检查了代码,发现日志输出的部分,有个类使用了类:org.apache.commons.lang.builder.ToStringBuilder,其内部实现使用了org.apache.commons.lang.builder.ReflectionToStringBuilder,再跟进的时候发现,ToStringBuilder的append方法,在append和toString的时候,向ToStringStyle register或unregister(就是threadlocal的set obj和 remove);跟踪代码时发现明显该threadlocal在每次toString时都有remove,不应该会出现leak memory的倾向才对(若运行一半异常还是有可能,但该方法就是一个日志记录,所以无这方面的担心),最后检查pom时发现,commons-lang有两个包,分别是appache-lang和commons-lang:jar名都是commons-lang,删掉appache-lang的这个warn警告就没出现了(我也不记得了,为什么会引入两个commons-lang)。

结果再运行时,发现另一个错误了:

信息: Illegal access: this web application instance has been stopped already.  Could not load ch.qos.logback.core.status.WarnStatus.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1588)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at ch.qos.logback.classic.LoggerContext.noAppenderDefinedWarning(LoggerContext.java:175)
at ch.qos.logback.classic.Logger.callAppenders(Logger.java:267)
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:442)
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:396)
at ch.qos.logback.classic.Logger.debug(Logger.java:503)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:612)

首先级别上来看,是信息,不是错误;

但是本着看到error stack就不爽的本质,了解了一把,原因大致如此:

  • quartz的SimpleThreadPool在初始化的时候,启动了一定数量的线程:WorkerThread,然后它就让WorkerThread自己滴去干活了。
  • 然后当容器发出shutdown的信号时,pool就告诉workerthread们,要shutdown了,现在问题来了,看代码:
 /**
* <p>
* Signal the thread that it should terminate.
* </p>
*/
void shutdown() {
run.set(false);
}

很明显,这就是它的shutdown方法,它并不是要自己杀掉自己,而是告知自己的当前状态是false了,再来看看该状态作用的地方:

 @Override
public void run() {
boolean ran = false; while (run.get()) {
try {
synchronized(lock) {
while (runnable == null && run.get()) {
lock.wait(500);
} if (runnable != null) {
ran = true;
runnable.run();
}
}
} catch (InterruptedException unblock) {
// do nothing (loop will terminate if shutdown() was called
try {
getLog().error("Worker thread was interrupt()'ed.", unblock);
} catch(Exception e) {
// ignore to help with a tomcat glitch
}
} catch (Throwable exceptionInRunnable) {
try {
getLog().error("Error while executing the Runnable: ",
exceptionInRunnable);
} catch(Exception e) {
// ignore to help with a tomcat glitch
}
} finally {
synchronized(lock) {
runnable = null;
}
// repair the thread in case the runnable mucked it up...
if(getPriority() != tp.getThreadPriority()) {
setPriority(tp.getThreadPriority());
} if (runOnce) {
run.set(false);
clearFromBusyWorkersList(this);
} else if(ran) {
ran = false;
makeAvailable(this);
} }
} //if (log.isDebugEnabled())
try {
getLog().debug("WorkerThread is shut down.");
} catch(Exception e) {
// ignore to help with a tomcat glitch
}
}

其实就是第5行,对run进行判断,如果是false,就直接退出了。

所以pool的shut通知并不是马上就能被执行,如果线程正好走while判断条件,马上就会被退出;

如果该thread无任务可做时,见第8行:lock.wait(500),所以会wait,这个时候再退出,结果去getLog的时候,loggerContextListener.destroy先执行,无logger了,所以会有该信息。

  • 解决办法就是:

  将log的listener放到最后(如果有多个listener的话):

<listener>
<listener-class>com.xxx.listener.QuartzLitener</listener-class>
</listener>
<listener>
<listener-class>com.xxx.web.listener.LogbackListener</listener-class>
</listener>

memory leak-----tomcat日志warn的更多相关文章

  1. tomcat 6.0.44 “has failed to stop it. This is very likely to create a memory leak” 问题调查

    1. 问题起因 我们项目中缓存模块某个实现采用了ehcache(2.4.3),当项目部署到tomcat中后,对tomcat做停止服务操作(点击eclipse的console红色的停止按钮,奇怪的是有小 ...

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

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

  3. Tomcat运行一段时间后,自动停止关闭,To prevent a memory leak,Druid 数据库连接自动关闭, the JDBC Driver has been forcibly unregistered.

    1. Tomcat 错误日志 tail -100f tomcat9/logs/catalina.out 21-Sep-2017 23:05:39.301 INFO [Thread-5] org.apa ...

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

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

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

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

  6. ThreadLocal Memory Leak in Java web application - Tomcat

    ThreadLocal variables are infamous for creating memory leaks. A memory leak in Java is amount of mem ...

  7. tomcat memory leak

    Struts + Hibernate做项目,重新部署项目会出现Memory Leak严重报错.虽然不影响使用,但还是有风险.经实验发现是Hibernate的session没有关闭.....粗心....

  8. tomcat关闭时Log4j2报错 Log4j Log4j2-TF-4-Scheduled-1 memory leak

    出错信息: 23-Sep-2017 17:43:18.964 警告 [main] org.apache.catalina.loader.WebappClassLoaderBase.clearRefer ...

  9. python 实现过滤出tomcat日志中含有ERROR 或Exception 的行并保存在另一个文件

    遍历多个tomcat日志文件,找出含有ERROR 和Exception 的日志,并把该行日志输出到另一个文件中:(这里为了体现python模块导入的知识,所有建立了多个文件夹和模块) 项目结构: co ...

随机推荐

  1. 【ZZ】各类程序开发语言概述 | 菜鸟教程

    http://www.runoob.com/w3cnote/programming-intro.html 各类程序开发语言概述, 点击查看大图:

  2. 安卓控件获取器uiautomatorviewer初体验:"unable to connect to the adb. check if adb is installed correctly"

    解决方法:转自:https://plus.google.com/108487870030743970488/posts/2TrMqs1ZGQv Challenge Accepted:1. Screen ...

  3. 什么是ODBC ?

    ODBC(Open Database Connectivity,开放数据库互连)是微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有关数据库的一 ...

  4. sql在外键存在的情况下删除表

    SQL Server 批量 停用/启用 外键约束 今天百度知道上面,看到这样一个要求: 现在有一个库,有很多张表想要删除一张表的记录的时候,由于外键关联太多,所以,没法删除相应的记录,谁能帮忙写个存储 ...

  5. 蓝桥杯历届试题-垒色子(DP+矩阵快速幂)

    一.题目 垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体.经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!我们先来规范一下骰子: ...

  6. 6.1课堂笔记—DML(数据操作语言),DQL查询语句

    一.DML(数据操作语言) InnoDB MyISAM 支持事务 不支持事务 不支持全文索引 支持全文索引 支持外键约束 不支持 命令查看默认存储引擎 show variables like '%st ...

  7. AngularJS学习笔记(3)——通过Ajax获取JSON数据

    通过Ajax获取JSON数据 以我之前写的与用户交互的动态清单列表为例,使用JSON前todo.html代码如下: <!DOCTYPE html> <html ng-app=&quo ...

  8. SQL Server数据库partition by 与ROW_NUMBER()函数使用详解[转]

    关于SQL的partition by 字段的一些用法心得 先看例子: if object_id('TESTDB') is not null drop table TESTDB create table ...

  9. Git----分支管理01

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没有啥影响,不过,在某个时间点,两个平行宇宙合 ...

  10. go_组合接口

    main函数入口 package main import ( "fmt" "learngo/retriever/mock" "learngo/retr ...