Common Java OutOfMemoryError Types and Their Solutions
This article lists the most frequent Java OutOfMemoryError cases—including Java heap space, GC overhead limit exceeded, PermGen/Metaspace, native thread creation failures, array size limits, and direct buffer memory—explains their root causes, and provides practical troubleshooting steps and JVM tuning parameters to resolve them.
When the JVM runs out of memory, it throws a java.lang.OutOfMemoryError. The article summarizes the most common OOM causes and their solutions.
1. Java heap space
Occurs when the heap cannot accommodate new objects, triggering java.lang.OutOfMemoryError: Java heap space. Typical reasons include creating oversized objects, traffic spikes, excessive finalizer usage, and memory leaks (e.g., unclosed resources). The usual fix is to increase the -Xmx heap size, or address the specific cause such as limiting large object creation, adding resources, or fixing leaks.
2. GC overhead limit exceeded
Thrown when the JVM spends >98% of time in GC but recovers <2% of memory repeatedly. The cause and solution are similar to heap space issues: increase heap size or reduce memory pressure.
3. PermGen space (pre‑Java 8)
Indicates the permanent generation is full, often due to loading many or large classes. Solutions include increasing -XX:MaxPermSize, restarting the JVM to clear duplicated classloaders, or enabling class unloading with -XX:+CMSClassUnloadingEnabled and -XX:+UseConcMarkSweepGC. Memory dumps can be analyzed with jmap -dump:format=b,file=dump.hprof <pid> and Eclipse MAT.
4. Metaspace (Java 8+)
Metaspace replaces PermGen; the error java.lang.OutOfMemoryError: Metaspace has similar causes. Adjust its size with -XX:MaxMetaspaceSize.
5. Unable to create new native thread
Occurs when the OS cannot allocate a native thread due to OS limits (ulimit, kernel.pid_max), or insufficient native memory. Solutions: increase OS limits, add memory, reduce thread count, lower stack size with -Xss, and configure thread pools.
6. Out of swap space
All virtual memory is exhausted. Causes include address space shortage, full physical memory, native memory leaks, or large DirectByteBuffer usage. Remedies: move to 64‑bit, check for compression issues, lower -XX:MaxDirectMemorySize, or upgrade hardware.
7. Requested array size exceeds VM limit
The JVM limits array length to Integer.MAX_VALUE-2. Verify whether such large arrays are necessary and split them if possible.
8. Direct buffer memory
Direct ByteBuffer default limit is 64 MB; exceeding it throws java.lang.OutOfMemoryError: Direct buffer memory. Diagnose with tools like Arthas, check NIO usage, adjust -XX:MaxDirectMemorySize, remove -XX:+DisableExplicitGC if present, and ensure proper buffer cleanup.
Recommended Tools & Products
• Eclipse Memory Analyzer (MAT) – https://www.eclipse.org/mat
• Alibaba Cloud APM for OOM keyword alerts – https://help.aliyun.com/document_detail/42966.html
• Alibaba Arthas – https://github.com/alibaba/arthas
For further reading see articles on Plumbr, GCeasy, and JVM memory architecture.
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.
