java.lang.OutOfMemoryError: Java heap space
The JVM ran out of heap memory and garbage collection couldn’t free enough.
Meaning
Either the heap (-Xmx) is too small for the workload, or the application leaks memory (objects stay referenced). Variants: “GC overhead limit exceeded”, “Metaspace”, “unable to create native thread”.
Common causes
- Heap too small for the workload
- Memory leak (static collections, caches without eviction, listeners)
- Loading huge files/result sets into memory
- Container memory limit lower than JVM assumes
⚡ Quick fix
- Increase heap:
-Xmx2g(or-XX:MaxRAMPercentage=75in containers) - Capture a heap dump:
-XX:+HeapDumpOnOutOfMemoryErrorand analyze with Eclipse MAT - Stream large data instead of loading it
Detailed fix by platform
Java
- In Docker/Kubernetes use
-XX:MaxRAMPercentageso the heap respects the container limit.
Android
- Gradle builds: set
org.gradle.jvmargs=-Xmx4gin gradle.properties.
How to diagnose
- Variant — Heap, Metaspace, GC overhead or native threads?
- Trend — Does memory grow steadily (leak) or spike (load)?
- Heap dump — Which objects dominate?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026