Good Riddance, PermGen OutOfMemoryError !
Tuesday, 13. March 2007, 20:44:47
<edit>
Before thinking "yay! a fix for my problem!", read this: http://my.opera.com/karmazilla/blog/2007/03/15/permgen-strikes-back
The rest of this article has been left untouched.
</edit>
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:
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:
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:
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:
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!
Before thinking "yay! a fix for my problem!", read this: http://my.opera.com/karmazilla/blog/2007/03/15/permgen-strikes-back
The rest of this article has been left untouched.
</edit>
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!
Will try this out. Have been plagued with OutOfMemory errors on development boxes recently with 5-15 different applications deployed. And almost always occurs on a new deploy.
If it works ... thanks in advance.
By anonymous user, # 14. March 2007, 05:33:49
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
By karmazilla, # 14. March 2007, 09:30:57
I was haunted by PermGen OutOfMemory for years, but have never been able to solve it. I surfed the whole web to find the solution, but never got one.
It is strange that there is no mention of this trick on sun's site!?
By anonymous user, # 14. March 2007, 10:59:33
"The standard garbage collector can't collect in the permanent generation"
I don't know every jvm, but the 1.5.0_10 (and 11) 64-bit Sun (server) JVM _does_ class unloading with the default parallel garbage collector.
By anonymous user, # 14. March 2007, 14:20:01
We use 1.5.0_06-b05 on the production server that I'm deploying to, and it's a 32-bit machine.
By karmazilla, # 14. March 2007, 17:44:32
By karmazilla, # 15. March 2007, 15:36:15
Hi,
there is alternative solution - use another JVM (BEA or IBM).
Regards,
Oleksandr Alesinskyy
By anonymous user, # 16. March 2007, 09:32:55
I'd recommend to upgrade to the latest JVM. At release 10 there were heavy bugfixes of garbage collecting subsystem.
By anonymous user, # 19. March 2007, 10:06:28
By karmazilla, # 19. March 2007, 14:38:30
By karmazilla, # 22. March 2007, 14:22:02
incredible! much respect
By anonymous user, # 14. May 2007, 09:16:58
Would have been to good to be true...
Thanks for your effort and article, but it didn't work for me here. Adding the JVM OPTS described to my Tomcat 5.5.20 startup script only now lead to the fact, that I can't undeploy my application because the ANT Task Undeploy is not able to remove log4j.properties from WEB-INF/classes and so another redeploy is not possible as the application still exists under webapps/myapp.
The memory was not released when the undeploy occured. (which might stem from the fact that log4j.properties was not removed)
Anyway thanks for this contribution. Hope that I will find the culprit.
By anonymous user, # 21. May 2007, 07:49:08
By karmazilla, # 22. May 2007, 07:25:22
I modified my eclipse.ini file as per your recommendation and it has not crashed for two days straight (compared to every 15 minutes). Keep up the good work. (Below are my eclipse.ini settings)
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize 256m
-vmargs
-XX:MaxPermSize=256m
-XX:+UseConcMarkSweepGC
-XX:+CMSPermGenSweepingEnabled
-XX:+CMSClassUnloadingEnabled
-Xms40m
-Xmx256m
By anonymous user, # 22. October 2007, 00:55:27
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.
By karmazilla, # 6. November 2007, 15:58:26
Don't mean to start (yet) another IDE war ... My NetBeans IDE never experiences those out of memory errors a couple of you are talking about here. And, the most recent version of NetBeans IDE 6.0 is very much improved. I find it much better than Eclipse.
By anonymous user, # 8. January 2008, 04:38:06
On CentOS4.6, JDK 1.5.0_12_64bit and Tomcat 5.5.23 with a Spring/Hibernate/CGlib application, the workaround doesn't help.
15 restarts of the application, and the PermGen is full.
By anonymous user, # 10. January 2008, 11:25:36