Exit and Heap Dump on OutOfMemoryError

Publicado às 19/11/2022 17:00 • #java #tutorial

Este conteúdo não está disponível na sua linguagem... Então se você não entende a linguagem... bem, você pelo ou menos pode apreciar as imagens da postagem, né?

When your Java app crashes due to OutOfMemoryError, you probably want to automatically restart your application. After all, what's the point of keeping the application running if a OutOfMemoryError was thrown, since in that case you probably have a memory leak, so trying to catch the error and continue executing the app is unviable.

To do this, you can add -XX:+ExitOnOutOfMemoryError to your startup flags, this will cause the JVM to automatically exit when a OutOfMemoryError is thrown, sweet!

However, you may also want to create a heap dump when a OutOfMemoryError is thrown, to analyze the heap dump later to figure out what triggered the error. In that case, use -XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError, this will create a heap dump on OutOfMemoryError and then exit.

You can change the heap dump folder with -XX:HeapDumpPath, so, if you want to store your heap dumps in the /tmp/dump folder, use -XX:HeapDumpPath=/tmp/dump!

Here's how you would use it when starting your application.

java -Xmx1G -Xms1G -XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/dump -jar ...