You could compare the total physical memory available to the number cgget
gives you. If the number given by cgget
is lower than the total physical memory, then you know for sure cgroups where used to limit the memory.
For instance if I run a container limiting the memory to 100M on my computer which has 2G of physical memory, cgget
will report 104857600
while the free
command reports 2098950144
bytes:
On the docker host :
# free -b
total used free shared buffers cached
Mem: 2098950144 585707520 1513242624 712704 60579840 367644672
-/+ buffers/cache: 157483008 1941467136
Swap: 3137335296 0 3137335296
Start a container limited to 100M
docker run --rm -it --memory=100M <any-image-with-cgget-available> bash -l
Now within that container:
# free -b
total used free shared buffers cached
Mem: 2098950144 585707520 1513242624 712704 60579840 367644672
-/+ buffers/cache: 157483008 1941467136
Swap: 3137335296 0 3137335296
# cgget -n --values-only --variable memory.limit_in_bytes /
104857600
Note that the free
command will report the same values on the docker host as from within the containers.
In the end, the following bash script defines a function is_memory_limited
that can be used in a test to check if cgroup where used to limit the memory.
#!/bin/bash
set -eu
function is_memory_limited {
type free >/dev/null 2>&1 || { echo >&2 "The 'free' command is not installed. Aborting."; exit 1; }
type cgget >/dev/null 2>&1 || { echo >&2 "The 'cgget' command is not installed. Aborting."; exit 1; }
type awk >/dev/null 2>&1 || { echo >&2 "The 'awk' command is not installed. Aborting."; exit 1; }
local -ir PHYSICAL_MEM=$(free -m | awk 'NR==2{print$2}')
local -ir CGROUP_MEM=$(cgget -n --values-only --variable memory.limit_in_bytes / | awk '{printf "%d", $1/1024/1024 }')
if (($CGROUP_MEM <= $PHYSICAL_MEM)); then
return 0
else
return 1
fi
}
if is_memory_limited; then
echo "memory is limited by cgroup"
else
echo "memory is NOT limited by cgroup"
fi