Issues with OutOfMemoryError: PermGen space in tomcat and solution

Recently when I have deployed a web application in tomcat 6.0.32 in a windows Development environment, I got the following PermGen Space errors.

so I would like to write the post on this with the 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 causing OutOfMemoryError?

This issue either occurs in the startup of your application or runtime whenever the application is running.

One of the causes is

As you know, a java object is stored in heap memory, when your application war is deployed and started in a tomcat container, the application loads all the objects and called the startup to call back objects and load them into the memory. so there is not enough memory to accommodate all the objects 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 for 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.

  • 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"
  • 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"
  • Most of the issues related to permgen issues are with SUN JVM implementations so you can change the JVM to JRockit JVM for avoiding these types of errors.

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

Still, the above approach is not solved, yes, the above settings does not help 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 stopping the server. so the references are not cleaned and available in memory and this would be a memory leak at the classloader, again after starting, the 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 the tomcat\lib folder to load from the Tomcat class loader. hope this will solve issues

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