Hey, Something that is very common to get wrong when starting with Linux containers is to think that free and other tools like top should report the memory limits. Here you’ll not only go through why that happens and how to get it right, but also take a look at where is the Kernel looking for information when you ask it for memory statistics. Also, if you’re curious about how the code for keeping track of per-cgroup page counter looks, stick to the end! Running top within a container To get a testbed for the rest of the article, consider the case of running a single container with a memory limit of 10MB in a system that has 2GB of RAM available: # Check the amount of memory available # outside the container (i.e., in the host) free -h total used free available Mem: 1.9G 312M 385M 1.5G # Define the total number of bytes that # will dictate the memory limit of the # container. MEM_MAX="$((1024 * 1024 * 10))" # Run a container using the ubuntu image # as its base image, with the memory limit # set to 10MB, and a tty as well as interactive # support. docker run \ --interactive \ --tty \ --memory $MEM_MAX \ ubuntu With the container running, we can now check what are the results from executing top over there: top -bn1 Tasks: 2 total, 1 running, 1 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 0.1 sy, 0.0 ni, 99.7 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st .----------------. | | KiB Mem :| 2040940 total, | 117612 free, 651204 used, 1272124 buff/cache KiB Swap:| 0 total, | 0 free, 0 used. 1196972 avail Mem *--+-------------* PID USER | PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root | 20 0 18508 3432 3016 S 0.0 0.2 0:00.02 bash 12 root | 20 0 36484 3104 2748 R 0.0 0.2 0:00.00 top | *---> Not really what we expect, that is 2GB!! As we outlined before, not what one would typically expect (it reports the total available memory as seen in the host - not showing the 10MB limit at all). What about free? Same thing: free -h total used free available Mem: 1.9G 612M 131M 1.2G Swap: 0B ...
First seen: 2025-08-14 01:09
Last seen: 2025-08-14 04:10