Backend Development 9 min read

Understanding Java OutOfMemoryError: Causes, Types, and Fixes

This article explains the various forms of Java OutOfMemoryError, their underlying causes such as heap exhaustion, GC overhead, metaspace limits, and native allocation failures, and provides practical solutions and code snippets to diagnose and resolve each scenario.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Understanding Java OutOfMemoryError: Causes, Types, and Fixes

1. Introduction

A common sign of memory leak is the java.lang.OutOfMemoryError exception, which occurs when the Java heap cannot allocate new objects, when native memory is insufficient, or when the garbage collector takes too long.

When this exception is thrown, a stack trace is printed. Native allocation failures (e.g., insufficient swap) also raise this error.

Diagnosis

First determine whether the heap or native memory is exhausted; the error message often includes details.

2. OOM Summary

2.1 OOM: Java heap space

Cause: The heap cannot allocate objects. This may be due to insufficient configured heap size or unintentional object retention (memory leak), including excessive use of finalizers.

Solution: Increase heap size and optionally generate a heap dump:

<code>// Enable heap dump on OOM
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/jvm/dump/xxx.hprof
// Increase heap memory
-Xms4g -Xmx4g</code>

Analyze the dump with MAT.

2.2 OOM: GC Overhead Limit Exceeded

Cause: GC runs continuously, using >98% of CPU time and reclaiming <2% of heap, indicating the heap is almost full.

Solution: Increase heap size or disable the limit with -XX:-UseGCOverheadLimit .

2.3 OOM: Requested array size exceeds VM limit

Cause: Attempt to allocate an array larger than the VM’s maximum allowed size.

Solution: Allocate a smaller array.

<code>for (int i = 3; i >= 0; i--) {
    try {
        System.out.printf("Requested array length: %d%n", (Integer.MAX_VALUE - i));
        int[] temp = new int[Integer.MAX_VALUE - i];
    } catch (Throwable t) {
        t.printStackTrace();
    }
}</code>

First two iterations trigger Java heap space OOM because the array requires >8 GB.

2.4 OOM: Metaspace

Cause: Native memory for class metadata (Metaspace) is exhausted; limited by MaxMetaSpaceSize .

Solution: Increase MaxMetaSpaceSize or reduce heap size to free native memory.

2.5 OOM: request size bytes for reason. Out of swap space?

Cause: Native heap allocation fails, often due to exhausted swap space.

Solution: Use OS‑level troubleshooting tools and examine fatal error logs.

2.6 OOM: Compressed class space

Cause: On 64‑bit JVM, class metadata pointers are compressed; the space is limited by CompressedClassSpaceSize . Exceeding it throws OOM.

Solution: Increase CompressedClassSpaceSize or disable UseCompressedClassPointers .

2.7 OOM: reason stack_trace_with_native_method

Cause: Native method or JNI allocation failed.

Solution: Diagnose with native tools such as DTrace.

JavaJVMGarbage Collectionmemory leakOutOfMemoryErrorHeap Dump
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.