A Container's Memory Limit Counts the Files It Writes, So Low Program Memory Doesn't Mean Headroom

Kubernetes Linux

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.current at the limit for hours, anon 108 MiB, file 902 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), then Killed 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 anon during 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_kill in memory.events and the pod's restart count for kills; file_dirty and file_writeback in memory.stat for cache that can't be dropped yet. memory.current alone tells you nothing on such a container.
  • Read the kernel log after an OOMKill: journalctl -k on the node shows which process triggered it, whether the constraint was the cgroup (CONSTRAINT_MEMCG) or the node, and the killed process's anon-rss and file-rss.

References

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.