Fix Java that runs out of memory
We'll confirm the error, increase heap with -Xmx, check for leaks—or tell you when to escalate.
What you'll need
- Access to JVM startup options (script, systemd, IDE)
- Optional: profiler (Eclipse MAT, VisualVM) for heap analysis
Step-by-step diagnostic
Quick triage — pick your path
Get started
Choose the option that matches what you see. You can jump straight to that section.
- Follow this guide Work through the full procedure.
- Check heap and increase -Xmx You want to see current settings and increase heap.
- Analyze with heap dump OOM recurs and you need to find the cause. Escalate if OOM continues at max -Xmx.
- When to escalate OOM continues at max -Xmx or you need analysis.
Show full guide
Steps
Goal: Confirm the error, increase heap with -Xmx, check for leaks.
- Check the logs. OutOfMemoryError: Java heap space or GC overhead limit exceeded.
- Good: You see OutOfMemoryError. Proceed to Check heap and increase -Xmx.
- Bad: Different error—check the exact message.
Check heap and increase -Xmx
Goal: See current heap limit and increase it.
- Check how the JVM is started. Look for -Xmx in the script, systemd unit, or IDE. Run
java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSizeto see default. - Add
-Xmx<size>to the JVM options. Examples:-Xmx2g,-Xmx4096m. Use-Xms2g -Xmx2gfor fixed heap. Restart the JVM. - When the app runs in Docker or systemd, check container memory limits. -Xmx must fit within the limit.
- Good: New -Xmx set and JVM restarted. Monitor for OOM.
- Bad: OOM continues—see Analyze with heap dump.
Analyze with heap dump
Goal: Capture a heap dump on OOM and analyze for leaks.
- Add
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dump.hprofto JVM options. Reproduce the OOM. The JVM writes a .hprof file. - Open the dump in Eclipse MAT or VisualVM. Look for retained objects and memory leaks.
- When a leak is found, fix the application code or upgrade the library.
- Good: Dump captured and analyzed. Fix applied or -Xmx increased.
- Bad: OOM continues at max -Xmx—see When to escalate.
When to escalate
When to get help: Escalate if:
- OOM continues after increasing -Xmx to the system limit.
- You need a heap dump analyzed.
- The app has a known leak that requires code changes.
Provide -Xmx value, full OutOfMemoryError, and heap dump path before escalating.
Verification
- The JVM runs without OutOfMemoryError under normal load.
- -Xmx is set and visible in the process or startup logs.
- Heap usage is stable or within limits when monitored.
Escalation ladder
Work from the device outward. Stop when the problem is fixed.
- Increase -Xmx Add -Xmx2g (or appropriate size) to JVM options.
- Heap dump Add -XX:+HeapDumpOnOutOfMemoryError; analyze with MAT or VisualVM.
- Check for leak Monitor heap over time; restart and observe.
- Tune GC Try G1GC or adjust MaxGCPauseMillis.
- Escalate Provide -Xmx, error, heap dump; add RAM or fix app.
What to capture if you need help
Before calling support or posting for help, have these ready. It speeds everything up.
- Full [OutOfMemoryError](#term-outofmemoryerror) message
- Current -Xmx and -Xms values
- Heap dump path (if generated)
- Steps already tried
Do you see OutOfMemoryError?
Check logs or console. OutOfMemoryError: Java heap space or GC overhead limit.
You can change your answer later.
Check the exact error
What is the current -Xmx setting?
Check JVM startup options or run -XX:+PrintFlagsFinal -version.
You can change your answer later.
Increase -Xmx and restart
Does heap grow until OOM without leveling off?
Monitor with jstat -gc <pid> or a profiler.
You can change your answer later.
Add heap dump and analyze
Escalate
Reviewed by Blackbox Atlas
Frequently asked questions
- What causes Java OutOfMemoryError?
- The [heap](#term-heap) is exhausted. Too little -Xmx, a memory leak, or a spike in allocation. Increase -Xmx first; then check for leaks.
- How do I increase Java heap size?
- Add -Xmx<size> to the JVM options. Example: -Xmx2g or -Xmx4096m. Set -Xms to match -Xmx for fixed heap. Restart the JVM after changing.
- When should I escalate Java OOM?
- If -Xmx is already high and OOM continues, or you need a heap dump analyzed. Provide -Xmx value, error message, and heap dump if available.
Rate this guide
Was this helpful?
Thanks for your feedback.