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
$ 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:


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)?

  • 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 5:20
  • @Atul That is what I thought too, but my understanding is that vm.swappiness should prevent overzealous swapping. – Sparhawk Jan 30 '19 at 5: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 7: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 9:07
  • 3
    Possible duplicate of How do I use swap space for emergencies only? – Philip Couling Apr 8 '19 at 23:54

Your Answer

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

Browse other questions tagged or ask your own question.