Troubleshooting Java Virtual Machine memory errors

A Java Virtual Machine (JVM) is a small, self-contained memory space. Like any machine, it is subject to problems caused by insufficient resources. Unlike a normal server, a JVM cannot recover from memory outages. If a JVM runs out of resources, it often enters an unpredictable state that can be resolved only by restarting the JVM.

The best way to avoid this situation is to configure JVM settings to minimize the likelihood of running out of resources. This configuration includes allocating resources to the JVM, ensuring that the physical resources as allocated are available on the physical computer, and restricting use cases to handle appropriate resource load.

This topic provides potential solutions for the following common memory errors for JVMs:

The following BMC Communities video (7:13) demonstrates how to take thread dumps and heap dumps in BMC Atrium Orchestrator for troubleshooting performance and memory issues.

java.lang.OutOfMemoryError: Java heap space

A heap space error occurs when the JVM has exhausted its disk space allocation. Common causes for this error include:

  • Memory leaks — The JVM consumes memory to perform some action and does not release it when the action is completed.
  • High data usage — The JVM is asked to perform operations on more data than it can handle. For example, a JVM with 2 gigabytes of internal disk space cannot read and process a 3-gigabyte file.

Increasing the heap size

You can avoid this error at least temporarily by increasing the heap size used by the JVM. To increase the heap size, use the instructions for increasing memory in Configure memory settings.

If the JVM or application running in it has a memory leak, the increased heap size will eventually be exhausted and the JVM will fail again, so the long-term solution is to diagnose and remove the root cause of the error.

Diagnosing the root cause

You can use the following methods to diagnose the root cause of the java.lang.OutOfMemoryError: Java heap space error.

Configure the JVM to generate a heap dump when it receives this error.

This method is considered best when other causes have been ruled out and a memory leak defect is suspected.

To configure the JVM to generate a heap dump, add the -XX:+HeapDumpOnOutOfMemoryError option to the Java options and restart the JVM.

When the heap space error occurs, the JVM creates a file the size of the configured maximum heap size. A Java developer can examine this file to identify a root cause.

Enable logging to track memory usage via a garbage collection log.

This method is considered best when high data-usage operations are suspected.

To enable logging, add the -Xloggc:gc.log option to your Java options and restart the JVM.

The JVM creates the gc.log file to track memory usage and release, which is commonly called garbage collection. You can use third-party Java utilities to view the log and evaluate memory consumption over time.

java.lang.OutOfMemoryError: unable to create new native thread

This error states that all available thread space has been claimed, so new thread creation will fail. Common causes for this error include:

  • Threading problems — The JVM is not properly releasing threads when it is finished with them, leading to thread exhaustion.
  • High usage — Because a thread limit is a limit on the number of simultaneous operations that a JVM can perform, an improperly configured JVM can have too few threads for its operations.

To avoid this error, increase the available thread space by reducing the thread stack size with the -Xss Java option. Allowing smaller threads effectively increases the number of possible concurrent threads. By default, this value is 1,024k for 64-bit JVMs and 320k for 32-bit JVMs. A good start is to change the stack size to 128k with the -Xss128k option.