3

In the past, setting vm.swappiness to 0 meant the system would only swap when RAM was totally full.

$ cat /etc/sysctl.d/99-sysctl.conf
kernel.sysrq = 1
vm.swappiness=0
$ sysctl vm.swappiness
vm.swappiness = 0

However, recently now this setting still allows swapping, even seconds after clearing swap with sudo swapoff -a; sudo swapon -a. For example, after a few hours,

$ free -m
        total   used   free   shared  buff/cache  available
Mem:    32047   7914    325     1509       23807      22177
Swap:    1974   1974      0

Hence, I have 22177 MiB available. From man free:

available

Estimation of how much memory is available for starting new applications, without swapping.

However, my system is still swapping heavily, using 1974 MiB of swap space and slowing everything down.

I've also tried setting vm.swappiness to 1, with similar results. I've also tried different kernels, including the recent LTS 4.19.17-1, 4.19.16-1-lts, 4.19.15-1-lts, and the many other LTS versions, and the mainline 4.18.16.arch1-1. How can I prevent my system swapping unnecessarily?

Apparently setting vm.swappiness = 0 on newer versions of the kernel means it will not "initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone." I'm not sure what this "high water mark" is, but I have a lot of free memory! Another page suggests that the OOM killer will be invoked when this occurs (not swapping itself). Is it possible to prevent swapping unless there is no RAM, and prevent any OOM killing from occurring (unless both RAM and swap are full)?

Sparhawk
  • 19,941
  • I think swap doesn't always comes into play if the RAM is consumed. Swapping also takes care of memory management by taking off load of pages which are not necessarily needed to be in main memory. This allows the RAM to be available for other things and swap is the best next buffer available if there is a need to load something back into main memory. – Atul Jan 30 '19 at 05:20
  • @Atul That is what I thought too, but my understanding is that vm.swappiness should prevent overzealous swapping. – Sparhawk Jan 30 '19 at 05:50
  • Are you seeing performance degradation? If not, i think its better to trust the kernel and let it manage the swapping. – Atul Jan 30 '19 at 07:32
  • I'm seeing massive performance degradation. As per the question, 20% of my used memory is in swap. This slows the system down to a crawl. "Trust the kernel" isn't such a good recommendation, because (my understanding is) the kernel should respect vm.swappiness. – Sparhawk Jan 30 '19 at 09:07
  • I'm also having this problem. I've set up vm.swappiness = 10 (confirmed via /proc/sys/vm/swappiness. However, after the latest kernel upgrade I see swap being used a lot more (when ~8GB of RAM is used on my 16GB laptop). I have also seen massive performance degradation.

    Another issue is, if you have an SSD, it is best for the life of the disk to minimize writes.

    – Mario Camou Feb 08 '19 at 13:53
  • This seems to have started happening after the latest kernel update (Ubuntu 18.10, kernel 4.18.04-14-generic #15-Ubuntu) – Mario Camou Feb 08 '19 at 13:57
  • How are you checking the swap activity? When running vmstat 1, are the si (swap in) and so (swap out) metrics actually very active? – spuk Mar 21 '19 at 15:26
  • @spuk I've just been checking with htop. I'm away at the moment, but I'll test vmstat 1 on Monday. – Sparhawk Mar 21 '19 at 23:00
  • @spuk There is 1.33 G swapped out today (and 21 GB available). For some reason performance is only moderately affected today. The numbers from vmstat 1 show that si and so are indeed active, if fairly sporadic (max of ~600 and 64 respectively). – Sparhawk Mar 24 '19 at 20:53
  • It is normal for vmstat's si (memory pages being read from swap) and so (memory pages being written to swap) to be sporadically active. When you say "swapping heavily" it gives the impression the system is heavily writing and reading pages all the time, which could be a cause of degraded performance, but simply having the swap space being used with sporadic pages written/read shouldn't be. Also, I'm not sure swappiness=0 prevents the use of swap.

    If you really don't want to use swap, you might just disable/remove it.

    – spuk Apr 08 '19 at 20:09
  • @spuk After leaving the system for a few days I certainly experience degraded performance. I do swapoff -a; swapon -a to clear the swap, and performance is perfect. The system is on so I can ssh from home, but I leave swap on in case some process runs riot with memory and crashes the system. Regarding swappiness=0 it looks like you are right! It used to mean that, but has changed. I've edited the question to add more information. – Sparhawk Apr 08 '19 at 23:12
  • 4
  • Can we leave this open please. It is a good question. It references specific kernel versions. swappiness=0 has been documented that it should not be swapping when the kernel itself reports that 22G out of 32G RAM is "available". – sourcejedi Apr 10 '19 at 09:15
  • If you would be interested in having logs of memory & swap usage - I find atop very useful. It includes a service you can enable to start automatically, which logs snapshots every ten minutes. – sourcejedi Apr 10 '19 at 20:13
  • @sourcejedi I'm not entirely sure that more recent versions suggest that at all. I don't really understand the documentation TBH. Thanks for the suggestion of atop; that seems useful. – Sparhawk Apr 11 '19 at 01:04
  • @Sparhawk the "new" part of the documentation was added five years ago, after the swappiness = 0 special-case was implemented. Before then, "reclaim code could still evict recently used user anonymous memory to swap even though there is a significant amount of RAM used for page cache". https://github.com/torvalds/linux/commit/8582cb96b0bfd6891766d8c30d759bf21aad3b4d – sourcejedi Apr 11 '19 at 07:55
  • @sourcejedi I'm not certain how to parse that output. Which line is relevant? – Sparhawk Apr 14 '19 at 03:46
  • @Sparhawk The watermarks are only small reservations, about 1% I think. I wrote a QA, does it help? https://unix.stackexchange.com/questions/512378/how-significant-are-the-watermarks-on-my-system/512379 – sourcejedi Apr 14 '19 at 09:33

0 Answers0