1

Few days ago, I noticed that I'm occasionally seeing a java.lang.OutOfMemory: PermGen exception when my Java web site runs under Tomcat.

I read about this error online and understand why this exception occurs. I followed a tutorial to increase my MaxPermSize, and edited by catalina.sh to contain the following:

    JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 
    -server -Xms1536m -Xmx1536m
    -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
    -XX:MaxPermSize=256m -XX:+DisableExplicitGC"

However, I would like to understand more about my PermGen capabilities:

  1. Can anyone tells me how I can understand my server capability in order to give the appropriate new size?

  2. Moreover does anyone know how I can make memory measurements on Apache tomcat?

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
Nick Robertson
  • 1,047
  • 4
  • 18
  • 41
  • Related question: http://stackoverflow.com/questions/3521251/tomcat-on-production-server-permgen-and-redeploys – Stephen C Jul 02 '12 at 15:23

6 Answers6

1

This is a common problem, and there is no good solution.

The Permgen problems are typically caused by a storage leakage when you do a hot redeploy into a running Tomcat instance. It is typically caused when something retains a reference to some object that is an instance of one of the classes that you redeployed. This causes the old classloader and all of the classes that it loaded to leak ...

There are three ways to address this:

  • Track down and fix the Permgen memory leak.
  • Regularly restart your Tomcat.
  • Don't do hot redeploys.

Increasing the Permgen doesn't fix the problem. It just puts it off ... so that your Tomcat doesn't die as often.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1 @Stephen C It is worth noting that the OP may have an application with thousands of classes in which an initial start may fail due to Permagen issues and not as a product of a class loader leak. In that case increasing PermaGen will resolve the issue at hand but not if there are leaks and hot re deploys are being done. – John Vint Jul 02 '12 at 15:36
1

You could try out JConsole to monitor memory usage:

http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

Tom
  • 4,096
  • 2
  • 24
  • 38
1

The PermGen is the space of the memory not managed by the Garbage Collector. It contains loaded classes, their methods, statics, strings, etc...

With a Xmx at 1536Mo, you can try to put the MaxPermSize to 384M. If the value is to high, the JVM won't start, so it's easy to find the maximum size.

But the real question is why is your PermGen so full. Do you load a lot of classes ?

Jean-Philippe Briend
  • 3,455
  • 29
  • 41
  • Yes i load a lot of classes.Its big project and i'm not experienced java developer so i treated wrong – Nick Robertson Jul 02 '12 at 15:35
  • Ok, it explains your problem. So try to put more Xmx and more MaxPermGenSize. Also make sure to disable the hot reloading in Tomcat for non-development servers. – Jean-Philippe Briend Jul 02 '12 at 15:43
  • Maybe, aso add -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled – Jean-Philippe Briend Jul 02 '12 at 16:02
  • What do you mean when you say "disable the hot reloading in Tomcat for non-development servers"? – Nick Robertson Jul 02 '12 at 16:03
  • Hot deployment (redeploying the webapp when putting a war file in webapps folder) is known to have problems after a lot of redeploying. The good process is to disable this and deploy the application by stopping the server, putting the war file and restarting the Tomcat server. You can prevent Tomcat from redeploying your webapp with this parameter in you web.xml : autoDeploy="false" – Jean-Philippe Briend Jul 02 '12 at 16:07
0

Running out of memory happens because your JVM doesnt have enough memory allocated to it.

To try to get a better look at whats going on, it probably makes sense to try a profiler. I have used the lambda probe and New Relic (which right now is giving a free shirt for trying) before, but there are many others.

John Kane
  • 4,383
  • 1
  • 24
  • 42
0

For tracking the Garbage Collector performance and watch the Heap sizes I would recommend turn on GC logging.

-Xloggc: -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps

this will generate the log file and you will be able to get which settings are more appropriate for sizing the heap.

Alexey A.
  • 1,389
  • 1
  • 11
  • 20
0

1) PermGen stores metadata like class definitions etc. So its size is directly related to size of your application. Mostly number of classes and size of classes. To adjust and monitor size of permgen you could use visual vm that is distributed with JDK. It is in the bin directory and very capable application. 2) You could use Visual VM for monitoring every memory area of JVM. You could monitor even in individual class level.

Deniz
  • 1,575
  • 1
  • 16
  • 27