In CentOS 7 how do I set the default memory.swappiness for all the systemd cgroups? I can do it per cgroup via the ControlGroupAttribute option, but I would like to override the default of 60 for all the cgroups.


If you want to do this for ALL cgroups, do you want to do this for the whole system? If that's the case, you can set swappiness system wide in "/etc/sysctl.conf". The line you would be editing is "vm.swappiness=" which you can set to anything from 0 to 100.

As a fair warning to anyone moving this value as a way to avoid swapping altogether, setting this very low will almost entirely disable demand paging, which is a very useful way to get idle pages out of RAM and into swap. Yes, it's "swapping", but that's not such a desperate thing as is the old traditional sense of swapping when we run out of memory. Demand paging is especially useful in large databases, and it shouldn't really slow anything down. It certainly beats swapping things out when we're out of memory last second, as demand paging helps to keep that from happening in the first place while the system isn't grinding to a halt, rather than after it is. A setting of 0 for vm.swappiness system wide can cause a system to OOM kill processes when under heavy pressure, potentially bringing the whole system down in a kernel panic.

Also worth mentioning, you must call upon the sysctl.conf file to be read after editing it. # sysctl -p reads lines that have been modified in /etc/sysctl.conf. vm.swappiness can be changed on a running system this way, or by using procfs thusly: # echo 50 > /proc/sys/vm/swappiness where 50 is any number from 0 to 100.

| improve this answer | |
  • 1
    I already have vm.swappiness set to zero and what I am see is some processes (mesos) swapping out a lot that have their cgroup swappiness set to 60. So I don't think vm.swappiness takes precedence over the cgroups setting, does it? – Jeff Kubina Sep 27 '16 at 13:25
  • 1
    I'm looking through kernel documentation. I see that a top-hierarchy cgroup does honor the global vm.swappiness level. However, I don't see any mention of child cgroups. I don't see why it wouldn't carry forth, but I need to chew on more documentation and probably spin up a little lab real quick. I know that memory.swappiness isn't a setting you may use on root cgroups with children. Also, this scenario may have already been addressed in unix.stackexchange.com/questions/77939/… – Spooler Sep 27 '16 at 15:58
  • My scenario was not addressed in the unix.stackexchange link, but it got me to even more documentation and to having more questions. 1) The default swappiness for cgroups is 60, but how do I change that system wide for all existing and/or new cgroups? 2) On my system vm.swappiness=0 but "cgget -r memory.swappiness /" returns 60, shouldn't it return zero? 3) From the cgroup/memory docs the "limit reclaim enforces that 0 swappiness really prevents from any swapping even if there is a swap storage available"; so wrt cgroups does that mean setting memory.swappiness=0 turns swapping off? – Jeff Kubina Sep 27 '16 at 18:39
  • I am wrong on 2), I just verified that doing "sysctl -w vm.swappiness=n" sets /sys/fs/cgroup/memory/memory.swappiness to n, and vise versa. – Jeff Kubina Sep 27 '16 at 18:51
  • 1
    sysctl -w only changes the running values on your system. It does not make persistent entries, so changes made with -w will be lost on reboot. If you want changes to persist, update the /etc/sysctl.conf file with your desired values as well as updating your running values (as the sysctl.conf file does not control things in real time). – Spooler Sep 28 '16 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.