记录一次内存泄漏问题的解决
问题现象:启动tomcat后,过几个小时,线上的工程就会停止运行了,日志里面就会报下面这样的错误

13-Aug-2019 12:51:27.248 SEVERE [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4a9cfa60]) and a value of type [com.daichao.loans.api.common.AuthenticationType] (value [TOKEN]) 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.

这个是由于类加载器泄漏导致的的,从上面的问题可以看出来当应用结束的时候,想释放AuthenticationType,但是却释放不了,为什么释放不了呢?
先看代码:
先在LoginUtils里面定义了一个ThreadLocal

public static final ThreadLocal<AuthenticationType> authenticationType = new ThreadLocal<AuthenticationType>();

再到SystemController类里设置值

LoginUtils.authenticationType.set(authenticationType);

最后再到MyRealm里取值

AuthenticationType authTypeClass = LoginUtils.authenticationType.get();

tomcat有一个线程池,当有外部请求来的时候,会从线程池里面取出一个线程来处理请求,ThradLocal引用了AuthenticationType,线程对ThradLocal设值,相当于将AuthenticationType绑定到了这个线程,当应用结束了,想去回收AuthenticationType的classloader,即使已经没有其他地方对AuthenticationType的引用了,但线程还没有结束,这个线程还对AuthenticationType有引用,tomcat回收资源的时候,发现AuthenticationType回收不了,就会出现内存泄漏的警告。

最后的解决方法是:当应用结束的时候手动释放ThreadLocal里面的AuthenticationType

try{AuthenticationType authTypeClass = LoginUtils.authenticationType.get();
}finally{LoginUtils.authenticationType.remove();
}

参考文章:https://blog.csdn.net/u010619243/article/details/62899033

Threads are going to be renewed over time to try and avoid a probable memory leak.相关推荐

  1. PAS启动:制台登录不了,用curl在服务器端测试没有反应,后台日志报错:Threads are going to be renewed over time to

    [问题描述] PAS 6.5 build1733 重启后,PAS管理控制台登录不成功,用curl命令在服务器端测试也没有返回,后台server.log里频繁出现如下异常: [2022-06-28T15 ...

  2. failed to remove it when the web application was stopped. Threads are going to be renewed over time

    该error的原因: tomcat关闭应用时的清理工作(3): ThreadLocal 这个泄露其实是可能造成classloader的泄露,因为ThreadLocal引用了自定义的类MyClass,绑 ...

  3. Tomcat 7.x热部署

    由于在开发阶段,需要频繁的修改源代码,如果每次修改完代码都去重启服务器来检测效果,那简直麻烦到你想死,又耽误时间,所以,在网上找了许久,终于找到了解决办法, 特来分享 我也试了修改Server.xml ...

  4. Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity

    1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...

  5. mysql sha1prng_Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException | 学步园...

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  6. Tomcat无法shutdown进程问题解决办法

    转:http://blog.csdn.net/yydcj/article/details/8482916 我自己平时处理是: ps 获取这个服务的进程号,全部kill掉,再sh startup.sh ...

  7. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor

    1.错误描写叙述 信息: Setting autowire strategy to name 2014-7-13 1:37:43 org.apache.struts2.spring.StrutsSpr ...

  8. Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

    1.错误叙述性说明 2014-7-12 0:38:57 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplica ...

  9. 使用ThreadLocal不当可能会导致内存泄露

    使用ThreadLocal不当可能会导致内存泄露 基础篇已经讲解了ThreadLocal的原理,本节着重来讲解下使用ThreadLocal会导致内存泄露的原因,并讲解使用ThreadLocal导致内存 ...

最新文章

  1. python 调用函数 开销_减少python中的函数调用开销
  2. 蓝桥杯乘法运算java,第四届蓝桥杯Java B——有理数类
  3. boost::fusion::make_fused_procedure用法的测试程序
  4. vim linux python3,VIM:在python-mode中使用python3解释器
  5. [leetcode] 413. 等差数列划分
  6. javascript字符串的方法
  7. 虚拟机克隆后没有IP
  8. linux删除进程文件,Linux如何查找或删除正在使用某文件的进程
  9. Unsupervised Learning | 对比学习——13篇论文综述
  10. 如何在ImageRanger中设置人脸识别来整理照片分类
  11. codesys 实现冒泡排序
  12. FCPX注释解说文字标题插件:motionVFX mCallouts Simple 2
  13. Nature’s 10——Nature发布2021年帮助塑造科学的十人名单
  14. 期货开户客户出入金是什么时间?
  15. 根据图片名字生成文件夹并归类+批量重命名照片名
  16. matlab f检验 f值,如何用matlab进行F检验
  17. rman-20207
  18. 免费WebCamps-北美,亚洲和欧洲-*立即注册*
  19. Windows使用任务计划执行批处理定时重启IIS部署的网站
  20. ASP.NET实现多文件上传的方法

热门文章

  1. 中国石油大学《安全管理》第一阶段在线作业
  2. getchar()的使用方法——给scanf当爹又当妈
  3. C语言之getchar()用法
  4. 面向对象:希望我们能够耐得住寂寞,守得住繁华
  5. 中国国际视听大会即将在北京召开,国产8K影视品牌齐聚
  6. MySQL—Replication
  7. python数据类型哪些是无序的_Python自学知识-Python中的数据类型有哪些?
  8. Android:自定义View实现签名带笔锋效果
  9. FPGA时序约束与分析 (2)--- primary clock主时钟约束
  10. 详细介绍mysql索引类型:FULLTEXT、NORMAL、SPATIAL、UNIQUE