3

I want to set swapiness to the minimum value such that Ubuntu 16.04 will still run without crashing. How can I do that?

Here is the output of ls -alt /var/crash:

drwxrwsrwt  2 root whoopsie    4096 Jul 26 14:12 .
-rw-r--r--  1 root whoopsie    1928 Jul 26 14:12 virtualbox-4.3.0.crash
-rw-r-----  1 some_user   whoopsie 3978513 Jul 26 14:02 _usr_bin_compiz.1000.crash
drwxr-xr-x 15 root root        4096 Aug  8  2016 ..
12
  • How much RAM do you have? How much swap? Edit your question and show me the terminal output of ls -alt /var/crash.
    – heynnema
    Aug 4, 2017 at 23:27
  • @heynnema I have 8GiB of RAM on this particular machine. However, I will be migrating to a new machine that has 16-32GiB of RAM. Aug 4, 2017 at 23:33
  • You didn't say how much swap?
    – heynnema
    Aug 4, 2017 at 23:34
  • I think a better question is why is Ubuntu crashing ? Swapiness should not cause a rash
    – Panther
    Aug 4, 2017 at 23:34
  • @bodhi.zazen that's why I'm asking the questions that I'm asking. And... Ubuntu isn't crashing.
    – heynnema
    Aug 4, 2017 at 23:35

3 Answers 3

9

It depends on how much physical memory your Ubuntu have, we can ask ourself that why should I use a high swappiness value when I got a lot of memory?

You can also experiment to see what is going to be the best for your situation, however I don't suggest you to disable swapping using "0", use something like "10" to improve performance. if you want something close to "0" then go with "1".

How the numbers work

The parameter value set to “60” means that your kernel will swap when RAM reaches 40% capacity. Setting it to “100” means that your kernel will try to swap everything. Setting it to 10 (like I did on this tutorial) means that swap will be used when RAM is 90% full. [1]

In other words, "60" means start swapping when 60% of memory's capacity is left.

0 and 1

“1” is the minimum possible “active swapping” setting while “0” means disable swapping completely and only revert to when RAM is completely filled. [1]

recommendations?

Here's a good article to read including recommendations.

5
  • What about a number between 1 and 9? The goal of this isn't necessarily to maximize performance (although that is nice to have). I just want mediocre to decent performance with minimal swap usage. Aug 4, 2017 at 23:35
  • 1
    Why do you say "0" will disable swapping when your own source says it will still swap to avoid out-of-memory conditions?
    – MadMike
    Aug 5, 2017 at 4:57
  • @MadMike The 0 means that the kernel avoids swapping at all except for when system is going to be crashed, so it's like being disabled... may other sources call it that way too, also I updated the answer ;)
    – Ravexina
    Aug 5, 2017 at 6:17
  • @BrianSchmitz I updated the answer ... it's should be more clear how to choose a number now.
    – Ravexina
    Aug 5, 2017 at 6:18
  • Read the latest Linux kernel source code documentation on swappiness here. The max is now 200 not 100, and 0 means "At 0, the kernel will not initiate swap until the amount of free and file-backed pages is less than the high watermark in a zone."--whatever that means. Dec 13, 2022 at 7:43
3

There is no real answer to your question as always "it depends".

Swap does not really affect performance directly and it depends on what you are doing.

In general, your kernel is already optimized for performance and it is unlikely you will gain any performance by tweaking the values.

With regard to swap, the question is how much RAM do you have and how much do you use ?

If you are using more ram then you have you will use swap and you will take a performance hit. However, if you do not have swap available, and you run out of RAM, your programs will crash or lock up or you will loose data, hard to say.

If you are taking a performance hit due to swap the solution is to buy more RAM not disable swap.

Performance can be affected by graphics cards or other hardware including poor performing wireless drivers (or any kernel driver really).

To debug your performance question we would need to know your hardware , what you are doing, what makes you think you have a performance problem, and probably benchmarks before and after you make your tweaks to swap or whatever.

On my system I have 4 GB of RAM and with normal desktop activities such as running a browser , word processing, email, etc I never run out of RAM

bodhi@daemon:~$free -m
              total        used        free      shared  buff/cache   available
Mem:           5403        2558         739          77        2106        2398
Swap:             0           0           0

In fact I am not even using a swap partition.

If, however, I want to fire up a Virtual Machine, and give it 1 GB of RAM, you bet I need swap, so in that event I mount my swap partition.

On my big server I run multiple virtual machines, but I have 16 Gb RAM. With all my VM I hit swap every now and again, but not much. Certainly better to use swap then to have a VM crash.

So it depends .... server side, it I hit my swap to the point where performance of the VM were slow I would buy more RAM. desktop side, I just don't need to run a VM enough to justify purchasing more RAM. Personal choice.

You need to analyze your usage and make your own choices. At this point we do not even know how much or how little you are limited by swap as opposed to other issues. If you have enough RAM, don't use swap at all and then it is a non-issue.

4
  • As mentioned earlier, I don't care about performance, per se. I just care about not crashing while performing basic functions in Ubuntu. It seems this limit is reached when swap and RAM are both exhausted, so as long as I don't exhaust RAM and have a small amount of swap for some essential Linux functions, I should be good. Aug 5, 2017 at 4:14
  • My particular machine will have 16 to 32GB of RAM. The most memory intensive tasks will be downloading very large videos and playing very large videos. My understanding, however, is that Linux will use swap space no matter how much available RAM you have. I don't have a strong understanding of what the various values of vm.swappiness imply. Aug 5, 2017 at 4:17
  • looks like this answers my question: askubuntu.com/questions/103915/how-do-i-configure-swappiness Aug 5, 2017 at 4:19
  • 1
    Swapping sadly also happens for processes not used for a while (EVEN IF YOU HAVE LIKE 5GB RAM FREE!), I strongly suggest to decrease vm.swappiness to 10 or maybe even just 1. If you are constantly running OUT OF RAM, then yes, buy more RAM (if possible), if you are constantly hitting SWAP but are NOT out of RAM - decrease vm.swappiness. If you can't buy more RAM and you are running out of both RAM AND SWAP - but you have disk space - try searching on how to create a swap file (don't need partitioning!) - same goes if your SSD swap is behaving funky.
    – jave.web
    Feb 1, 2021 at 12:17
-1

Ubuntu will not crash with swappiness=1, because it will still use the swap file, just avoid it at all costs.

It will actually degrade performance compared to a reasonable swappiness.

In fact, a swappiness value of 1 will keep ubuntu from swapping out until the very last possible moment. Which means delays and possible crashes when starting a new application with nearly full memory. Because it needs to swap out a lot at once to fit the new application. Then, with memory completely full every single page miss would cause a new page to be swapped in, possibly multiple times a second degrading performance even further

The stuff ubuntu swaps out first are unused code (app entrypoint) and cached files first, things that need to be "in memory" but may never be accessed again, so forcing this behavior is completely unnecessary.

The Kernel is very good at managing its memory, if you are thrashing swap with default settings you should buy more RAM or run less things.

On the other hand, Its common on embedded systems to run without swap. Instead of messing with swappiness you should just go all in and eliminate the swap file/partition to convince yourself it isn't that great.

Eliminating swap will simply prevent the application from being started if there isn't enough memory. Which may be desirable in certain scenarios.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .