18

I was reading about swappiness in various forums and I saw some videos. Many stated that it's a good thing for desktop users to decrease the swappiness value below 60 (which is the default). Some stated that 60 is good for servers. Exactly why would it be better for me to lower it and why is 60 good for servers? I tried to search for an answer (I looked here for example) but couldn't understand. ...Please use easy terms :D

2

4 Answers 4

22

Your premise is wrong. There is no 'good for servers' swapiness value.

Swapiness is the amount of swapping that is done, especially how aggressively you swap. For my servers the swappiness is close to zero, as I make damn sure they have memory enough for everything. There is no 'best for servers' value. there is 60, which is the default, and then there is "your value" which depends on usage and available memory.

If you have servers that sometimes use LOADS of memory, you could have them higher than default, but I would say that some servers (database servers?) might want to keep them way lower than a home computer would.

7
  • well okay :D I was following this unix.stackexchange.com/questions/88693/… . the second answer (the one currently with 2 upvotes) said that 60 may be most suitable for servers. ... And let me get this straight. I should set a high swappiness if I do processes that require large memory and low ones if I do multiple small processes?
    – 842Mono
    Feb 12, 2014 at 18:25
  • (and depending on my computer's specs ofcourse) I have a core 2 duo about 2.9 GHz with intel integrated graphics by the way
    – 842Mono
    Feb 12, 2014 at 18:29
  • 1
    Impossible to say. Depends on a lot of things, as also stated in that answer -> some databases might do their own caching, but some might not. It depends on the programs and on your hardware. There is a default and the best thing you can do is test how your system performs under what situation. As said, I have added enough mem to my systems, and I know what they require, so I can have swappiness at near 0. But on healthy systems some swapping can be a good thing. there is no easy 'god for all' setup advice here, read the best-upvoted answer. Also: it says "may be", not "is" in that 2nd answer
    – Nanne
    Feb 12, 2014 at 18:29
  • (and at your specs: the most important thing if you are talking the memory you have and you, but no specific need to add it here: we could talk for days about all sorts of specifics, the best thing to do is to read the highest answer on that unix link you gave, and continue on with testing how it works in your environment.)'
    – Nanne
    Feb 12, 2014 at 18:31
  • well that's when I said that I couldn't understand what I've read. I'm fairly weak on knowledge (and even language to be honest XD) ... so I just need to keep trying! okay thanks :D
    – 842Mono
    Feb 12, 2014 at 18:49
13

The swappiness value is scaled in a way that confuses people. They see it has a range of 0-100 so they think of percent, when really the effective minimum is around 50.

60 roughly means don't swap until we reach about 80% memory usage, leaving 20% for cache and such. 50 means wait until we run out of memory. Lower values mean the same, but with increasing aggressiveness in trying to free memory by other means.

The describe it pretty well at http://lwn.net/Articles/83588/

2
  • What do higher values mean, e.g. 80 or 100?
    – jocull
    Aug 1, 2019 at 18:15
  • 3
    The linked article is excellent to understand what's going on!
    – Tim
    Feb 4, 2020 at 18:44
6

The swappiness setting is a tweak to the way Linux calculates the benefits/drawbacks to swapping in a particular scenario. A high swappiness will not cause the system to swap when it doesn't need to, and a low swappiness, providing you don't disable swapping completely, will not prevent your system from swapping when it absolutely has to.

The default swappiness of 60 or so is a very good compromise ensuring good performance of your system by ensuring it is not struggling too hard to allocate cache memory while there is unused allocated program memory.

Remember that if you have plenty of RAM for all your processes plus cache, which should usually be the case if you have plenty of physical RAM, swapping should not be an issue for you anyway. The preferred way to fix issues of too much swapping is to decrease RAM use by processes, and/or to increase physical RAM. Reducing swappiness will have a negative effect if what you really need is more RAM.

Please see the answer I wrote to a very similar question:

https://askubuntu.com/a/184221/54256

Excerpt:

Why most people recommend to reduce swappiness to 10-20?

Because most believe that swapping = bad and that if you don't reduce swappiness, the system will swap when it really doesn't need to. Neither of those are really true. People associate swapping with times where their system is getting bogged down - however, it's mostly swapping because the system is getting bogged down, not the other way around. It's true that there are certain times when swapping can have a noticeable performance penalty, but the system will have already factored that in to its decision to swap, and decided that not doing so would have a greater overall penalty in system performance or stability which may later become noticeable. Overall the default settings should result in the best overall performance and reliability. I'd recommend leaving it at the default.

...

Once you have used up enough memory that there is not enough left for a smooth-running cache, Linux may re-allocate some unused application memory from RAM to swap.

It doesn't do this according to a definite cutoff though. It's not like you reach a certain percentage of allocation then Linux starts swapping. It has a rather "fuzzy" algorithm. It takes a lot of things into account, which can best be described by "how much pressure is there for memory allocation". If there is a lot of "pressure" to allocate new memory, then it will increase the chances some will be swapped to make more room. If there is less "pressure" then it will decrease these chances.

Your system has a "swappiness" setting which helps you tweak how this "pressure" is calculated. It's normally not recommended to alter this at all, and I would certainly never recommend you alter it. Swapping is overall a very good thing - any occasional performance penalties are intended to be offset by a gain in overall system responsiveness and stability for a wide range of tasks. If you reduce the swappiness, you let the amount of cache memory shrink a little bit more than it would otherwise, even when it may really be useful. You therefore risk slowing down your computer in general, because there is less cache, while memory is being taken up by applications that aren't even using it. Whether this is a good enough trade-off for whatever specific problem you're having with swapping is up to you. If you go further than this and actually disable swapping, you risk system instability in the event that the system runs out of memory for processes.

What is happening when the system is bogged down and swapping heavily?

A lot of the time people will look at their system that is thrashing the disk heavily and using a lot of swap space and blame swapping for it. That's the wrong approach to take. If swapping ever reaches this extreme, it means that your system is extremely low in memory and the swapping is the only thing keeping it from crashing or killing processes randomly. Without swapping, in this situation, processes will crash and die. The swapping is a symptom of a deeper problem. In a system with enough memory for all its tasks, swapping only ensures that memory is utilised in efficient ways, dealing out memory to the cache over dormant processes where it sees it will be worth it. In a system where swapping is relentlessly thrashing the disk, it's not swapping's fault.

When choosing what is to be swapped to disk, the system tries to pick memory that is not actually being used - read to or written from. It has a pretty simple algorithm for calculating this that chooses well most of the time.

...

But how can swap speed up my system? Doesn't swapping slow things down?

The act of transferring data from RAM to swap is a slow operation, but it's only taken when the kernel is pretty sure the overall benefit will outweigh this. For example, if your application memory has risen to the point that you have almost no cache left and your I/O is very inefficient because of this, you can actually get a lot more speed out of your system by freeing up some memory, even after the initial expense of swapping data in order to free it up.

Feel free to read the whole post at https://askubuntu.com/a/184221/54256

0

Swappiness is completely contradicting to all these answers.

Real Working of swappiness is described in the article: https://gokulvasanblog.wordpress.com/2016/11/14/empathising-mammoths-mentality-a-study-on-linux-memory-management/

The ambiguity is corroborated by the statement from Andrew Morton: https://lwn.net/Articles/83593/

I'm gonna stick my fingers in my ears and sing "la la la" until people tell me "I set swappiness to zero and it didn't do what I wanted it to do".

Hope its useful

1
  • 1
    Welcome to AU. Could you summarize the key points of the article you point to and explain how it answer the question, which is not to explain what swappiness is or not. Feb 16, 2018 at 17:51

You must log in to answer this question.

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