Can I auto restart tomcat jvm on out of memory exception

not easily, and definitely not through the JVM that just suffered the out of memory exception. Your best bet would be some combination of tomcat status monitor coupled with cron scripts or related scheduled system administrator scripts; something to check the status of the server and automatically stop and restart the service if it has failed.


You can try to use the OnOutOfMemoryError JVM option

-XX:OnOutOfMemoryError="/yourscripts/tomcat-restart"

It is also possible to generate the heap dump for later analysis:

-XX:+HeapDumpOnOutOfMemoryError

Be careful with combining these two options. If you force killing the process in "tomcat-restart" the heap dump might not be complete.


I know this isn't what you asked, but have you tried looking through a heap dump to see where you may be leaking memory?

Some very useful tools for tracking down memory leaks:

jdk/bin/jmap -histo:live pid

This will give you a histogram of all live objects currently in the JVM. Look for any odd object counts. You'll have to know your application pretty well to be able to determine what object counts are odd.

jdk/bin/jmap -dump:live,file=heap.hprof pid

This will dump the entire heap of the JVM identified by pid. You can then use the great Eclipse Memory Analyzer to inspect it and find out who is holding on to references of your objects. Your two biggest friends in Eclipse Memory Analyzer are the histo gram and a right click -> references -> exclude weak/soft references to see what is referencing your object.

jconsole is of course another good tool.

Tags:

Java

Tomcat

Jvm