Developing with IntelliJ and Grails sometimes leads to memory problems, particularly considering the PermGen space – even with plenty of RAM. This issue occures especially after re-deploying several times while debugging.
This is more a flaw with Tomcat itself, rather then Grails or any framework components – though Grail’s ”convention over coding” guide line heads to the problem by using reflection to help enforce. And reflection does use perm gen to do it’s work.
If you are using JBoss I’d recommend Dror Bereznitsky’s blog post, he discusses this topic in depth.
This is how the exception looks like:
java.lang.OutOfMemoryError: PermGen space Error automatically restarting container: java.lang.OutOfMemoryError: PermGen space 2010-08-03 14:53:38,208 [http-8080-52] ERROR [/SampleTemplateApp].[grails] - Servlet.service() for servlet grails threw exception java.lang.OutOfMemoryError: PermGen space 2010-08-03 14:53:38,211 [http-8080-52] ERROR [/SampleTemplateApp].[default] - Servlet.service() for servlet default threw exception java.lang.OutOfMemoryError: PermGen space 2010-08-03 14:53:38,213 [http-8080-52] ERROR [Tomcat].[localhost] - Exception Processing ErrorPage[errorCode=500, location=/grails-err orhandler]
Now you might find a lot of blogs and posts where one recommend to configure the JVM options to provide more RAM for the Perm itself.
But this will only delay the problem, not vanish. The point is, that Tomcat does not clean up the PermGen (Permanent Generation) and so it grows and grows with every hot re-deployment.
To get rid of this annoying circumstance you might want to enable both the unloading of the PermGen and its sweeping. This can be done as follow:
For IntelliJ users:
There are two options to configure IntelliJ to use this VM-args:
You can set those parameters in the debug/run configuration for each individual project as shown in the screenshot (debug configuration).
Update the idea.vmoptions located in /idea-IU-xxx/bin/.
The latter approach has the advantage that it automatically affects all existing grails projects, and certainly all prospective as well.
Unfortunately I’m still facing the memory issues in developing mode, even with project-wide configured PermGen arguments. I guess I have to try different approaches..
Currently I’m checking if this might help anything: http://docs.oracle.com/cd/E13209_01/wlcp/wlss30/configwlss/jvmgc.html
-server -XX:MaxPermSize=256m -XX:+UseParNewGC -XX:MaxNewSize=256m -XX:NewSize=256m -Xms768m -Xmx1024m -XX:SurvivorRatio=128 -XX:MaxTenuringThreshold=0 -XX:+UseTLAB -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
After a couple of weeks I recognized that the solution mentioned above works like a charm in my case. I can just recommend these settings.