1

In my project i get error like below :

SEVERE: Servlet.service() for servlet [dispatcher] in context with path       [/ExamSy                                                                                       stem] threw exception [javax.servlet.ServletException: java.lang.OutOfMemoryErro                                                                      r: PermGen space] with root cause
java.lang.OutOfMemoryError: PermGen space
May 06, 2014 2:44:18 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /home/anything/public_html/ExamSystemold       .war
May 06, 2014 2:47:08 AM org.apache.tomcat.util.digester.Digester startElement
SEVERE: Begin event threw error
java.lang.OutOfMemoryError: PermGen spaceException in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]"  jav                                                                                            a.lang.OutOfMemoryError: PermGen space
Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]" jav                                                                                            a.lang.OutOfMemoryError: PermGen space
user3588852
  • 19
  • 1
  • 1
  • 6

3 Answers3

12

Once you come across the PermGen Space issue, you will need to find out if the issue is due to large number of classes your application is loading or due to memory leak. If it is due to large number of classes, you can fine tune to increase the PermGen Space allocated and that will resolve the issue.

The 1st reason could be your application or your server has too many classes and the existing PermGen Space is not able to accommodate all the classes. **-XX:MaxPermSize=XXXM**

If the issue is due to insufficient PermGen Space due to large number of classes, then you can increase the PermGen space by adding the –XX:MaxPermSize=XXm parameter. This will increase the space available for storing the classes and should -XX:MaxPermSize=256m -XX:+CMSClassUnloadingEnabled

This parameter indicates whether class unloading enabled when using CMS GC. By default this is set to false and so to enable this you need explicitly set the following option in java options.

-XX:+CMSClassUnloadingEnabled

If you enable CMSClassUnloadingEnabled the GC will sweep PermGen, too, and remove classes which are no longer used.This option will work only when UseConcMarkSweepGC is also enabled using the below option.

-XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled

This parameter indicates whether sweeping of perm gen is enabled. By default this parameter is disabled and so will need to explicitly set this for fine tuning the PermGen issues. This option is removed in Java 6 and so you will need to use -XX:+CMSClassUnloadingEnabled if you are using Java 6 or above. So the options added to resolve the PermGen Space memory issues will look like

-XX:MaxPermSize=128m -XX:+UseConcMarkSweepGC XX:+CMSClassUnloadingEnabled Memory leaks

And the 2nd reason could be memory leak. How the class definitions that are loaded could can become unused.

Normally in Java, classes are forever. So once the classes are loaded, they stay in memory even if that application is stopped on the server. Dynamic class generation libraries like cglib use lot of PermGen Space since they create a lot of classes dynamically. Heavy use of Proxy classes, which are created synthetically during runtime. It’s easy to create new Proxy classes when a single class definition could be reused for multiple instances.

Spring and Hibernate often makes proxies of certain classes. Such proxy classes are loaded by a classloader. The generated class definitions are never discarded causing the permanent heap space to fill up fast.

For PermGen space issues, you will need identify the cause of leak and fix it. Increasing the PermGen space will not help, it will only delay the issue, since at some point the PermGen space will still be filled up.

You can go through this link: http://wiki.apache.org/tomcat/MemoryLeakProtection

Thanks.

MrYo
  • 1,797
  • 3
  • 19
  • 33
0

When you have PermGen space OutOfMemoryError you have to check the value XX:MaxPermSize=XXXM.

The permanent space is where the classes, methods, internalized strings, and similar objects used by the VM are stored and never deallocated.

  1. Generally for web applications due to jsp dynamic class generations and hot redeployment, memory uses is going to increase with multiple copies of same class.
  2. Max Heap Size (-Xmx) and MaxPermSize should be set considering how much memory is required for application classes and instances, total memory of server or machine and memory required for application. There should be safe space between Max Heap Size (-Xmx) and MaxPermSize for application execution.
  3. Check memory leaks

    Starting with Java 8, both the permgen space and this setting are gone. User won't have this error.

References:

Community
  • 1
  • 1
gyanu
  • 945
  • 1
  • 8
  • 20
-2

Set the -Xms1024M -Xmx4096M -XX:MaxPermSize=4096M in your tomcat -> launch configuration -> Arguments and you're good to go. The numbers mentioned are just example, you can use that best suits your need.

sMajeed
  • 323
  • 4
  • 10