The classic trap when upgrading the OS under a log collector: the Logstash container on a fresh distro hits the cgroup memory limit and dies of OOM even though the JVM heap is barely a third full. The alerts dig into the heap — but the heap has nothing to do with it. A step-by-step breakdown: symptoms, diagnostics, cause and the fix.
Symptoms
- The container's RSS grows to 98% of the limit; the OOM killer claims the process at peak write load.
- The heap is fine:
-Xmxnot exceeded, no OOM inside Java code, GC pauses calm. - The leak is outside the JVM, in native memory. On the old distro with the same load and the same config everything works.
- Reproduces on fresh Ubuntu/Debian (glibc 2.35+), not on older images.
Diagnostics step by step
PID=$(pgrep -f logstash | head -1)
# 1. How many threads? On a big machine - hundreds.
ps -o nlwp -p $PID
# 2. How much memory vs the heap?
pmap -x $PID | tail -1
cat /proc/$PID/smaps_rollup | grep -E 'Rss|Anonymous'
# 3. How many malloc arenas? Look for regions of the same size.
grep -c 'rw-p' /proc/$PID/smaps
The picture that settles the diagnosis: RSS is a multiple of the heap, the thread count is at its peak, and smaps is full of identical anonymous regions of ~64 MB each — these are glibc malloc arenas, one per background thread.
The cause
In recent glibc, every background thread of the JVM and JNI gets a separate malloc arena. The default arena count is 8 × the number of cores. In a multithreaded application this fragments ptmalloc: pages are held by arenas and never returned to the kernel, so RSS grows regardless of the heap. The known issue is described in JDK-8193521; Logstash is a typical victim: pipeline workers, flush threads, plugins — all background threads.
The fix
Three levels, from easy to heavy. The first one covers 90% of cases:
environment:
- MALLOC_ARENA_MAX=2
For Java 21+ there's also forced native heap trimming — once a minute the JVM asks glibc to return freed pages to the kernel:
# jvm.options
-Xms4g
-Xmx4g
-XX:+UnlockExperimentalVMOptions
-XX:TrimNativeHeapInterval=60000
Doesn't help — switch the allocator to jemalloc, which returns memory more aggressively than glibc:
RUN apt-get update && apt-get install -y libjemalloc2
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
And the finishing touch — an honest layout of the container's memory: half the limit for the heap, half for native. Otherwise an -Xmx equal to the limit will guarantee the collector runs out of native headroom.
The result
| Metric | Before | After |
|---|---|---|
| Container RSS | 98% of the limit, grows for hours | steady 55–60%, flat |
| OOM per week | 3–5 incidents | 0 |
| Throughput | unchanged | unchanged |
General lessons for JVM containers
- Watch RSS, not just the heap. The container metric
container_memory_rssagainst the limit is the main graph; heap metrics mislead. - An OS upgrade is a memory model upgrade. A test run of JVM services on the new distro under real load is a mandatory item of the plan.
- The same fix applies to Elasticsearch, Kafka Connect and any JNI-heavy services — the symptom is the same, not only in Logstash.
MALLOC_ARENA_MAX=2is the default for all Java containers with many threads: the setting costs nothing, the memory savings are tens of percent.
The moral: upgrading the distro, you also upgrade its malloc. Check the memory of JVM containers by RSS, not by heap — otherwise you'll learn about glibc arenas at three in the morning.
Diagnostics in Kubernetes: the same steps via kubectl
POD=$(kubectl get pod -l app=logstash -o name | head -1)
# container memory against the limit
kubectl get --raw /api/v1/namespaces/logs/pods/$POD/... | jq .memory
# top memory by processes inside the pod
kubectl exec $POD -- bash -c 'ps aux --sort=-rss | head'
# who holds anon memory
kubectl exec $POD -- bash -c 'grep -E "Rss|AnonHuge" /proc/1/smaps_rollup'
# and the main check - the arena count via jemalloc stats, or just pmap:
kubectl exec $POD -- pmap 1 | grep -c 00007f
The practical marker in k8s: container_memory_working_set pins at the limit and grows monotonically, and the events show Pod OOMKilled with exit code 137. If a fresh base image bump preceded it — almost certainly the arenas.
Similar symptoms — different diseases
| Symptom | glibc arenas | Heap leak | Direct memory |
|---|---|---|---|
| RSS grows | yes, in steps | yes, smoothly | yes, up to max direct |
| Did GC help? | no | yes (full GC frees) | no |
| Heap metrics | normal | persistently high used | normal |
| Quick test | MALLOC_ARENA_MAX=2 | jmap histogram | -XX:MaxDirectMemorySize + Netty metrics |
So the order of elimination is: first the heap histogram (5 minutes), then direct memory metrics, and only then the native allocator — otherwise you can spend a week tuning the wrong thing.
A memory checklist for a JVM container
-Xmx= 50% of the container limit (no more).MALLOC_ARENA_MAX=2in the image env — the default for all Java services.- Java 21+:
TrimNativeHeapInterval=60000. - An alert on
container_memory_working_set > 85% of limit for 10m— before OOMKiller strikes. - When upgrading the base image — a staging soak test under real load, comparing p99 RSS before/after.