Skip to content

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

Key points

  • RSS = heap + metaspace + thread stacks + code cache + direct buffers + GC overhead.
  • Size the heap as a share of the container limit with MaxRAMPercentage.
  • Heap after GC is the number that reveals leaks.
  • Heap dumps show exactly which objects are retained.

Where JVM memory goes

AreaControlled byNotes
Heap-Xmx or -XX:MaxRAMPercentageYour objects; the only area GC collects
Metaspace-XX:MaxMetaspaceSizeClass metadata; grows with classes loaded
Thread stacks-Xss × thread count~1 MB per thread by default
Code cache-XX:ReservedCodeCacheSizeJIT-compiled code
Direct buffers-XX:MaxDirectMemorySizeNIO, Netty; not on the heap
GC and JVM overheadGC choiceCard 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.

bash
# Native memory tracking: where non-heap memory goes
java -XX:NativeMemoryTracking=summary -jar app.jar
jcmd <pid> VM.native_memory summary

Finding a leak

bash
# 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.jar

Open 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.

Frequently asked questions

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.

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.