Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The OutOfMemoryError class

Reference for java.lang.OutOfMemoryError: heap space, metaspace and native-memory variants, how to diagnose leaks, and which JVM options help.

java.lang.OutOfMemoryError (OOME) is thrown when the JVM cannot allocate an object or other resource because memory is exhausted and the garbage collector cannot free enough. Like StackOverflowError it is an Error, so it is not caught by catch (Exception e).

The detail message tells you which memory area ran out. The most common is Java heap space, but the same class is used for metaspace, native threads, direct buffers and more. There are two root causes: the data you legitimately need does not fit, or something keeps objects reachable that you no longer use (a memory leak).

Java
public class OutOfMemoryError extends VirtualMachineError

Try it

Java Provoking and catching a heap OutOfMemoryError

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java A leak by accident, and a bounded cache instead

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public OutOfMemoryError()
public OutOfMemoryError(String s)
Constructor Description
OutOfMemoryError() No message.
OutOfMemoryError(String s) Detail message, e.g. the memory area.

Common messages the JVM produces:

Message Meaning
Java heap space The heap is full and cannot grow (limit set with -Xmx).
Requested array size exceeds VM limit An array was requested near Integer.MAX_VALUE elements.
GC overhead limit exceeded The Parallel collector spent almost all time collecting while freeing almost nothing.
Metaspace Too many loaded classes (class loader leaks, heavy code generation).
unable to create native thread The operating system refused another thread.
Direct buffer memory Off-heap ByteBuffer.allocateDirect() space is exhausted.

Usage notes

  • Diagnose before tuning. Start the JVM with -XX:+HeapDumpOnOutOfMemoryError and open the dump in a profiler (VisualVM, Eclipse MAT, JDK Mission Control) to see what holds the memory. Raising -Xmx only delays a leak.
  • Usual leak sources: static collections that only grow, caches without a size limit, listeners that are never unregistered, ThreadLocal values in pooled threads, unclosed resources, and class loaders that are never released.
  • Reduce footprint: stream large files instead of loading them, use primitive arrays or specialized collections instead of boxed ones, and prefer Map.of/records with compact fields for many small objects.
  • Catching it is dangerous. The JVM may be in a fragile state, and other threads can fail too. Legitimate uses: a single huge allocation you can reasonably refuse (as in the example), or a last-resort handler that logs and shuts down.
  • Fail fast in production: -XX:+ExitOnOutOfMemoryError or -XX:+CrashOnOutOfMemoryError let a supervisor restart a healthy process instead of keeping a half-broken one.
  • Containers: the JVM sizes the heap from the container limit by default (-XX:MaxRAMPercentage); the process can still be killed by the OS if non-heap memory grows beyond the limit, which is not an OutOfMemoryError.
  • Not the same as the OS killer: an operating-system kill shows as Killed or exit code 137 without any Java exception.