Archive

Posts Tagged ‘jvm’

Of Tomcat, OutOfMemory Errors & Continuous Development

March 14, 2013 Leave a comment

So you are working in your favorite IDE on some sort of web application or web service, and you are continuously deploying to Tomcat but ocassionally the deployment fails, you see the CPU of the JVM process spike to 100% and you see something like java.lang.OutOfMemoryError: PermGen space show you in your catalina.out file.

The root cause is arguably either a memory leak in Tomcat or something that your application is not cleaning up when its being undeployed/shut down.

But never mind that, you just are annoyed by having to find the process ID of the Tomcat JVM and kill it so you can restart Tomcat.

Instead of doing all that by hand, you can have the JVM kill itself when it encounters this condition, with the use of the “-XX:OnOutOfMemoryError” JVM option. It is as simple as starting up the JVM with the option

-XX:OnOutOfMemoryError="kill -9 %p"

Now, I use NetBeans, and it’s actually not that trivial to try to specify this option correctly in the NetBeans tomcat config, so I punted to just tweaking the catalina.sh script, here’s what I did.

First, I made a backup of catalina.sh

cp catalina.sh catalina.sh-orig

Next, I added the following lines just after the initial comment block in the file.

OOM_OPTS="-XX:OnOutOfMemoryError=kill -9 %p"
export OOM_OPTS

Finally, I found every location where the JVM was being invoked and added “$OOM_OPTS” to the argument list. Its actually pretty easy to do with a sed command

sed -e 's/\($JAVA_OPTS $CATALINA_OPTS\)/\1 "$OOM_OPTS"/g' catalina.sh

And voila. Next time you restart Tomcat, when it encounters an OutOfMemory condition it will kill itself automatically.

Advertisements
Categories: Computing Tags: , ,