Because RAM (Random Access Memory) is extremely faster than your HDD (or any other storage devices), when running programs, the operating system tries to keep their data on the RAM as long as possible for increasing the performance. However, because RAM is much more expensive thus it’s capacity is limited when compared with ‘cheaper’ storage options like the HDD as well.
So under heavy multitasking, the OS has to move some of the programs and their stored data in the RAM back into your HDD, temporarily. But for reducing its impact on the performance, the OS uses a special location (either a partition or a virtual file) in your HDD for storing that data, so it can access it faster than by using those programs’ original location on the HDD. In GNU/Linux, it’s called the ‘Swap space‘.
And because the OS uses the ‘Swap space’ as a virtual RAM, it’s one of the most important features that helps your PC to stay ‘stable’ (or ‘balanced’) under heavy load. Otherwise, it can easily crash the computer, simply because of not having enough free space on RAM while trying to load other programs.
When compared with MS Windows, GNU/Linux seems to be using the ‘Swap space’ less often (unless it’s terribly important). And depending on how much RAM you have (when compared with the ‘recommended’ RAM the OS needs etc), the OS automatically adjusts a value called ‘Swappiness’. It’s the main setting that can be used to either increase or decrease the ‘sensitivity’ or the ‘importance’ that the OS gives to ‘Swap space’.
For instance, if your PC doesn’t have a lot of RAM but you usually run few ‘heavy’ programs and would like the OS to make use of a bit more your ‘Swap space’ (which again, helps to increase the stability), then you can manually change the ‘swappiness’ value.
‘Swappiness’ uses numbers (from ‘0’ to ‘100’) to indicate how less or more aggressively the OS uses ‘Swap space’. The higher the number, higher the possibility of the OS being using RAM more and using the ‘Swap space’ as less as possible.
Enough talking, let’s do it …
For this example I’m using Ubuntu (12.04 Precise Pangolin), but, the steps should be pretty much the same under any never GNU/Linux distribution.
First, let’s ‘fetch’ the current ‘swappiness’ value. For that, open your Terminal and enter the below command.
As you can see, when I did that, it output the number ’10’, which means my Ubuntu installation tries aggressively to keep programs in RAM than in the ‘Swap space’ (because I have 4GB of RAM, more than enough to run recent versions of Ubuntu).
Then to manually tweak the setting, enter the below command in your Terminal (you’ll need to have ‘sudo/administrative’ privileges for this) and it should open a setting file called ‘sysctl.conf’ in the ‘gedit’ (text editor).
gksudo gedit /etc/sysctl.conf
Now, press ‘Ctrl’ + ‘F’ shortcut keys to open the search box and search for the below term.
If it doesn’t exist, then that’s because by default, Ubuntu uses a dynamic ‘swappiness’ value, so it can adjust the Swap file’s ‘sensitivity’ whenever it wants.
This actually makes sense, because having a ‘fixed’ value, can hurt the performance, when used unwisely (for instance: Using a value that suits for heavy loads so the ‘Swap space’ is used more, thus increasing the stability, and if it’s also used under normal loads, where ‘Swap space’ isn’t terribly important, can hurt the performance).
Anyhow, no need to worry. Because if it doesn’t exist, all you gotta do is manually create that value inside this configuration file.
For that, put your mouse cursor at the end of that file and press the ‘Enter’ key (to start a new line) and copy and past the below code into that new line (as shown in the below screenshot).
You can change the number ’10’ (range of 0 to 100), depending on your needs. Again, remember, if you want the OS to rely more on ‘Swap space’ then use a higher number and for using less ‘Swap space’ and relying more on RAM, use a lower number. It’s wiser not to make it ‘0’ though, as it will disable the use of ‘Swap space’ completely (because you never know ;-)).
Once done, save the changes and close the editor and the setting should be applied to the system (no need to reboot). That’s it!.
If you want, you can read this Ubuntu Wiki page for ‘Swap’ which explains pretty much everything about ‘Swap space’ that most end-users will ever need to know :). Good luck.