I've trying to limit the amount of memory a user can use using cgroups (using Ubuntu 16.04 with systemd).
After setting the MemoryLimit property of user-.slice I can see the change reflected in its cgroup's memory.limit_in_bytes.
However, when that user runs a VirtualBox VM, even though VirtualBox shows as using 2GB of RAM (see ps output below), that memory isn't reflected in the cgroup's memory.usage_in_bytes file:
$ cat /sys/fs/cgroup/memory/user.slice/user-1001.slice/memory.usage_in_bytes
1353863168
I can see the VirtualBox process id in the cgroups tasks file:
$ ps un -p 19678
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
1001 19678 97.4 29.4 4660560 2378040 ? Sl 07:25 3:48 /usr/lib/virtualbox/VirtualBox
$ grep 19678 /sys/fs/cgroup/memory/user.slice/user-1001.slice/tasks
19678
The memory.stat file for the cgroup shows the following values:
$ cat /sys/fs/cgroup/memory/user.slice/user-1001.slice/memory.stat
cache 423411712
rss 880754688
rss_huge 331350016
mapped_file 176910336
dirty 307200
writeback 0
pgpgin 14357052
pgpgout 14129610
pgfault 14533957
pgmajfault 16595
inactive_anon 546553856
active_anon 447401984
inactive_file 151785472
active_file 158326784
unevictable 36864
hierarchical_memory_limit 5368709120
total_cache 423411712
total_rss 880754688
total_rss_huge 331350016
total_mapped_file 176910336
total_dirty 307200
According to memory.txt, rss + cache should give a more accurate version of memory.usage_in_bytes. The result is 1304166400, which is even lower.
So why is VirtualBox's memory left out?
EDIT: added detailed output and memory.stat file.