How-to guide
Java memory usage: heap, metaspace and leaks
A JVM uses much more memory than its heap. Knowing where the rest goes is the key to sizing containers and avoiding surprise OOM kills.
3 min read9 sectionsUpdated
On this page
| Area | Controlled by | Notes |
|---|---|---|
| Heap | -Xmx or -XX:MaxRAMPercentage | Your objects; the only area GC collects |
| Metaspace | -XX:MaxMetaspaceSize | Class metadata; grows with classes loaded |
| Thread stacks | -Xss × thread count | ~1 MB per thread by default |
| Code cache | -XX:ReservedCodeCacheSize | JIT-compiled code |
| Direct buffers | -XX:MaxDirectMemorySize | NIO, Netty; not on the heap |
| GC and JVM overhead | GC choice | Card tables, remembered sets |
Sizing for containers
With a 1 GiB container limit, a heap of 1 GiB will be killed by the kernel, because everything else needs room too. A common starting point is -XX:MaxRAMPercentage=75, then check real RSS under load. Watch both heap after GC and container memory against its limit; see Kubernetes OOMKilled.
# Native memory tracking: where non-heap memory goes
java -XX:NativeMemoryTracking=summary -jar app.jar
jcmd <pid> VM.native_memory summary# heap after GC over time
jcmd <pid> GC.heap_info
# capture a heap dump (pauses the JVM; do it on one instance out of rotation)
jcmd <pid> GC.heap_dump /tmp/heap.hprof
# dump automatically on OutOfMemoryError
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps -jar app.jarOpen the dump in Eclipse MAT or VisualVM, run the leak suspects report and look at the dominator tree: the objects retaining the most memory and the reference chain keeping them alive. Usual culprits are unbounded caches, static collections, ThreadLocals in thread pools and listeners never removed.
Watching it continuously
The OpenTelemetry Java agent reports JVM memory and GC metrics; chart heap after GC and alert when it stays above 85% of max. VitalRix infrastructure monitoring shows each pod's memory against its limit, and APM deploy markers show which release started the growth. See also Java performance monitoring and Java OutOfMemoryError.
Alerts to start with
Heap after GC above 85% of max for 15 minutes, GC time above a few percent of wall time, metaspace growing after each redeploy (a classloader leak), container memory above 90% of its limit, and any OutOfMemoryError in logs. Pair each with a runbook that says how to take a heap dump safely.
Why is my Java process using more memory than -Xmx?
Because -Xmx limits only the heap. Metaspace, thread stacks, code cache, direct buffers and JVM overhead come on top.
Should I set -Xms equal to -Xmx?
In containers it is common, to avoid heap resizing and make memory use predictable. It does mean the memory is committed up front.
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.
- How to find a Node.js memory leak: Detect a Node.js memory leak from heap metrics, compare heap snapshots to find what retains memory, fix the usual causes and alert before a crash.
- Python memory usage: find what uses memory: Measure and reduce Python memory usage: RSS vs allocated objects, tracemalloc snapshots, finding leaks in long-running workers.
- Goroutine leaks in Go: how to find and fix them: Find and fix goroutine leaks in Go: read pprof goroutine profiles, spot blocked channels and missing context cancellation, and use goleak in your tests.
- .NET OutOfMemoryException: causes and how to fix it: Fix .NET System.OutOfMemoryException: use dotnet-counters and dotnet-gcdump, spot Large Object Heap fragmentation and set the right GC limits in containers.
- 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.
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.