Sub topic 14
Making Applications Respect Container Limits
This chapter assumes you know how to set --memory and --cpus with docker run, and that you know what an out-of-memory (OOM) kill looks like (exit code 137). The goal here is to make the program inside the container aware of those limits.
Why applications can be unaware
Docker enforces limits with cgroups. The program inside the container is not told about them. Many tools read host-wide values instead:
/proc/meminfoandfreereport the host's total memory.nprocand many runtimes count the host's CPU cores.nprochonors--cpuset-cpus, but it ignores--cpus.
An application that sizes itself from those numbers may plan for far more than the container is allowed to use. For memory, the result is an OOM kill. For CPU, the result is throttling.
Memory: sizing caches and heaps
A runtime that sets its heap from host memory can grow past the container limit. The kernel then kills the process, even though the runtime believes it still has room.
Most modern runtimes can read the cgroup limit. The Java Virtual Machine (JVM) does this by default in current versions. You can then set the heap as a fraction of the container limit:
docker run -d --name api --memory 512m \
-e JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75.0" \
eclipse-temurin:21-jreThis gives the heap about 384 MiB. The remaining 128 MiB is for thread stacks, metaspace, and native buffers. A heap set equal to the whole limit leaves no room for these, so the container is still killed.
Other runtimes need similar settings:
- Node.js: set
--max-old-space-size(in MiB) below the container limit. - Go: set the
GOMEMLIMITenvironment variable, for exampleGOMEMLIMIT=400MiB, to make the garbage collector work harder as usage nears that value. - Python and C programs: they have no heap cap. Keep your own caches bounded.
CPU: sizing thread pools
A --cpus 0.5 limit does not reduce the core count that the application sees. A program on an 8-core host may start 8 worker threads. Those threads share half a core of quota, so the kernel throttles them. You will see high latency and bursts of stalls, even though average CPU use looks modest.
Set pool sizes from the limit, not from the host. The Go runtime, for example, uses the host core count for GOMAXPROCS unless you override it:
docker run -d --name worker --cpus 2 -e GOMAXPROCS=2 my-go-serviceRecent JVMs derive availableProcessors() from the CPU quota, rounding up. Other languages often need an explicit setting such as a worker count or pool size environment variable.
Reading the limits from inside
You can check what the container is subject to by reading the cgroup files. On a host using cgroup v2:
docker run --rm --memory 256m --cpus 0.5 alpine \
sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max'Expected output:
268435456
50000 100000The first value is the memory limit in bytes. The second pair is the quota and period in microseconds, which is half a core. On cgroup v1 hosts, the files are /sys/fs/cgroup/memory/memory.limit_in_bytes and /sys/fs/cgroup/cpu/cpu.cfs_quota_us.
Common mistakes
- Setting the heap to 100% of the container limit.
- Trusting
freeortopinside the container to show the limit. - Leaving thread pools at the host core count under a low
--cpusvalue. - Using an old runtime version that predates cgroup awareness.
Exercise
A container runs with --memory 1g. The Java application inside uses -Xmx1g and is repeatedly killed with exit code 137, although its heap never exceeds 1 GiB. What is the likely cause, and what would you change?
Show answer
The JVM needs memory outside the heap (thread stacks, metaspace, native buffers), so total usage passes the 1 GiB limit and the kernel kills the process. Lower the heap, for example with -XX:MaxRAMPercentage=75.0, so the heap is about 768 MiB. Alternatively, raise the container limit.