博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution
阅读量:6913 次
发布时间:2019-06-27

本文共 4144 字,大约阅读时间需要 13 分钟。

Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz3QDWa3Zqi

Tomcat web server often suffers from 
java.lang.OutOfMemoryError: PermGen space whenever you deploy and undeploy your web application couple of time. No matter you are using 
tomcat6
tomcat7 or using bundled tomcat in Netbeans or  you will face this error now and then while developing web application on tomcat server. I thought about this article after writing  . I have touched this issue there but then I thought to write separate tutorial for tomcat outofmemoryerror because I am getting this error too frequently.
 
In this article we will see 
what causes java.lang.OutOfMemoryError: PermGen Space in tomcat and 
how to fix java.lang.OutOfMemoryError: PermGen Space in tomcat server.
 

Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution

Cause of OutOfMemoryError in PermGen space in Tomcat:

PermGen Space of heap is used to store classes and Meta data about  . When a class is loaded by a classloader it got stored in PermGen space, it gets unloaded only when the classloader which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError
: PermGen space. Another important point is that when you deploy your web application a new Clasloader gets created and it loads the classes used by web application. So if Classloader doesn't get garbage collected when your web application stops you will have memory leak in tomcat.
 

Solution of Tomcat: OutOfMemroyError:

1) Find the offending classes which are retaining reference of   and preventing it from being garbage collected. Tomcat provides memory leak detection functionality 
after tomcat 6 onwards which can help you to find when particular library, framework or class is causing memory leak in tomcat. Here are some of the common causes of 
java.lang.OutOfMemoryError: PermGen space in tomcat server:
 
1) 
JDBC Drivers:
JDBC drivers are most common cause of java.lang.OutOfMemoryError: PermGen space in tomcat if web app doesn't unregister during stop. One hack to get around this problem is that JDBC driver to be loaded by common class loader than application classloader and you can do this by transferring driver's jar into tomcat lib instead of bundling it on web application's war file.
 
2) 
Logging framework:
Similar solution can be applied to prevent logging libraries like Log4j causing java.lang.OutOfMemoryError: PermGen space in tomcat.
 
3) 
Application Threads which have not stopped.
Check your code carefully if you are leaving your   unattended and running in while loop that can retain classloader's reference and cause java.lang.OutOfMemoryError: PermGen space in tomcat web server. Another common culprit is ThreadLocal, avoid using it until you need it absolutely, if do you make sure to set them null or free any object   are holding.
 
Another Simple Solution is to increase PermGen   in catalina.bat or catalina.sh of tomcat server; this can give you some breathing space but eventually this will also return in 
java.lang.OutOfMemoryError: PermGen space after some time.
 

Steps to increase PermGen Heap Space in Tomcat:

 
1) Go to Tomcat installation directory i.e C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.14\bin in Windows and something similar in linux.
 
2) Add JAVA_OPTS in your catalina.bat or Catalina.sh
 
In Windows:
 
set JAVA_OPTS="-Xms1024m -Xmx10246m -XX:NewSize=256m -XX:MaxNewSize=356m -XX:PermSize=256m -XX:MaxPermSize=356m"
 
In linux:
 
export JAVA_OPTS="-Xms1024m -Xmx10246m -XX:NewSize=256m -XX:MaxNewSize=356m -XX:PermSize=256m -XX:MaxPermSize=356m"
 
You can change the actual heap size and PermGen Space as per your requirement.
 
3) Restart Tomcat.
 
As I said earlier 
increasing PermGen space can prevent 
java.lang.OutOfMemoryError: PermGen in tomcat only for some time and it will eventually occur based on how many times you redeploy your web application, its best to find the offending class which is causing memory leak in tomcat and fix it.

转载地址:http://xsicl.baihongyu.com/

你可能感兴趣的文章
Linux-weblogic 10.3.6控制台密码解密过程
查看>>
我奋斗的目标是什么
查看>>
模式识别 - 视频检测器(VideoDetector)项目设计
查看>>
Android 位置服务与GPS实时定位
查看>>
Nginx配置——用户认证
查看>>
SD卡分区及取消分区
查看>>
创建VLAN、中继链路和参与以太网捆绑的详细配置和截图
查看>>
健康的办公族作息时刻表
查看>>
新博客地址此博客不再更新baishuchao.github.io
查看>>
Git问题Everything up-to-date解决
查看>>
淘宝Tengine安装指南
查看>>
nginx-mysql-php安装配置
查看>>
div加链接 html给div加超链接实现点击div跳转的方法
查看>>
layer 旋转
查看>>
菜鸟学Linux 第016篇笔记 bash脚本之文件测试,变量类型
查看>>
Vue cli 资源文件的引用
查看>>
OCP知识点讲解 之 Undo回滚基础知识
查看>>
跨碧岭观赏龙潭瀑布群
查看>>
Windows 命令打开无线WIFI热点
查看>>
编写一个算法,若M*N矩阵中某个元素为0,则将其所在的行与列清零。
查看>>