/proc/sys/vm/swappiness is nice, but I want a knob that is per process like
/proc/$PID/oom_adj. So that I can make certain processes less likely than others to have any of their pages swapped out. Unlike
memlock(), this doesn't prevent a program from being swapped out. And like
nice, the user by default can't make their programs less likely, but only more likely to get swapped. I think I had to call this
You can configure swappiness per cgroup:
For an easier introduction to cgroups, with examples, see
I would like to extend Šimon Tóth's answer with a real life solution that I have developed. I think that it deserves a separate answer.
RedHat 7 - cgroups v1 and systemd
As only since RedHat 8 the cgroups v2 are supported, we need to use cgroups v1 here.
The official guide suggests adding a
ExecStartPost that changes memory settings for your service, but it does not work until you make another change that will make
So I ended up with adding this to my service unit file, assuming that the service name is
# the first entry is here only to make /sys/fs/cgroup/memory/system.slice path # available for the next entry MemoryLimit=64G # this entry actually effectively disables swap for the service ExecStartPost=/bin/bash -c \ "echo 0 > /sys/fs/cgroup/memory/system.slice/example.service/memory.swappiness"