Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a JVM to dump heap when OutOfMemoryError is thrown

I am trying to set the JVM of the server I am working on, so it dumps a heap to file when an OOME occurs.

I know I have to add this option -XX:-HeapDumpOnOutOfMemoryError to the JVM arguments somewhere, but I can't figure how to do this.

FYI, I can access the server through PuTTY, so I am looking for a command line way of doing this.

The JVM I am using is OpenJDK64-Bit Server VM.

I don't know if that's relevant, but the application is a war file.

PS : ps -ef|grep java

tomcat   23837     1  0 Mar25 ?        00:03:46 /usr/lib/jvm/jre/bin/java -classpath :/usr/share/tomcat6/bin/bootstrap.jar:/usr/share/tomcat6/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat6 -Dcatalina.home=/usr/share/tomcat6 -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat6/temp -Djava.util.logging.config.file=/usr/share/tomcat6/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start

EDIT :

I found something, correct me if I'm wrong : since I am using Tomcat, I decided to add these lines in the tomcat.conf file:

JAVA_OPTS=-XX:-HeapDumpOnOutOfMemoryError

JAVA_OPTS=-XX:HeapDumpPath=/root/dump

JAVA_OPTS=-Xmx20m

What do you think ?

like image 844
l0r3nz4cc10 Avatar asked Apr 05 '11 15:04

l0r3nz4cc10


People also ask

How do you get heap dump on OutOfMemoryError?

In order to capture a heap dump automatically, we need to add the HeapDumpOnOutOfMemoryError command-line option that generates a heap dump when a java. lang. OutOfMemoryError is thrown.

How do I manually take a heap dump?

Start the administrative console. In the navigation pane, click Troubleshooting > Java dumps and cores. Select the server_name for which you want to generate the heap dump. Click Heap dump to generate the heap dump for your specified server.

What is JVM heap dump?

A heap dump is a snapshot of all the objects in the Java Virtual Machine (JVM) heap at a certain point in time. The JVM software allocates memory for objects from the heap for all class instances and arrays.


2 Answers

As mentioned by @CoolBeans, the JVM options to use are:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<path to dump file>

For setting this in tomcat, create a file named setenv.sh (setenv.bat for windows) under TOMCAT_HOME/bin directory & add the following line

export CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<path to dump file>"

CATALINA_OPTS is preferred for these kind of options as they need not be applied to the shutdown process.

like image 74
Karthikeyan Marudhachalam Avatar answered Oct 26 '22 00:10

Karthikeyan Marudhachalam


This option from the HotSpot VM options. I would think it'd be the same in the OpenJDK VM but let me know if it's not.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<path to dump file>

You can also manually generate a memory map using jmap if you know the process id:

jmap -J-d64 -dump:format=b,file=<path to dump file> <jvm pid>

You can use JHat to analyze the dump.

jhat <path to dump file>

like image 27
CoolBeans Avatar answered Oct 26 '22 01:10

CoolBeans