A Container's Memory Limit Counts the Files It Writes, So Low Program Memory Doesn't Mean Headroom
On cgroup v2, a container's memory limit (memory.max) applies to everything its cgroup is charged for, and that includes the page cache of the files its processes read and write, not only the program's own memory. memory.current therefore climbs to the limit on any I/O-heavy container and stays there, and memory.stat splits it into anon (the program's heap and stacks) and file (cache).
The usual reading is that the cache doesn't matter: when the cgroup reaches its limit, the kernel reclaims clean cache and carries on. That holds for clean pages. It doesn't hold for dirty pages, data written but not yet on disk: they must be written back before they can be freed. On a network filesystem, writeback goes at the speed of the network and the server. A container that writes faster than that fills its limit with pages the kernel can't free in time, and when a process then asks for memory the cgroup's OOM killer runs, even though most of the memory "is only cache".
What It Looks Like
My download client in Kubernetes (limit 1 GiB, writing to an NFS share):
- One download:
memory.currentat the limit for hours,anon108 MiB,file902 MiB,oom_kill 0. It looked like a textbook case of "ignore the cache". - Three jobs at once, one of them unpacking a large archive: the container was OOMKilled. The kernel log:
unrar invoked oom-killer: gfp_mask=…(…|__GFP_WRITE),constraint=CONSTRAINT_MEMCG(the container's own limit, the node had memory to spare), thenKilled process … (python3) … anon-rss:429628kB.
Two lessons from the same pod: program memory grew with the number of jobs (108 → ~420 MiB), and the writer that hit the wall was the unpacker, asking for a page to write into while the rest of the limit was cache still waiting for NFS.
What To Do
- Size the limit for the busiest moment, not the idle one: several jobs at once, unpacking or repairing. Measuring
anonduring one quiet download says little about that. - Cap the program's own buffers where it has a setting (a download client's article cache): programs often size defaults from the node's RAM, which they see, not from the container's limit, which they don't.
- Watch the right counters:
oom_killinmemory.eventsand the pod's restart count for kills;file_dirtyandfile_writebackinmemory.statfor cache that can't be dropped yet.memory.currentalone tells you nothing on such a container. - Read the kernel log after an OOMKill:
journalctl -kon the node shows which process triggered it, whether the constraint was the cgroup (CONSTRAINT_MEMCG) or the node, and the killed process'sanon-rssandfile-rss.
Related
- containerd 1.7 Gives Containers a Billion-File Limit, So Code Sized by It Can Exhaust a Node's RAM - the other side of limits: without one, a runaway took down the whole node.
- A Hardlink Can't Cross Mounts, So Downloads and Media Libraries Must Share One Mount - why the download client writes straight onto the NFS share.
- Kubernetes Storage Failures Usually Start on the Host or the NAS - NFS behaviour showing up as a Kubernetes symptom.
- Homelab v2 Rebuild - where it happened (2026-11-26, found 2026-11-27).
References
- Linux kernel: Control Group v2, Memory - primary source for
memory.max,memory.current,memory.stat(anon,file,file_dirty,file_writeback),memory.eventsandmemory.oom.group, and how page cache is charged to a cgroup. - Kubernetes blog: "The Shift to cgroup v2 in Kubernetes: What You Need to Know" - the kubelet's
singleProcessOOMKill: falsedefault settingmemory.oom.group, and page cache counting against pods on I/O-heavy workloads (last checked 2026-11). - kubernetes/kubernetes PR #117793 - the change that turned on
memory.oom.groupfor container cgroups. homelab/docs/troubleshooting.md, "SABnzbdOOMKilledwhile unpacking with several jobs" - the incident, with the commands used.
Review cards
Flashcard Why does `memory.current` of a container that writes a lot sit at its limit even when the program is small?
On cgroup v2 the page cache of the files its processes read and write is charged to the container's cgroup, so cache fills the limit up to memory.max.
Flashcard Why can a container be OOMKilled although most of its memory is "only cache"?
Dirty pages must be written back before they can be freed. When writeback is slow (NFS), the kernel can't reclaim enough in time and the cgroup's OOM killer runs.
Quiz A kernel OOM message says `constraint=CONSTRAINT_MEMCG`. What ran out of memory?
- The whole node
- The container's or pod's cgroup, at its own memory limit
- The GPU's memory
CONSTRAINT_MEMCG means the limit of a memory cgroup was reached; the node may have plenty free.
Flashcard Why did killing one helper process take down the whole download client's container?
On cgroup v2 the kubelet sets memory.oom.group on container cgroups, so the OOM killer kills every process in the container together.