3

I just stumbled upon a random question that goes like, does the Linux kernel swap out any memory pages even when there are still some available memory spaces? I thought it does not in principle, but the Linux distributions still usually demand a dedicated swap partition when it's installed.

To put this in another way, can I unset the entire swap partition and still get the stable Linux system, given that I have a sufficient amount of main memory that I cannot exhaustively use?

3 Answers3

2

does the Linux kernel swap out any memory pages even when there are still some available memory spaces

Check out vim /proc/sys/vm/swapiness (on Ubuntu at least). This specifies how often swaps are done and can imply that swaps are done even when memory is available. The real reasoning to find an optimal value for this heavily depends on the way the OS works, the available memory, and the processor itself. (My swapiness is specified at 60.)

From what I see in newer updates is that Linux automatically creates a /swapfile (which grows in size to 1-2 GBs) using available storage space, if no swap partition is specified. This does not explicitly exhaust your secondary storage but just makes your computer run smoother. Look at the output of ubuntu@ubuntu:/home/ubuntu$ swapon. Mine is:

NAME      TYPE      SIZE   USED PRIO  
/swapfile file      1.1G 123.5M   -2  
/dev/sda6 partition   2G   1.7G    1

This means you can pretty much "get the stable Linux system", without a swap partition.

The only exception is that a swap partition makes reloading semi-saved (or unsaved) information easier when your OS hibernate/crashes and you switch to another OS in between. (I am unsure but I think this is because the swap partition holds a /hiberfile.)

  • 1
    Thanks for the answer! Do you have any idea about why the kernel swaps the memory even if it doesn't have to? One reason I can think of is that it's for the exceptional situations when the main memory suddenly bloats. – Gwangmu Lee Jul 29 '20 at 01:55
  • 1
    Bloating memory is a good example. Look here for some detailed explanation: https://unix.stackexchange.com/questions/2658/why-use-swap-when-there-is-more-than-enough-free-space-in-ram. In general think about having to move a lot of data at once and continuously moving smaller chunks beforehand so as to not be caught off guard. – Sarwagya Jul 29 '20 at 22:30
1

does the Linux kernel swap out any memory pages even when there are still some available memory spaces?

In my experience it does that.

To put this in another way, can I unset the entire swap partition and still get the stable Linux system, given that I have a sufficient amount of main memory that I cannot exhaustively use?

I stopped using SWAP/pagefile circa 15 years ago when I first installed enough RAM in my PC (512MB at the time). If you have more than enough RAM for your needs, there's no need to have or use swap/pagefile at all.

I haven't had a single issue running swapless.

1

There is a kernel parameter vm.swappiness, which is responsible for the percentage of the memory usage limit, after which swapping occurs. You can see that

sysctl -a | grep vm.swappiness

in Ubuntu by default is 60%. You can change it like that

sysctl -p vm.swappiness=10

or swap off.

Do not forget that a running OOM killer will kill a process that you may not want to kill in a situation of insufficient RAM in the future.