Page 1 of 1

Swappiness - Improve your system's performance.

PostPosted: Mon Aug 05, 2013 12:51 pm
by tanmay.01
Question : I have heard that changing the swappiness of my system will improve performance. How does this work and what is a good setting for swappiness?

Answer : Let us imagine for a moment that on our Linux system we have one large pool of memory and this pool is divided into two parts. One part of the memory pool is our machine's RAM. RAM is very fast and in high demand, but there usually isn't a lot of it. The other part of the memory pool is called swap space. Swap space resides on the hard disk which is slow and typically in low demand.

In an ideal situation we would have lots of RAM and everything we need, applications and files, would stay in RAM where it would be accessed amazingly quickly. However, since RAM is relatively small and the hard disk relatively large, not everything we need will fit into RAM. This means the Linux kernel needs to make some tough choices. When the operating system starts to run out of room in RAM should the Linux kernel keep copies of files in memory and send the applications we are using into the slower swap space? Or should the kernel keep our programs in memory and drop files that it has copied into memory for quick access? It's a puzzle. Some applications will benefit from having files stored in RAM, but all processes (especially desktop programs we want to have respond quickly) will benefit from being kept in RAM too. Really, we want to rely on accessing the disk (and swap space) as little as possible.

What the swappiness parameter does is let us tell the Linux kernel our preference. Swappiness is expressed as a number from 0 to 100, inclusive. A high swappiness value (near 100) means that unused process memory should get removed from RAM and sent to swap space as much as possible. A low swappiness value (near 0) means the kernel will attempt to keep programs in memory and, if it needs RAM, cached files will be dropped from memory to free up space. Typically, on a desktop machine, we want a relatively low swappiness value. This means applications such as Firefox and our music player stay in memory and should remain responsive. To see what swappiness value our kernel is currently using we can run

cat /proc/sys/vm/swappiness

Most modern distributions set the default swappiness value to the mid-range, typically 60. Often people running Linux on desktop/laptop machines like to set swappiness to a lower value. So how do we adjust the swappiness of the Linux kernel? First let's look at changing the level of swappiness on a running machine. To set our machine's swappiness level to 10, where applications are less likely to get transferred out of RAM, we can run the following at a command prompt as the root user:

Code: Select all
nano /etc/sysctl.conf


vm.swappiness=10

To return the swappiness value to 60 we can run

vm.swappiness=60

Should we wish to make our new swappiness level permanent we can add a line to the text file /etc/sysctl.conf. This line will indicate the new swappiness value for the next time the system is booted. In this example I set swappiness to 10:

vm.swappiness=10

Most people will probably get along fine with the defaults, especially on a modern computer with lots of RAM. Swappiness is less of an issue if RAM isn't getting filled up, so if you have 8GB of RAM and only need 2GB for applications, swap space may not be used at all. On the other hand, if you have 1GB of RAM and you find your system is using swap space, then lowering the swappiness value is probably a good idea. If you are not sure if your operating system is making use of swap space, run the free command

free

The free command will show you, on the bottom line, the amount of total swap space available, how much is being used and how much is currently free (unused). If the value in the "used" column is zero then swap space isn't being used and swappiness won't matter much. A value above zero means swap space is used and your system may benefit from being tuned.

Source : http://askubuntu.com/questions/103915/h ... swappiness

Re: Swappiness - Improve your system's performance.

PostPosted: Mon Aug 05, 2013 4:21 pm
by ryanvade
Very nice tutorial.

Also, if anyone finds they need some swap space but do not want to mess with their partitions they can make a swap file:
http://www.cyberciti.biz/faq/linux-add- ... ile-howto/