Linux内存参数`swappiness` # 与cgroup关系 `swappiness`是cgroup v1的特性,在cgroup v2已经被替代。 > swapiness is a cgroupvs1 feature, and it has no counterpart on cgroupsv2. (the new latency stuff is maybe a better replacement though). `swappiness`实现很糟糕,systemd不打算再维护它了。 # swappiness作用范围 Similar to /proc/sys/vm/swappiness, but affecting a hierarchy of groups only. Following cgroups' swappiness can't be changed. \- root cgroup (uses /proc/sys/vm/swappiness). \- a cgroup which uses hierarchy and it has other cgroup(s) below it. \- a cgroup which uses hierarchy and not the root of hierarchy. # 关于swappiness=0 Note that a value of 0 does not prevent process memory being swapped out; swap out might still happen when there is a shortage of system memory because the global virtual memory management logic does not read the cgroup value. To lock pages completely, use mlock() instead of cgroups. swappiness设置为0就相当于关闭swap么? 不是的,关闭swap要使用swapoff命令。swappiness只是在内存发生回收操作的时候用来平衡cache回收和swap交换的一个参数,调整为0意味着,尽量通过清缓存来回收内存。 # 参考 * [Turning off swapping for only one process with cgroups?](https://unix.stackexchange.com/questions/77939/turning-off-swapping-for-only-one-process-with-cgroups) * [system.slice swappiness is inconsistent with vm.swappiness sysctl](https://github.com/systemd/systemd/issues/9276) * [Add knob for vm.swappiness to systemd.resource-control](https://github.com/systemd/systemd/issues/17598) * [我就是认真:Linux SWAP 深度解读](https://mp.weixin.qq.com/s?__biz=MzA4Nzg5Nzc5OA==&mid=2651660097&idx=1&sn=a3d38e3af2c9d8d431c46fe7680b428d&scene=2&srcid=0606f21oK1jm1IKMwEyi6aNz&from=timeline&isappinstalled=0#wechat_redirect)