ThreadLocal variables are infamous for creating memory leaks. A memory leak in Java is amount of memory hold by object which are not in use and should have been garbage collected, but because of unintended strong references, they still live in Java heap space. There are many ways memory leak can be caused in Java but when this memory leak is caused due to ThreadLocal variable, it’s refereed as ThreadLocal memory leak. In our last post about ThreadLocal variable, we have seen How ThreadLocal variable can make SimpleDateFormat thread-safe and also raised point that in managed environment like J2EE application server or web server like Tomcat, Jetty, WebSphere or Weblogic use of ThreadLocal should be avoided. In this post we will go little deep and find out How ThreadLocal variables creates memory leak in Java web application, much like we have seen How to fix PermGen memory leak in Tomcat. If you are not familiar with ThreadLocal variables I suggest to go through my previous post. Before going into detail let's recap How ThreadLocal class works in Java.

How ThreadLocal works in Java :

ThreadLocal in Java is a mechanism to provide separate copy of shared object to every Thread. So that they no longer shared between multiple Threads and remain thread-safe. ThreadLocal variables are stored in a special map called ThreadLocalMap which is designed to hold thread local objects, it uses WeakReferences for keys.

Since every Thread has strong reference to there copy of ThreadLocal variables, they are not garbage collected until Thread is Alive and this is what creates memory leak in a typical J2EE web application. This is explained in detail in next section “How ThreadLocal variable creates memory leak in Java”.

How ThreadLocal creates memory leak in Java

In web server and application server like Tomcat or WebLogic, web-app is loaded by a different ClassLoader than which is used by Server itself. This ClassLoader loads and unloads classes from web application. Web servers also maintains ThreadPool, which is collection of worker thread, to server HTTP requests. Now if one of the Servlet or any other Java class from web application creates a ThreadLocal variable during request processing and doesn't remove it after that, copy of that Object will remain with worker Thread and since life-span of worker Thread is more than web app itself, it will prevent the object and ClassLoader, which uploaded the web app, from being garbage collected. This will create a memory leak in Server. Now if you do this couple of time you may see java.lang.OutOfMemoryError: PermGen space . Now this brings an important question,  is it possible to to use ThreadLocal variable safely in a managed environment?  Answer is Yes,, but that requires a careful usage of ThreadLocal variable and making sure to remove the object from ThreadLocal once done.

How to use ThreadLocal safely in Java Web application

Many people use Filters to initialize and remove ThreadLocal variables. You can initialize ThreadLocal in filter, put some expensive object as ThreadLocal and once request has been processed remove it from ThreadLocal as shown in below example:

public void doFilter(ServeletRequest request, ServletResponse){
try{ //set ThreadLocal variable
chain.doFilter(request, response) }finally{
//remove threadLocal variable.
}
}
That’s all on How ThreadLocal variable creates memory leak in Java. Having said that, If not necessary or You can manage without ThreadLocal variable than it’s best to avoid using ThreadLocal in managed environments like J2EE web and application servers.

转载自:https://javarevisited.blogspot.com/2013/01/threadlocal-memory-leak-in-java-web.html

