Java : OutOfMemoryError with Examples
OutOfMemoryError (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the OutOfMemoryError methods.
Summary
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.
try {
final var array = new int[1000000000];
} catch (OutOfMemoryError e) {
System.out.println(e);
}
// Result
// ↓
//java.lang.OutOfMemoryError: Java heap space
Constructors
OutOfMemoryError ()
Constructs an OutOfMemoryError with no detail message.
final var e = new OutOfMemoryError();
System.out.println(e); // java.lang.OutOfMemoryError
OutOfMemoryError (String s)
Constructs an OutOfMemoryError with the specified detail message.
final var e = new OutOfMemoryError("abcd");
System.out.println(e); // java.lang.OutOfMemoryError: abcd
System.out.println(e.getMessage()); // abcd
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- Error
- Exception
- RuntimeException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- BufferOverflowException
- BufferUnderflowException
- CancellationException
- ClassCastException
- ConcurrentModificationException
- DateTimeException
- DateTimeParseException
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- ReadOnlyBufferException
- StringIndexOutOfBoundsException
- UnsupportedOperationException
- Throwable