Good Riddance, PermGen OutOfMemoryError !
Tuesday, March 13, 2007 8:44:47 PM
Hi, you're probably here because you are hot-deploying some Java web applications and you have problems with some PermGen: OutOfMemoryErrors. Then you somehow found a link to an article that claimed to solve this very problem.
Well, that article was wrong. So I put up a big red note, but people still link to it.
So, in an attempt to set things straight again, I present a short list of facts that I have collected since I wrote the original article:
The only solution I have found that is feasible, reliable and portable is this: reboot your servers!
P.S. Don't bother giving me links to other articles on this issue. Even if you happen to find something I haven't already read, chances are that it is simply rehashing stuff I already know.
Thanks!
Bellow this line is the original article with all it's faulty conclusions.
-----------------------------------------------------------------------------------------------------------------------
I did it!
Yes, lads & lasses! If you've been annoyed with having JBoss or Tomcat die with an OutOfMemoryError every fifth time you redeploy your beloved brainchild of a web application, then this is your lucky day! Because I found a fix! It's true! Yay! ....!!!!!11one (can you tell this has been a pain to me?)
Boring but Serious Theory and Hypothesis part:
The "PermGen" error happens, when the Java virtual machine runs out of memory in the permanent generation. Recall that Java has a generational garbage collector, with four generations: eden, young, old and permanent.
In the eden generation, objects are very short lived and garbage collection is swift and often.
The young generation consists of objects that survived the eden generation (or was pushed down to young because the eden generation was full at the time of allocation), garbage collection in the young generation is less frequent but still happens at quite regular intervals (provided that your application actually does something and allocates objects every now and then).
The old generation, well, you figured it. It contains objects that survived the young generation, or have been pushed down, and garbage collection is even less infrequent but can still happen.
And finally, the permanent generation. This is for objects that the virtual machine has decided to endorse with eternal life - which is precicely the core of the problem. Objects in the permanent generation are never garbage collected; that is, under normal circumstances when the jvm is started with normal command line parameters.
So what happens when you redeploy your web application is, that your WAR file is unpacked and its class files loaded into the jvm. And here's the thing: almost always ends up in the permanent generation... Because, seriously, who wants to garbage collect their classes?!? Well, apparently application servers do, and here's how we make that happen;
PermGen, The Fix:
The standard garbage collector can't collect in the permanent generation, but the concurrent collector can. So the first thing we need to do is to make sure that the jvm uses the concurrent garbage collector. This is done by putting this:
> -XX:+UseConcMarkSweepGC
In java's command line arguments. But this is not enough. We must also specifically tell it to collect in the permanent generation, and this is done with this command line argument:
> -XX:+CMSPermGenSweepingEnabled
Good, now the concurrent collector will take the permantent generation under its wings. But wait! Classes are special, and the jvm is reluctant to let go of them, so we must also explicitly allow classes to be unloaded:
> -XX:+CMSClassUnloadingEnabled
Now we're certain that the permanent generation will be properly cleaned. But this raises another issue: what if the jvm unloads classes that might still be needed? I imagine it can be hard for a collector to tell whether or not a class might still be needed with the amount of reflection that goes on in an application server. Therefor, we might want to tweak the amount of memory allocated for the permanent generation, and this is done with this command line parameter:
> -XX:MaxPermSize=128m
Which will set the maximum size of our permanent generation to 128 megabytes - tweak it to fit your needs.
With these parameters properly applied to the jvm that runs your application server, your chances of running into a PermGen OutOfMemoryError will be considerably lessened.
Take care!
Well, that article was wrong. So I put up a big red note, but people still link to it.
So, in an attempt to set things straight again, I present a short list of facts that I have collected since I wrote the original article:
- Bugs in the Sun garbage collector is one source of this problem.
- There are (were? can't be arsed to check) more than one bug, hence confusion.
- Your app failing to undeploy completely will leak all classes in your app, every time you redeploy - this is another source of the problem.
- None of these sources can be fixed with GC flags.
- The JRocket JVM does NOT fix this problem - it simply lets the permgen grow unbounded, until something bad happens, like swap-thrashing or the OS kills it.
The only solution I have found that is feasible, reliable and portable is this: reboot your servers!
P.S. Don't bother giving me links to other articles on this issue. Even if you happen to find something I haven't already read, chances are that it is simply rehashing stuff I already know.
Thanks!
Bellow this line is the original article with all it's faulty conclusions.
-----------------------------------------------------------------------------------------------------------------------
I did it!
Yes, lads & lasses! If you've been annoyed with having JBoss or Tomcat die with an OutOfMemoryError every fifth time you redeploy your beloved brainchild of a web application, then this is your lucky day! Because I found a fix! It's true! Yay! ....!!!!!11one (can you tell this has been a pain to me?)
Boring but Serious Theory and Hypothesis part:
The "PermGen" error happens, when the Java virtual machine runs out of memory in the permanent generation. Recall that Java has a generational garbage collector, with four generations: eden, young, old and permanent.
In the eden generation, objects are very short lived and garbage collection is swift and often.
The young generation consists of objects that survived the eden generation (or was pushed down to young because the eden generation was full at the time of allocation), garbage collection in the young generation is less frequent but still happens at quite regular intervals (provided that your application actually does something and allocates objects every now and then).
The old generation, well, you figured it. It contains objects that survived the young generation, or have been pushed down, and garbage collection is even less infrequent but can still happen.
And finally, the permanent generation. This is for objects that the virtual machine has decided to endorse with eternal life - which is precicely the core of the problem. Objects in the permanent generation are never garbage collected; that is, under normal circumstances when the jvm is started with normal command line parameters.
So what happens when you redeploy your web application is, that your WAR file is unpacked and its class files loaded into the jvm. And here's the thing: almost always ends up in the permanent generation... Because, seriously, who wants to garbage collect their classes?!? Well, apparently application servers do, and here's how we make that happen;
PermGen, The Fix:
The standard garbage collector can't collect in the permanent generation, but the concurrent collector can. So the first thing we need to do is to make sure that the jvm uses the concurrent garbage collector. This is done by putting this:
> -XX:+UseConcMarkSweepGC
In java's command line arguments. But this is not enough. We must also specifically tell it to collect in the permanent generation, and this is done with this command line argument:
> -XX:+CMSPermGenSweepingEnabled
Good, now the concurrent collector will take the permantent generation under its wings. But wait! Classes are special, and the jvm is reluctant to let go of them, so we must also explicitly allow classes to be unloaded:
> -XX:+CMSClassUnloadingEnabled
Now we're certain that the permanent generation will be properly cleaned. But this raises another issue: what if the jvm unloads classes that might still be needed? I imagine it can be hard for a collector to tell whether or not a class might still be needed with the amount of reflection that goes on in an application server. Therefor, we might want to tweak the amount of memory allocated for the permanent generation, and this is done with this command line parameter:
> -XX:MaxPermSize=128m
Which will set the maximum size of our permanent generation to 128 megabytes - tweak it to fit your needs.
With these parameters properly applied to the jvm that runs your application server, your chances of running into a PermGen OutOfMemoryError will be considerably lessened.
Take care!


Anonymous # Wednesday, March 14, 2007 5:33:49 AM
Christian Vest Hansenkarmazilla # Wednesday, March 14, 2007 9:30:57 AM
They have deployed it on one of our production servers with, I think, is under medium load. There's two sides to the test: How will these GC settings affect the overall perfomance of the server? And will it now be able to run for more than a week without having to be restarted?
If I remember it, I'll let you know how it turns out. If it fails misurably, I guess I'll learn the hard way
Anonymous # Wednesday, March 14, 2007 10:59:33 AM
Anonymous # Wednesday, March 14, 2007 2:20:01 PM
Christian Vest Hansenkarmazilla # Wednesday, March 14, 2007 5:44:32 PM
We use 1.5.0_06-b05 on the production server that I'm deploying to, and it's a 32-bit machine.
Christian Vest Hansenkarmazilla # Thursday, March 15, 2007 3:36:15 PM
Anonymous # Friday, March 16, 2007 9:32:55 AM
Anonymous # Monday, March 19, 2007 10:06:28 AM
Christian Vest Hansenkarmazilla # Monday, March 19, 2007 2:38:30 PM
Christian Vest Hansenkarmazilla # Thursday, March 22, 2007 2:22:02 PM
Anonymous # Monday, May 14, 2007 9:16:58 AM
Anonymous # Monday, May 21, 2007 7:49:08 AM
Christian Vest Hansenkarmazilla # Tuesday, May 22, 2007 7:25:22 AM
Anonymous # Monday, October 22, 2007 12:55:27 AM
Christian Vest Hansenkarmazilla # Tuesday, November 6, 2007 3:58:26 PM
cd to the eclipse application directory (most likely /Applications/eclipse) and just type:
emacs $(find . -name eclipse.ini)
To edit the memory settings for eclipse without having to worry about where the file actually is.
Anonymous # Tuesday, January 8, 2008 4:38:06 AM
Anonymous # Thursday, January 10, 2008 11:25:36 AM
Anonymous # Monday, August 25, 2008 5:36:13 PM
Anonymous # Wednesday, October 8, 2008 5:25:18 AM
Anonymous # Tuesday, January 20, 2009 7:34:44 AM
Anonymous # Thursday, February 12, 2009 10:19:57 AM
Anonymous # Tuesday, June 9, 2009 10:35:45 AM
Anonymous # Sunday, June 14, 2009 12:13:47 PM
Anonymous # Friday, July 17, 2009 3:38:32 PM
Anonymous # Tuesday, July 28, 2009 9:12:18 AM
Anonymous # Tuesday, September 15, 2009 2:41:11 PM
Anonymous # Thursday, October 29, 2009 1:27:06 PM
Anonymous # Friday, October 15, 2010 9:14:41 PM