Linux: Why Containers Run Out of Memory With RAM Free
Outline
Transcript
0:00 A Linux container is an isolated space for a program. The program can run out of memory there while the machine has plenty available. We'll follow a single request up to the memory limit Linux enforces. Then we'll see how to tell why the program stopped. We read the documentation and followed the code in Linux's kernel, the part of the operating system that controls memory. The links are in the description. Take a worker that builds records and holds them in memory. It failed when we capped it at a few dozen megabytes.
0:30 We ran it in a virtual machine, a computer simulated inside another one, which still reported about 7 gigabytes available. Yeah, it died with gigabytes to spare. Next we gave the worker three times that limit and it finished. Same work. The exact amounts are on screen. The limit was the only change. My first thought was to check the machine's memory, which would have told me there was room somewhere. Where did Linux decide there was no room for this worker? At the worker's own memory limit. An engineer checking the machine might say, “There's 7 gigabytes right here.” That chart shows what the machine has available.
1:08 The worker has a smaller allowance. Let's track how Linux accounts for a new piece of memory. Which group gets the added usage? First look at the box around our worker. Linux can account for a set of running programs, or processes, together. That box is their control group, often called a cgroup. For example, the worker saves a record and touches a new page, a small chunk of memory. A memory charge is Linux adding that page's usage to the group's account. The command program that starts the worker, its shell, shares that account.
1:41 Linux checks the group's usage against its hard limit, called memory dot max. There's one more place our worker can hit a limit. Picture it in one of these two containers. They sit inside a larger group, called the parent. Could they fill its account while each stays under its own limit? They could. The parent counts both. I think it's worth finding out which limit was hit first. That tells me where Linux will try to make space. Our worker hit its own limit in the run we're tracing. Can Linux free some of what it counted there so the worker can keep going? Once the next charge would take the group over its limit, Linux usually tries to free memory already charged there.
2:24 The kernel calls that reclaim. For instance, a group may hold data that Linux kept in memory after reading it from a file. If the file still has the data, Linux may be able to drop that copy. In our test the worker kept its records. The group couldn't use swap, storage that Linux can move some data to when it needs more working memory. That leaves less reclaimable memory in the group. If the kernel can free enough, it can retry the charge. What happens if there still isn't room? On this path Linux can begin an out-of-memory search.
2:59 The kernel code passes along the group that hit the limit. So we know where Linux looks. What decides which process gets picked? Linux compares the processes it can kill inside that group. Which means the request that set this off and the choice of which program to stop are two separate decisions. Now for that choice. Walk me through these two workers. One asks for another page. How does Linux choose who dies? Take this pair. The worker on the left asks for more memory. The other is holding much more already.
3:30 In the kernel version we traced, Linux looks inside the group and any groups nested under it. It weighs memory use and settings that make each program more or less likely to get picked. It could stop the worker on the right. So the request starts the search, and that comparison decides who stops. I'd probably look at the one on the right first. Meanwhile the one on the left could still be adding to the total. Yes. The one asking for more is still running on the left in our drawing. When we ran the test, Linux stopped the worker that asked for more.
4:06 The run left us one witness. The shell stayed alive and printed the worker's exit status, the number you get when a program stops. It printed the group's counters too. Those are running totals of memory events. The kernel can force the victim to stop by sending a message called signal nine. The shell's number is consistent with that signal. I'd have read that number as the explanation for the stop. Could I see it even if the app had enough memory? First check the actual result. We saw 137. An engineer can send the same signal and produce it.
4:47 The screen has two event counts. Why keep a separate kill count? Both went from zero to one. The out-of-memory count says Linux was about to fail a memory request because the group reached its limit. The kill total says Linux stopped a process in the group because of a memory shortage. At the higher limit, both stayed at zero and the worker completed. The peak on screen is the highest amount charged to the whole group during that run, including the shell. A count of killed group members can also go up when the whole machine runs short and Linux kills one of them.
5:25 I'd match the group's limit and its out-of-memory event with the kernel's recorded messages, called the kernel log. Suppose the kill count was already one before this run. A process was killed earlier. I'd want to see whether the count changed around our worker's exit. If the worker starts again in a new group, can I still find out what happened in the old one? With a restart into another group, you get fresh readings. Save the old limit and the event file while they're still there. In the repeat on screen, the first reading came while the worker was still waiting to run. Both counts start at 0.
6:04 The group path on screen is where its folder sits among the Linux groups. That tells us which group's numbers we're looking at. Then we let the worker run. We waited for the kernel's message naming that group and the worker it killed before we took the second reading. Now both counts are one. We read them before and after the same kill. The machine had memory available, and the worker stopped at its own group's limit. Thanks for listening to Learning Podcasts.