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

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 tomcat6tomcat7 or using bundled tomcat in Netbeans or Eclipse you will face this error now and then while developing web application on tomcat server. I thought about this article after writing 2 Solution of OutOfMemoryError in Java. 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 classes in Java. 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 Classloader 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 thread 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 ThreadLocal variables are holding.

 

Another Simple Solution is to increase PermGen heap size 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.

时间: 2024-09-26 21:42:55

Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution的相关文章

java.lang.OutOfMemoryError: PermGen space及其解决方法

PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决方法也一定是加大内存.说说为什么会内存益出:这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入PermGen space区域,它和和存放Instance的Heap区域不同,GC(Garbage Collection)不会在主程序运行期对PermGen space进行清理

公司的系統點擊一個功能的時候報java.lang.OutOfMemoryError: PermGen space

问题描述 在公司的系統中點擊了一個打印的功能,前臺是用flex,后臺是java,打印的時候是彈出來一個jsp頁面,中間夾了大量的計算.在本地測試沒事 可以彈出來jsp頁面,但是放在測試機上時候就會報如下異常,我在測試服務器的環境變量里設置了 变名:JAVA_OPTS 变量值:-Xms1024m -Xmx1024m 還是沒有作用,不知道應該怎么處理了.是程序的問題嗎?java.lang.OutOfMemoryError: PermGen spaceat java.lang.ClassLoader.

java.lang.OutOfMemoryError: PermGen space: java reflection

原文地址:https://stackoverflow.com/questions/16130292/java-lang-outofmemoryerror-permgen-space-java-reflection/47090043#47090043 问题 使用如下java反射代码: Method method = LogFactory.class.getDeclaredMethod("getContextClassLoader"); method.setAccessible(true)

tomcat-java.lang.OutOfMemoryError: PermGen space

问题描述 java.lang.OutOfMemoryError: PermGen space 项目是用:Spring+Struts2+Mybatis开发的, 服务器:Tomcat7.x IDE:MyEclipse10 在项目启动时报内存溢出异常(java.lang.OutOfMemoryError: PermGen space) 解决方案 感谢各位大神的热心帮助.问题解决了,解决过程如下:Window --> Preferences --> MyEclipse --> Servers -

Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结(转)

java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的原因大都出于以下原因:JVM内存过小.程序不严密,产生了过多的垃圾. 导致OutOfMemoryError异常的常见原因有以下几种: 内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 代码中存在死循环或循环产生过多重复的对象实体: 使用的第三方软件中的BUG: 启动参数内存值设定的过小: 此错误常见的错误提示: tomcat

java.lang.OutOfMemoryError 错误整理及解决办法_java

java.lang.OutOfMemoryError处理错误 java.lang.OutOfMemoryError异常解决方法 原因: 常见的有以下几种: 1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 3.代码中存在死循环或循环产生过多重复的对象实体: 4.使用的第三方软件中的BUG: 5.启动参数内存值设定的过小: 常见错误提示: 1.tomcat:java.lang.OutOfMemoryError: Perm

tomcat-启动项目时!一直报错 OutOfMemoryError: PermGen space

问题描述 启动项目时!一直报错 OutOfMemoryError: PermGen space java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect

Eclipse+Jboss报java.lang.OutOfMemoryError:PermGen space异常的解决办法

Eclipse+Jboss报java.lang.OutOfMemoryError:PermGen space异常的解决办法 在Eclipse的工具栏中找到"小虫子"的调试图标或向右的白色箭头的图片,点击它们其中一个的右侧的向下的黑色箭头,找到 Configurations 选项,然后在右边的选项卡中选择 Arguments 选项卡,在下面的VM arguments 输入框中输入如下一段,其中"D:/develop/jboss-4.0.4.GA"是我的电脑的JOSS的

当tomcat报:java.lang.OutOfMemoryError: Java heap space

问题描述 我要导出一个10W条记录的文件,但是tomcat后台报java.lang.OutOfMemoryError:Javaheapspace异常我用已经用了两种方法不行1./tomcat/bin/catalina.bat加上下面的命令:在remGuessCATALINA_HOMEifnotdefined这句话的下面加setJAVA_OPTS=-Xms32m-Xmx256m2.修改TOMCAT_HOME/bin/catalina.sh在「echo"UsingCATALINA_BASE:$CAT