Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

OutOfMemoryError — memory allocation failure

OutOfMemoryError signals a JVM memory allocation problem. It is a subtype of Error, not Exception, and needs no throws declaration. It does not automatically mean a memory leak or merely a full Java heap.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

new OutOfMemoryError()
new OutOfMemoryError("detail message")

java.lang.OutOfMemoryError extends VirtualMachineError, and through it Error and Throwable. The JVM throws it when it cannot create an object because memory is exhausted and the garbage collector cannot make more available.

As an Error subtype, it is unchecked: no throws declaration is required. A catch (Exception e) block does not catch it. Although an explicit catch is possible, do not treat this as routine handling of invalid input or assume that the application can safely continue.

The OutOfMemoryError() constructor creates an object without a detail message; OutOfMemoryError(String s) stores the supplied message. The JVM may create these objects with suppression disabled or a non-writable stack trace. Do not assume that complete diagnostic information is always available.

For diagnosis, distinguish messages: Java heap space concerns the heap, Metaspace concerns class metadata, and Requested array size exceeds VM limit concerns an array-size limit independent of available heap. Native memory allocation failures are also possible. The error alone does not prove a leak; identify the resource and cause before automatically increasing -Xmx.

The example only constructs a diagnostic object: it neither throws it nor exhausts memory. It shows the class name, message, and inheritance relationship. Documentation scope: Java SE 21; the repository runtime does not attest the production runtime version.

Examples

public class Main {
    public static void main(String[] args) {
        OutOfMemoryError error = new OutOfMemoryError("demo only");
        System.out.println(error.getClass().getSimpleName());
        System.out.println(error.getMessage());
        System.out.println(error instanceof VirtualMachineError);
    }
}

Result

OutOfMemoryError
demo only
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources