3 Things you can do when OutOfMemory happens

In this brief article we will outline three JVM configuration parameters you can add to the JVM to handle a scenario when your application issues an OutOfMemoryError.

The OutOfMemoryError: Java heap space error means thatyour JVM heap size (or the default size, if you rely on defaults) is insufficient for the application. In this article we will not discuss how to troubleshoot a possible memory leak in your application; rather we will learn which JVM options you can include at start up to be reactive when this error happens.

Exiting the application when you hit an OutOfMemory

When there is insufficient space to allocate an object in the Java heap an OutOfMemory Error is thrown. However, the Java process remains active. This might not be desirable in several cases therefore you can configure the JVM to terminate when the garbage collector cannot make space available to accommodate a new object,

Since Java 1.8.92 you can add the following option to force the termination of the JVM:

-XX:+ExitOnOutOfMemoryError

If you are running an earlier JVM version, the option to include is the following one:

-XX:OnOutOfMemoryError

Finally, don’t confuse the -XX:OnOutOfMemoryError option with the -XX:OnError option. The OnError option allows to execute a script/command when a fatal error occurs. For example, something is wrong with the JIT, therefore you have attempted to execute invalid code.

Example:

-XX:OnError="pmap %p"

Running a command when you hit the Error

On the other hand, if you prefer to handle the JVM termination with a command or a script file, you can pass the command as an argument to the -XX:OnOutOfMemoryError. For example, the following command will issue the ‘kill -9’ command to terminate the ProcessId (%p):

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

If you prefer, instead of using a command, you pass as argument a script file:

-XX:OnOutOfMemoryError="/path/quitJava.sh"

Taking an Heap Dump on Error

Next, we would like to account for the option -XX:+HeapDumpOnOutOfMemoryError. This flag tells the JVM to generate a heap dump when a memory allocation cannot be satisfied. There is no overhead in running with this option, it’s fine (or say recommended) to include it also in production systems:

 -XX:+HeapDumpOnOutOfMemoryError

To specify a different Path for your Head Dump, you can use the following argument:

-xx:HeapDumpPath=path

Finally, you can also set the above options programmatically with JVM API. See the following example:

String pid = ManagementFactory.getRuntimeMXBean().getName();
pid = pid.substring(0, pid.indexOf('@'));
String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String fileName = "/tmp/heap_" + pid + "_" + date + ".dump";

HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(
    ManagementFactory.getPlatformMBeanServer(),
    "com.sun.management:type=HotSpotDiagnostic",
    HotSpotDiagnosticMXBean.class);
bean.setVMOption("HeapDumpOnOutOfMemoryError", "true");
bean.setVMOption("HeapDumpPath", fileName)
Found the article helpful? if so please follow us on Socials