linux poison RSS
linux poison Email

How to Tune Swap Setting on Linux for Maximum Performance

HANDLE WITH CARE

Since 2.6, there has been a way to tune how much Linux favors swapping out to disk compared to shrinking the caches when memory gets full.

ghoti adds: "When an application needs memory and all the RAM is fully occupied, the kernel has two ways to free some memory at its disposal: it can either reduce the disk cache in the RAM by eliminating the oldest data or it may swap some less used portions (pages) of programs out to the swap partition on disk. It is not easy to predict which method would be more efficient. The kernel makes a choice by roughly guessing the effectiveness of the two methods at a given instant, based on the recent history of activity."

Before the 2.6 kernels, the user had no possible means to influence the calculations and there could happen situations where the kernel often made the wrong choice, leading to thrashing and slow performance. The addition of swappiness in 2.6 changes this. Thanks, ghoti!

Swappiness takes a value between 0 and 100 to change the balance between swapping applications and freeing cache. At 100, the kernel will always prefer to find inactive pages and swap them out; in other cases, whether a swapout occurs depends on how much application memory is in use and how poorly the cache is doing at finding and releasing inactive items.

The default swappiness is 60. A value of 0 gives something close to the old behavior where applications that wanted memory could shrink the cache to a tiny fraction of RAM. For laptops which would prefer to let their disk spin down, a value of around 30 or less is recommended.

You can see what the current ( default ) swappiness is by doing:

 # cat /proc/sys/vm/swappiness

As a sysctl, the swappiness can be set at runtime with following command:

 # echo 30 >/proc/sys/vm/swappiness

The default when linux boots can also be set in /etc/sysctl.conf:

vm.swappiness = 30

Some patchsets allow the kernel to auto-tune the swappiness level as it sees fit; they may not keep a user-set value.


3 comments:

blink4blog said...

when I tried $ sudo echo 30 > /proc/sys/vm/swappiness I got myself access denied.

DevOps said...

Which distribution you are using?
also can you try it using the "root" user instead of "sudo"

mike said...

you can also use this command instead of echo

sudo sysctl -w vm.swappiness=30

Post a Comment

Related Posts with Thumbnails