ThreadLocal Memory Leak in Java web application - Tomcat
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.
}
}
转载自:https://javarevisited.blogspot.com/2013/01/threadlocal-memory-leak-in-java-web.html
ThreadLocal Memory Leak in Java web application - Tomcat的更多相关文章
- java web服务器tomcat介绍【转载】
机器矩阵2016-08-10 22:14 java程序员亲切地称他为tom猫,看到这只猫可以说明1 服务器部署成功了 ,2 网络是联通的. 到底这只猫是什么来头呢? tomcat是Apache基金会下 ...
- java java web及tomcat的使用
java web及tomcat的使用 一.什么是java web: 参考百度百科: http://baike.baidu.com/link?url=HnaWXFD7wFfPAlFMW02GV6r5p ...
- Java Web Application使Session永不失效(利用cookie隐藏登录)
在做 Web Application 时,因为 Web Project 有 session 自动失效的问题,所以如何让用户登录一次系统就能长时间运行三个月,就是个问题. 后来,看到 session 失 ...
- java web(三) Tomcat虚拟目录映射方式
Tomact服务器虚拟目录的映射方式 web应用开发好后若想被外界访问,需要将web应用所在的目录交给web服务器管理,这个过程称为虚拟目录的映射. 方式一:在server.xml文件的host元素中 ...
- Java web 项目 tomcat部署方式.
本地做Java Web项目的时候常常会用到tomcat部署测试的问题, 这里介绍项目的部署方法: 1,配置文件的形式: 例如: 你的项目目录为:f:\workspaces\MyProject,此时使用 ...
- Web —— java web 项目 Tomcat 的配置 与 第一个web 项目创建
目录: 0.前言 1.Tomcat的配置 2.第一个Web 项目 0.前言 刚刚开始接触web开发,了解的也不多,在这里记录一下我的第一个web项目启动的过程.网上教程很多,使用的java IDE 好 ...
- java web(一):tomcat服务器的安装和简单介绍,与eclipse关联
一:下载tomcat安装包和安装 这个百度一下就可以了. 安装完以后进入tomcat的安装路径查看 如图所示:有几个目录简单介绍下 bin目录: 存放运行tomcat服务器的相关命令. conf目 ...
- JAVA and JAVA WEB with TOMCAT and ECLIPSE 学习过程中遇到的字符乱码问题及解决方法汇总(随时补充)
JAVA语言具有跨平台,unicode字符集编码的特点. 但是在开发过程中处理数据时涉及到的字符编码问题零零散散,尤其是处理中文字符时一不留神就可能出现一堆奇奇怪怪的符号,俗称乱码. 对于乱码,究其原 ...
- java web 在tomcat没有正常输出
目录 文章背景 目录 问题介绍 问题解决 说明 参考文章 版本记录 文章背景 调试程序时候突然发现一些位置设置的日志输出没有了,最后总算是解决了! 目录 问题介绍 本地运行时候的环境如下: windo ...
随机推荐
- yum 安装 出错 Error: Protected multilib versions:
例如:安装zlib出错 yum install zlib-1.2.3-29.el6.i686 Error: Protected multilib versions: zlib-1.2.3-29.el6 ...
- learning java Runtime类中的exec
var rt = Runtime.getRuntime(); // 类c语言当中的system()函数. rt.exec("notepad.exe");
- suds
Suds: 是一个轻量级的SOAP客户端 pip install suds 可以访问webservice 选择公网的Webservice,http://www.webxml.com.cn/webser ...
- GoCN每日新闻(2019-09-28)
GoCN每日新闻(2019-09-28) 1. 可视化Go程序的调用图 https://truefurby.github.io/go-callvis/2. Go modules编写和发布官方教程 h ...
- sturts标签中
struts中的s标签中textfield和textarea中不需要加上value和中间值,通过name可以直接取到. textfield和textarea标签没有value属性,需要加上${ylqx ...
- IPV4 VS IPV6 谈谈省级ipv6的必要性
11月26日,中办.国办印发了<推进互联网协议第六版(IPv6)规模部署行动计划>,提出国内要在 5~10 年的时间形成下一代互联网自主技术体系和产业生态,建成全球最大规模的 IPv6 商 ...
- Fluent 批量添加线面、点面、平面的scheme实现 【转载】
转载自http://chan1629.blog.163.com/blog/static/19595703220137219166686 新建一个filename.scm,文件,用记事本打开. 在文件中 ...
- [C++] typeid关键字使用方法
typeid 关键字的作用就是获取一个表达式是类型,返回表达式的类型 表达式可以是类型名称.变量名.数字.字符串.指针.结构体等 #include <iostream> using nam ...
- Leet Code 1.两数之和
给定一个整数nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 可以假设每种输入只会对应一个答案.但是,不能重复利用这个数组中同样的元素. 题解 提交代 ...
- Ubuntu 18.04.1 安装mysql 5.7.27
sudo apt-get update sudo apt-get install mysql-server 配置 sudo mysql_secure_installation 参考文档 Ubuntu1 ...