# Linux swappiness - how do you set optimal value?



## ICPH (Oct 28, 2014)

Hello,

how do You set proper value of swappiness for the Linux server?

i mean how to meter the performance before/after change (its a webserver) to find optimal value..

-------

show current value:

cat /proc/sys/vm/swappiness

Set temporary new value

sysctl -w vm.swappiness=0

set permanently new value:

add it into /etc/sysctl.conf


----------



## raj (Oct 28, 2014)

http://askubuntu.com/questions/103915/how-do-i-configure-swappiness

Good info there.


----------



## drmike (Oct 28, 2014)

I've long ran with swappiness at 0.

Unsure what the other values are intending with swapping out to disk.  Disk is slow. Even SSD based stuff.

Long considered the overly aggressive caching in Linux, especially in desktop use to be rather broken/aggressive/annoying.


----------



## MannDude (Oct 28, 2014)

If I use less than 50% of my allocated RAM on a machine, which I normally do even during peak times, I set it to zero or 10 at the highest.


----------



## yomero (Oct 28, 2014)

drmike said:


> Unsure what the other values are intending with swapping out to disk.  Disk is slow. Even SSD based stuff.



This. Swap is bad, period. If you need to swap, then you need more ram.

Anyway, I use it at "1"


----------



## johnlth93 (Oct 28, 2014)

I usually just keep it at default value, rarely getting the need to be swapped


----------



## zzrok (Oct 28, 2014)

I much prefer this StackExchange topic over the Ubuntu topic: http://unix.stackexchange.com/questions/88693/why-is-swappiness-set-to-60-by-default.

The Ubuntu topic misrepresents how swapping works.  It doesn't work simply by amount of RAM consumed; the LRU algorithm makes it more nuanced than that.

Take note of this quote from the answer:



> As with any performance parameter, adjusting vm.swappiness should be based on benchmark data comparable to real workloads, not just a gut feeling.


The correct way to tune swappiness is to leave it at the default value and setup server monitoring that tells you when pages are actually being moved in to and out of swap.  Every time pages are moved from RAM to swap (swap out) is a time you effectively get extra memory for free.  This is when unused code and data in you programs is being moved out of RAM, making more space for used code, data, and file caching.  Only worry about tweaking swappiness when you start seeing a significant amount of pages moving from swap into RAM (swap in).  At that point, tweaking may not help much unless you are just on the edge of not having enough RAM.  You need to make sure any programs that manage their own memory aren't trying to use too much (like MySQL innodb cache) or you may just plain need more memory.

TL;DR: Leave swappiness alone. Monitor swap in and swap out.  Don't worry about swap out.  Worry about swap in.

Edit: improve description of what to do about swap in and add TL;DR.


----------



## sleddog (Oct 28, 2014)

drmike said:


> I've long ran with swappiness at 0.


Just a note that this has new meaning with newer kernels (3.5). It used to mean "Don't swap unless you have to." Now it seems to mean "Don't swap."

I use swappiness = 1

I first read of this here:

http://www.percona.com/blog/2014/04/28/oom-relation-vm-swappiness0-new-kernel/


----------



## zzrok (Oct 28, 2014)

sleddog said:


> Just a note that this has new meaning with newer kernels (3.5). It used to mean "Don't swap unless you have to." Now it seems to mean "Don't swap."
> 
> I use swappiness = 1
> 
> ...


A note on that, the 3.5 patch mentioned was backported to RHEL6's 2.6 kernel, so it applies there too, as mentioned in the Percona article.


----------



## drmike (Oct 28, 2014)

sleddog said:


> Just a note that this has new meaning with newer kernels (3.5). It used to mean "Don't swap unless you have to." Now it seems to mean "Don't swap."
> 
> I use swappiness = 1
> 
> ...


Good find there and yeppers, off to converting to 1 I go    Why? Because I use MySQL quite a bit on many things.  OOM condition with MySQL could be PITA fatal.


----------



## tonyg (Oct 28, 2014)

For servers my general rule is the less RAM the greater the need for swap, but I never disable it.

I typically follow the below:

256MB RAM or less  =  60 (Default Value)

512MB to 1GB  RAM =  40

Above 1GB RAM       =  30


----------



## splitice (Oct 29, 2014)

Your hard drive type is a very important factor, you don't want to be killing SSD's with swapping if you dont have to be.

I usually use a value of 1 on SSD servers, or when there is a normally excessive amount of ram. Its based on limited testing and experiences with the default values.

Values between 10 and 30 are probably sane in most other cases (dedicated servers).


----------