ThreadLocal Memory Leak in Java web application - Tomcat的更多相关文章

  1. java web服务器tomcat介绍【转载】

    机器矩阵2016-08-10 22:14 java程序员亲切地称他为tom猫,看到这只猫可以说明1 服务器部署成功了 ,2 网络是联通的. 到底这只猫是什么来头呢? tomcat是Apache基金会下 ...

  2. java java web及tomcat的使用

     java web及tomcat的使用 一.什么是java web: 参考百度百科: http://baike.baidu.com/link?url=HnaWXFD7wFfPAlFMW02GV6r5p ...

  3. Java Web Application使Session永不失效(利用cookie隐藏登录)

    在做 Web Application 时,因为 Web Project 有 session 自动失效的问题,所以如何让用户登录一次系统就能长时间运行三个月,就是个问题. 后来,看到 session 失 ...

  4. java web(三) Tomcat虚拟目录映射方式

    Tomact服务器虚拟目录的映射方式 web应用开发好后若想被外界访问,需要将web应用所在的目录交给web服务器管理,这个过程称为虚拟目录的映射. 方式一:在server.xml文件的host元素中 ...

  5. Java web 项目 tomcat部署方式.

    本地做Java Web项目的时候常常会用到tomcat部署测试的问题, 这里介绍项目的部署方法: 1,配置文件的形式: 例如: 你的项目目录为:f:\workspaces\MyProject,此时使用 ...

  6. Web —— java web 项目 Tomcat 的配置 与 第一个web 项目创建

    目录: 0.前言 1.Tomcat的配置 2.第一个Web 项目 0.前言 刚刚开始接触web开发,了解的也不多,在这里记录一下我的第一个web项目启动的过程.网上教程很多,使用的java IDE 好 ...

  7. java web(一):tomcat服务器的安装和简单介绍,与eclipse关联

    一:下载tomcat安装包和安装 这个百度一下就可以了. 安装完以后进入tomcat的安装路径查看 如图所示:有几个目录简单介绍下 bin目录:   存放运行tomcat服务器的相关命令. conf目 ...

  8. JAVA and JAVA WEB with TOMCAT and ECLIPSE 学习过程中遇到的字符乱码问题及解决方法汇总(随时补充)

    JAVA语言具有跨平台,unicode字符集编码的特点. 但是在开发过程中处理数据时涉及到的字符编码问题零零散散,尤其是处理中文字符时一不留神就可能出现一堆奇奇怪怪的符号,俗称乱码. 对于乱码,究其原 ...

  9. java web 在tomcat没有正常输出

    目录 文章背景 目录 问题介绍 问题解决 说明 参考文章 版本记录 文章背景 调试程序时候突然发现一些位置设置的日志输出没有了,最后总算是解决了! 目录 问题介绍 本地运行时候的环境如下: windo ...

随机推荐

  1. 使用unsafe改善性能

    这种方式是Go所推荐的,优点就是安全,尽管这种操作会发生内存拷贝,导致性能上会有所损耗,这在处理一般业务时这种损耗是可以忽略的.但如果是拷贝频繁的情况下,想要进行性能优化时,就需要引入unsafe.P ...

  2. Greenplum 添加mirror步骤

    原文链接:https://yq.aliyun.com/articles/695864 [TOC] 概述 新安装的greenplum集群只有primary节点,没有mirror.高可用性没得到保证.所以 ...

  3. web实现大文件上传分片上传断点续传

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  4. Bzoj 2818: Gcd(莫比乌斯反演)

    2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对 ...

  5. 一次 react-router 中遇到的小坑

    react-router Link 标签不生效的问题 废话不多说, 直接上问题, 排解过程和答案 现象: 发现 使用 Link 标签没有 元素的样式和效果, 也不能进行跳转 代码如下: render( ...

  6. 1071 Speech Patterns (25)(25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  7. 【洛谷】P3177 [HAOI2015]树上染色

    懒得复制题面了直接传送门吧 分析 直接求点与点之间的距离感觉不是很好求,所以我们考虑换一个求法. 瞄了一眼题解 距离跟路径上边的长度有关,所以我们直接来看每一条边的贡献吧(这谁想得到啊) 对于每一条边 ...

  8. ciscn2019华北赛区半决赛day1web5CyberPunk

    刚比赛完的一段时间期末考试云集,没有时间复现题目.趁着假期,争取多复现几道题. 复现平台 buuoj.cn 解题过程 首先进入题目页面 看起来没有什么特别的,就是一个可以提交信息的页面.查看响应报文也 ...

  9. 第十二周助教工作总结——NWNU李泓毅

    助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:基于原型的团队项目需求调研与分析:https://www.cnblogs.com/nwnu-daizh ...

  10. C平衡二叉树(AVL)创建和删除

    AVL是最先发明的自平衡二叉查找树算法.在AVL中任何节点的两个儿子子树的高度最大差别为一,所以它也被称为高度平衡树,n个结点的AVL树最大深度约1.44log2n.查找.插入和删除在平均和最坏情况下 ...