java.lang.OutOfMemoryError: PermGen space error during deployment to Glassfish

Sometimes I see this error popping up when I try to deploy applications. Some big projects with a lot of dependencies will hit the server limitation almost on first or second deploy. This happens more often with development or integration servers because Agile teams may deploy to those servers for testing purposes very often.
If you deploy 10 times a day to your production servers, like Etsy or Facebook does (they not on jvm by the way), then you want to make sure you don’t see this error during production deployment.
The question is – what does this error mean, what is PermGen space, how to change it and how to tune it. JVM has three memory spaces – young generation, old generation and permanent generation.

Young generation space is where java allocates newly created object, then , after some period of time and surviving garbage collections, object are moved into old generation memory. Permanent generation space holds VM itself and Java class metadata as well as interned String and class static variables. If Permanent Generation size is not specified, java automatically chooses values based on some system configuration it discover. Usually this is relatevly small number. When you experience this error first step is to increase –XX:PermSize and –XX:MaxPermSize parameters. You can start with about 20% of total memory allocated to JVM. Other 80% should be configured for heap space using -Xms and -Xms parameters.
It is very easy to modify those parameters in Glassfish using asadmin command line administrative utility or by writing small script.
To list current JVM option values:

asadmin list-jvm-options


To update to new values:
#update jvm option
#mke sure domain is running
asadmin --host localhost  start-domain domain1

asadmin --host localhost delete-jvm-options "-XX\:MaxPermSize=192m"
asadmin --host localhost create-jvm-options "-XX\:MaxPermSize=512m"
asadmin --host localhost create-jvm-options "-XX\:PermSize=512m"
asadmin --host localhost delete-jvm-options -Xmx512m
asadmin --host localhost create-jvm-options -Xmx1024m

#restart domain
asadmin --host localhost stop-domain domain1
asadmin --host localhost start-domain domain1

Note escaping backslash before column.(/:) This script should be included into your configuration management tools, like Chief or Puppet, so you know how servers are configured and how to create a new ones.
This should allow you to deploy/redeploy your applications.

If you still having problems, for example have to restart server before deployment, or can’t restart servers because can’t afford loosing session in statefull apps, or servers have to be online I would recommend checking out new product called LiveRebel.

I successfully use the sister product, JRebel in my everyday development work on Glassfish. According to product web site: “No downtime. No lost session. No OutOfMemoryErrors. Fully automated. Instant.”
I am planning to test this application and report back on my blog. But if you already have experience with the product, please share with community.
If you really want to learn all nuts and bolts of JVM tuning and optimization, then this new book is for you:

Interesting Article on the topic of Java memory: From Java code to Java heap

I hope this short blog will help you with this annoying but easy to fix error.

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>