Home › Java › java.lang.OutOfMemoryError: Java heap space — Find the Leak (Don't Just Raise -Xmx)
Intermediate 3 min · November 21, 2026

java.lang.OutOfMemoryError: Java heap space — Find the Leak (Don't Just Raise -Xmx)

OutOfMemoryError: Java heap space means live objects filled the heap.

N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.

Follow
✓ Production
production tested
November 21, 2026
last updated
2,136
articles · all by Naren
Before you start⏱ 7 min
  • ✓Basic Java
  • ✓JVM memory basics
 ● Production Incident 🔎 Debug Guide
⚡Quick Answer
  • The heap is full of objects that are still reachable after GC.
  • Look at heap usage right after each GC: a rising floor is a leak; a flat floor that's high means the heap is too small.
  • Raising -Xmx only delays a leak.
  • Add -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps.
  • Open the .hprof in Eclipse MAT; the dominator tree shows the biggest retained object.
  • Usual leaks: unbounded caches/static maps, listeners never removed, ThreadLocals in pools.
  • In containers, size the heap with -XX:MaxRAMPercentage.
✦ Definition~90s read
What is java.lang.OutOfMemoryError?

java.lang.OutOfMemoryError: Java heap space is thrown when the JVM can't allocate an object because the heap (sized by -Xmx) is full of objects that are still reachable, even after garbage collection.

★
The heap is a cupboard.
Plain-English First

The heap is a cupboard. Garbage collection throws out what nobody uses. If something keeps everything it's ever given, like a cache with no limit, the cupboard fills no matter how often you clean it. A bigger cupboard just takes longer to fill.

The order service died every three days with Java heap space. Doubling -Xmx made it last six. The heap wasn't too small; a cache was growing forever.

Leak or Too Small?

Ignore the sawtooth; look at the low points right after each collection. If they rise over time, something is never freed. If they're flat, the heap is simply too small for the load.

gc.shBASH
1
2
jstat -gcutil <pid> 10s     # watch O (old gen %) after each full GC
jcmd <pid> GC.heap_info
🎯 Key Takeaway
The floor after GC tells you which.

Capture a Heap Dump

Have the JVM dump the heap when it throws OutOfMemoryError, so you see exactly what filled it.

jvm.shBASH
1
2
3
4
5
6
7
java -Xmx2g \
  -XX:+HeapDumpOnOutOfMemoryError \
  -XX:HeapDumpPath=/dumps \
  -jar order-service.jar

# or on demand
jcmd <pid> GC.heap_dump /dumps/now.hprof
🎯 Key Takeaway
Always run production with dump-on-OOM.

Find the Leak in MAT

Open the .hprof in Eclipse MAT. The dominator tree lists objects by retained size; the leak is usually one collection holding most of the heap.

mat.txtTEXT
1
2
3
Dominator tree:
  java.util.HashMap  (static CustomerCache.MAP)   1.6 GB  78%
  ...
🎯 Key Takeaway
Biggest retained object = suspect.

Bound It

Give caches a maximum size and expiry; remove listeners; clear ThreadLocals.

CustomerCache.javaJAVA
1
2
3
4
Cache<String, Customer> cache = Caffeine.newBuilder()
    .maximumSize(10_000)
    .expireAfterWrite(Duration.ofHours(1))
    .build();
🔥Containers
Size the heap from the container limit with -XX:MaxRAMPercentage=75 instead of a fixed -Xmx.
🎯 Key Takeaway
Anything that grows needs a limit.
● Production incidentPOST-MORTEMseverity: high

Doubling the Heap Bought Three Days

Symptom
java.lang.OutOfMemoryError: Java heap space after about 71 hours of uptime, every time.
Assumption
The team assumed traffic had grown and the 2 GB heap was too small.
Root cause
A static HashMap cached customer lookups with no eviction. Heap-after-GC rose about 0.3 GB a day until it hit the limit.
Fix
Replaced the map with a Caffeine cache with maximumSize(10_000) and expireAfterWrite. Heap-after-GC stayed flat at ~0.7 GB.
Key lesson
  • Watch heap after GC.
  • Dump on OOM.
  • Bound every cache.
Production debug guideLeak or too small?3 entries
Symptom · 01
Heap-after-GC rises over hours or days
→
Fix
Leak: take a heap dump and check the dominator tree.
Symptom · 02
Flat but near the limit under load
→
Fix
Heap too small: raise -Xmx or reduce per-request memory.
Symptom · 03
Single huge allocation
→
Fix
Stream the file or result set instead of loading it all.
⚙ Quick Reference
4 commands from this guide
FileCommand / CodePurpose
gc.shjstat -gcutil <pid> 10s # watch O (old gen %) after each full GCLeak or Too Small?
jvm.shjava -Xmx2g \Capture a Heap Dump
mat.txtDominator tree:Find the Leak in MAT
CustomerCache.javaCache<String, Customer> cache = Caffeine.newBuilder()Bound It

Key takeaways

1
Reachable objects filled the heap.
2
Rising floor after GC = leak.
3
-Xmx only delays a leak.
4
Dump on OOM, read the dominator tree.
5
Bound caches.

Common mistakes to avoid

3 patterns
×

Raising -Xmx for a leak

Symptom
Crashes later, not never.
Fix
Find and bound the leak.
×

No heap dump on OOM

Symptom
Nothing to analyse after the crash.
Fix
HeapDumpOnOutOfMemoryError.
×

Unbounded static caches

Symptom
Steadily rising heap floor.
Fix
maximumSize and expiry.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
How do you tell a memory leak from an undersized heap?
Q01 of 01SENIOR

How do you tell a memory leak from an undersized heap?

ANSWER
Look at heap usage after GC: rising over time is a leak; flat but high means the heap is too small.
FAQ · 3 QUESTIONS

Frequently Asked Questions

01
How do I fix java.lang.OutOfMemoryError: Java heap space?
02
How do I increase Java heap size?
03
What's the difference from GC overhead limit exceeded?
N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.

Follow
✓ Verified
production tested
November 21, 2026
last updated
2,136
articles · all by Naren
🔥

That's Exceptions. Mark it forged?

3 min read · try the examples if you haven't

←
Previous
java.net.SocketTimeoutException: Read Timed Out
12 / 12 · Exceptions
Next
ADB Device Unauthorized Fix
→