Common Java OutOfMemoryError Causes and Their Solutions
This article enumerates the most frequent Java OutOfMemoryError scenarios—including heap space exhaustion, GC overhead limits, oversized array allocations, PermGen/Metaspace depletion, thread creation failures, and native method errors—detailing their causes and practical JVM flag or code fixes to resolve them.
1. Java Heap Space
Frequency: 5 stars
Causes
1. Unable to allocate objects in the Java heap 2. Increased throughput 3. Application unintentionally retains object references, preventing GC 4. Excessive use of finalizers; the finalizer daemon may not keep up with the queue
Solutions
1. Increase heap size with -Xmx 2. Fix memory leaks in the application
2. GC Overhead Limit Exceeded
Frequency: 5 stars
Causes
Java process spends 98% of its time in garbage collection and recovers less than 2% of heap space, repeatedly hitting the overhead limit.
Solutions
1. Increase heap size with -Xmx 2. Disable the limit using -XX:-UseGCOverheadLimit 3. Fix memory leaks
3. Requested Array Size Exceeds VM Limit
Frequency: 2 stars
Causes
Application attempts to allocate an array larger than the maximum heap size.
Solutions
1. Increase heap size with -Xmx 2. Fix bugs that allocate excessively large arrays
4. PermGen Space Exhaustion
Frequency: 3 stars
Causes
PermGen stores class names, fields, methods, related object and type arrays, and JIT optimizations; when it runs out, the JVM throws an exception.
Solutions
1. Increase PermGen size with -XX:MaxPermSize 2. Restart the JVM (a full restart clears PermGen)
5. Metaspace Exhaustion
Frequency: 3 stars
Causes
Since Java 8, Metaspace replaces PermGen and stores class metadata in native memory; exhaustion triggers an exception.
Solutions
1. Increase Metaspace size with -XX:MaxMetaspaceSize 2. Remove the -XX:maxmetaspaceSize limit if set 3. Reduce Java heap size to free native memory for Metaspace 4. Allocate more physical memory to the server 5. Fix application bugs that cause uncontrolled class loading
6. Unable to Create New Native Thread
Frequency: 5 stars
Causes
Insufficient native memory prevents the OS from creating new threads; this indicates overall memory pressure.
Solutions
1. Add more memory to the machine 2. Decrease Java heap size 3. Fix thread leaks in the application 4. Raise OS limits (e.g., ulimit -a) 5. Increase the maximum number of user processes (e.g., -u 1800) 6. Reduce thread stack size with
-Xss7. Process or Sub‑process Killed by the OS
Frequency: 1 star
Causes
The kernel OOM killer terminates processes when available memory is critically low.
Solutions
1. Migrate the process to another machine 2. Add more memory to the host 3. Recognize that this is an OS‑level OOM, not a JVM error
8. StackTraceWithNativeMethod Error
Frequency: 1 star
Causes
Native method allocation failure; the top frame of the stack trace is a native method.
Solutions
Use native OS diagnostic tools to investigate the failure.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
