Error explained
java.lang.OutOfMemoryError: causes and fixes
The message after OutOfMemoryError tells you which memory area ran out. Each one has a different cause and fix.
3 min read8 sectionsUpdated
On this page
| Message | Meaning | Typical fix |
|---|---|---|
| Java heap space | Heap full of live objects | Find the leak or large allocation; raise heap if the live set is genuinely bigger |
| GC overhead limit exceeded | GC is using almost all CPU and freeing little | Same as heap space: too much live data |
| Metaspace | Too many classes loaded | Classloader leaks after redeploys; raise MaxMetaspaceSize if legitimate |
| unable to create native thread | OS refused another thread | Thread leak or limits (ulimit -u, container pids limit) |
| Direct buffer memory | Off-heap NIO buffers exhausted | Netty/NIO buffers not released; tune MaxDirectMemorySize |
| Requested array size exceeds VM limit | One huge array | Stream or chunk the data |
ExitOnOutOfMemoryError makes the JVM exit instead of limping on in a broken state, so the orchestrator restarts it. Analyse the dump with Eclipse MAT: the dominator tree shows which objects retain the memory. See Java memory usage for all the areas a JVM uses.
Catching it in production
An OOM crash shows as restarts and errors; the heap-after-GC trend on dashboards usually climbed for hours before. Alert on heap after GC above 85% and on container memory near its limit.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
Should I just increase -Xmx?
Only after confirming the live data set legitimately needs it. If memory grows without bound, a bigger heap only delays the crash.
What is the difference between OOMKilled and OutOfMemoryError?
OutOfMemoryError is thrown by the JVM when one of its own areas is full. OOMKilled is the kernel killing the whole process for exceeding the container memory limit.
Related reading
- GC overhead limit exceeded in Java: how to fix it: Fix java.lang.OutOfMemoryError: GC overhead limit exceeded. Enable GC logs, take a heap dump, find what fills the heap and size the JVM for containers.
- java.lang.StackOverflowError: causes and how to fix it: Fix java.lang.StackOverflowError: read the repeating frames, find runaway recursion, circular toString and JSON mappings, and know when -Xss is appropriate.
- JavaScript heap out of memory: how to fix it in Node.js: Fix FATAL ERROR: JavaScript heap out of memory. Set --max-old-space-size correctly, handle build-time failures and find runtime leaks with heap snapshots.
- 401 Unauthorized: causes, fixes and how it differs from 403: Fix 401 Unauthorized errors: expired tokens, missing Authorization headers, proxies stripping credentials and clock skew, plus how 401 differs from 403.
- 503 Service Unavailable: causes and fixes: Why servers return 503 Service Unavailable, from overload and maintenance to no healthy backends, how to diagnose each cause and how to recover and prevent it.
- PHP maximum execution time exceeded: how to fix it: Fix PHP Maximum execution time of 30 seconds exceeded: why external waits may not count, which timeouts line up with Nginx, and how to move long work away.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.