High Kubernetes pod memory isn't always a leak: why JVM apps like WSO2 API Manager 'fill' their limitNote · digest for November 24
Security engineer's desk with a studio still life of cryptographic security artifacts and a clean verification monitor free of third-party trackers
Note3 sections≈ 4 min

High Kubernetes pod memory isn't always a leak: why JVM apps like WSO2 API Manager 'fill' their limit

You open your monitoring dashboard, check a pod running WSO2 API Manager, and see memory sitting at 85–90% of its limit. Your first instinct is a memory leak—better double limits.memory before everything crashes. Only after the upgrade, the graph climbs right back to the new ceiling and parks there again.

For JVM services in Kubernetes, this is completely normal. A high flat line on a dashboard doesn't prove the app is starving. To avoid paying for idle gigabytes—while still catching a real OOMKilled—let's look at what the orchestrator measures and where RAM goes inside Java.

Why the dashboard scares you early

kubectl top pod and Grafana charts read the container_memory_working_set_bytes metric. According to the Kubernetes documentation, it adds the process's resident memory (container_memory_rss) to the active Linux kernel file cache (page cache). WSO2 API Manager reads JAR archives and configs, and the kernel caches them in free RAM. Under memory pressure, Linux drops that cache safely without harming the app, yet on your chart it looks like a nearly full limit.

The second reason is JVM behavior. The JVM reserves and commits memory upfront for its heap and pools. Even after a garbage collector like G1GC clears old objects, it doesn't rush to return pages to the OS because it expects the next traffic spike. From the outside, memory sits near the ceiling while the service runs comfortably inside.

What makes up JVM memory in a container

A common mistake is equating the container limit with Java Heap size. In reality, the heap is only part of the footprint; native memory (off-heap) takes the rest. Without headroom for off-heap, the Linux kernel kills the container with SIGKILL and exit code 137 (OOMKilled), and the JVM won't even get a chance to throw OutOfMemoryError or write a heap dump.

The heap holds application objects—in WSO2 API Manager, that means JWT caches, keys, resources, and analytics queues. Off-heap holds Metaspace for class and classloader metadata, thread stacks (-Xss), Code Cache for JIT-compiled code, and GC structures. Direct ByteBuffers also consume native memory for network I/O, which HTTP transports like WSO2's passthrough layer rely on. On top of all that sits the kernel page cache.

How to tell normal usage from real memory pressure

If your chart shows 85%, don't panic yet. Compare container_memory_working_set_bytes and container_memory_rss in Prometheus: when working_set is high while rss stays flat well below the limit, the gap is just page cache.

Next, look inside the process. OpenJDK includes Native Memory Tracking (NMT)—enable it at startup and pull a live breakdown inside the pod.

# Compare Kubernetes' external view with the JVM memory map
kubectl top pod wso2-apim-0 -n api-gateway

# Summary of heap and native regions (PID 1 is the main JVM process)
kubectl exec -n api-gateway wso2-apim-0 -- \
  jcmd 1 VM.native_memory summary

# Heap generation usage and GC pauses (5 samples every 2 seconds)
kubectl exec -n api-gateway wso2-apim-0 -- \
  jstat -gcutil 1 2000 5

jcmd breaks the Committed column down into Java Heap, Metaspace, Thread, Code, GC, and Other. Real memory pressure shows three signs: container_memory_rss climbs steadily without dropping after GC cycles, jstat shows a full Old Generation with growing GC pauses, or a section like Thread or Metaspace keeps expanding in NMT. If those numbers stay flat, there is no leak.

Finally, tune your startup flags. Per the OpenJDK 21 java documentation, -XX:MaxRAMPercentage defaults to 25.0—a quarter of the container limit. Cranking it up to 90% isn't safe either, because native memory still needs room.

# JVM flags for a container with off-heap and NMT headroom
java \
  -XX:MaxRAMPercentage=70.0 \
  -XX:InitialRAMPercentage=70.0 \
  -XX:MaxMetaspaceSize=256m \
  -XX:NativeMemoryTracking=summary \
  -XX:+HeapDumpOnOutOfMemoryError \
  -XX:HeapDumpPath=/tmp/heap.hprof \
  -jar app.jar

NMT's summary mode requires a restart and adds only a couple percent overhead, making it safe in production unlike the heavier detail mode. -XX:MaxMetaspaceSize caps class metadata growth, and -XX:+HeapDumpOnOutOfMemoryError saves a dump for Eclipse MAT if a real heap leak ever happens.

Practical formula for container memory headroom

Set -XX:MaxRAMPercentage between 65.0 and 75.0, leaving 25–35% of limits.memory for Metaspace, thread stacks, Code Cache, NIO buffers, and the OS. That lets the JVM use memory efficiently without hitting OOMKilled during traffic spikes.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 6