1

There are copious posts on disabling Ubuntu swap completely. Is it possible to tell Ubuntu to allow but discourage swap usage?

I've installed Xubuntu on a laptop with 1Gb ram and a small SSD, turning it into a Samba server. In a worst case scenario free memory still can fit all data from a swap file:

~$ free -m
            total       used       free     shared    buffers     cached
Mem:           991        696        294         17         66        175
-/+ buffers/cache:        454        536
Swap:         1952        107       1845

I want to discourage discourage swap when free memory is available, because swap does a lot of IO writes that degrade SSD quickly.

Is it possible to prioritize RAM over swap or the choice is limited to "swap on/off"? What side effects if any does it cause?


Thank you for pointing out existing answers they are relevant but hard to find. User must know 90% of the answer to search for "how to configure swappiness". Question answered. Hope this question header helps indexing.

Stepan
  • 338

1 Answers1

3

Unless you have a pretty old SSD, halfway modern SSDs can handle lots of writes, so you don’t really need to do anything if you have a halfway modern SSD.

If you still want to reduce swapping, reduce swappiness (minimum value on modern Linux is 1, not 0, if you want to go all out, though 10 is the recommended minimum and 60 is the Ubuntu default). This can slow things down greatly (to the point that the system can freeze up for quite a while if under heavy memory pressure) when swapping finally begins, since little to nothing will be swapped out ahead of time.

To set swappiness, replace 10 below with the swappiness you want, 1 to 100, where lower values swap less frequently. Open a terminal (press Ctrl+Alt+T), and run:

sudo sysctl vm.swappiness=10
sudo nano /etc/sysctl.conf

Now, add a line to the bottom (or if there’s already a vm.swappiness line, edit that):

vm.swappiness = 10

Press Ctrl+O to save. Press Ctrl+X to exit nano. Close the terminal window.

Chai T. Rex
  • 5,193