Issues with OutOfMemoryError: PermGen space in tomcat and solution

Recently when i am deploy webapplication in tomcat 6.0.32 in windows Development environment, i got following 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 startup of your application or in runtime whenever the applicaiton is running.

One of the causes is
As you know, java object are stored in heap memory, when your applicaiton war is deployed and started in tomcat container, the applicaiton loads all the objects and and called the startup call back objects and loaded into the memory. so there is no enough memory to accommadate all the object in memory. it throws heap permgen errors. and the result is applicaiton failed with error and does't start properly. By defalult tomcat assigns 256m as heap memory for the process, so you have to increase the size based on your applicaiton size as well as your RAM capacity

and the other cause is

JVM take care of cleaning the objects automcatically, but some objects holds in memory in long time whenever your applicaiton start/stops or whenever your applicaiton process the data, it holds different version of same class in the memoryl



How this issue can be solved.
We can fix this in many ways
1. set the below value in startup.bat in windows or startup.sh in linxu flavours.

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 linxu flavours.

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 doest 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 avilable in memory and this would be a memory leak at classloader, again after started, classloader doest 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.

No comments:

Post a Comment

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