Sunday, January 15, 2012

Issues with OutOfMemoryError: PermGen space in tomcat and solution

OutOfMemoryError: PermGen space in tomcat
Recently when I have deployed a web application in tomcat 6.0.32 in windows Development environment, I got following PermGen Space errors.
so I would like to write the post on this with root cause and exceptions


Caused by: java.lang.OutOfMemoryError: PermGen space
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

What actually causing OutOfMemoryError?

This issue either occur in the startup of your application or in runtime whenever the application is running.
One of the causes is
As you know, java object is stored in heap memory, when your application war is deployed and started in tomcat container, the application loads all the objects and called the startup to call back objects and loaded into the memory. so there is no enough memory to accommodate all the object in memory. it throws heap permgen errors. and the result is application failed with an error and doesn't start properly. By default, tomcat assigns 256m as heap memory for the process, so you have to increase the size based on your application size as well as your RAM capacity
and the other cause is
JVM take care of cleaning the objects automatically, but some objects hold in memory in a long time whenever your application start/stops or whenever your application process the data, it holds a different version of the same class in the memory

Fixing OutOfMemoryError: PermGen space in tomcat

We can fix this in many ways
1. set the below value in startup.bat in windows or startup.sh in Linux flavors.

set JAVA_OPTS=" -Xms1563m -Xmx1563m -XX:NewSize=1024m -XX:MaxNewSize=1024m -XX:PermSize=1024m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled"

2. and other way is to set the below value in startup.bat in windows or startup.sh in Linux flavors.
set CATALINA_OPTS=" -Xms1563m -Xmx1563m -XX:NewSize=1024m -XX:MaxNewSize=1024m -XX:PermSize=1024m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled"

3. Most of the issues related to permgen issues are with SUN JVM implementations so you can change the JVM to JRocket JVM for avoiding this types of errors.


Most of the times permgen issues are solved with the above approaches.

Still, the above approach is not solved, yes, the above settings does not helped me to solve the exception, In that case,

you need to see the logs.in the logs, I am seeing the below message.

SEVERE: A web application registered the JBDC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

Yes, this is the culprit for this issue. my app is not unregistering properly after stopped the server. so the references are not cleaned and available in memory and this would be a memory leak at classloader, again after started, classloader does not load correctly and references are there in memory. this will be causing the issue.

and the solution for this is to copy the corresponding oracle jar to tomcat\lib folder to load from the tomcat classloader. hope this will solves issues



Please let me know if you are still facing the issue.


EmoticonEmoticon

Note: Only a member of this blog may post a comment.