Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch OutOfMemoryError in JVM and run a script if it's caught?

I have a program that sometimes throw OOME, I understand that there is a flag in the JVM options that I can set and whenever a certain Error/Exception appears (such as OOME) it calls a script I wrote. The script will give the user a notification and will call a the program with a different argument so it won't get OOME again.

does anyone know how to set this flag? what is the JVM options I need to set? I looked everywhere on line and couldn't find the answer.

help me please! Thanks, Aye

like image 551
Aye Avatar asked Sep 29 '10 11:09

Aye


People also ask

How do you deal with Outofmemory error in Java?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

Can we handle OutOfMemoryError in catch block?

Java has the option to catch exceptions and handle them gracefully. For example, you can catch the FileNotFoundException that can be thrown when trying to work with a file that doesn't exist.

What happens when JVM run out of memory?

OutOfMemoryError is a runtime error in Java which occurs when the Java Virtual Machine (JVM) is unable to allocate an object due to insufficient space in the Java heap. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java. lang.

How do you solve heap memory problems?

There are several ways to eliminate a heap memory issue: Increase the maximum amount of heap available to the VM using the -Xmx VM argument. Use partitioning to distribute the data over additional machines. Overflow or expire the region data to reduce the heap memory footprint of the regions.


1 Answers

-XX:OnOutOfMemoryError="cmd args;cmd args"

From: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#DebuggingOptions

like image 193
TuomasR Avatar answered Sep 29 '22 13:09

TuomasR