How to Tweak the ‘Swap’ file’s behavior (for Performance or Stability) in Ubuntu Linux?

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 . 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 ‘‘.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’. A higher value makes the OS use the swap space more.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.Step 1:First, let’s ‘fetch’ the current ‘swappiness’ value. For that, open your Terminal and enter the below command.cat /proc/sys/vm/swappinessAs 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).Step 2: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.confStep 3:Now, press ‘Ctrl’ + ‘F’ shortcut keys to open the search box and search for the below term.vm.swappinessIf 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).Step 4: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).vm.swappiness=If this setting isn’t here, then create it as explained above …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

which explains pretty much everything about ‘Swap space’ that most end-users will ever need to know :). Good luck.Related Posts

An RHCE, ‘Linux’ user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.So glad you’re keeping this blog going.

FYI, I got most of the guides saved on my Ubuntu One.

😀I am thinking whether I should switch completely to Ubuntu 12.04 LTS.

The only issue I am still having is backlight, indicator-cpufreq, and Ubuntu Tweak.

These 3 seems to reset on every reboot.

Really annoying!YouTube does’t go to HD mode on fullscreen *sigh* (13Mbps)Last thing is that Ubuntu 12.04 is still running hotter than Windows 7 and fan would turn on much more frequently when launching apps (ahem, Ubuntu Software Center, etc…)Ready for the good news, Gayan?

Because of your blog, I am now using Ubuntu daily and having a hard time switching back to WIndows which I adored since 1998!

It’s extremely stable but the impractical, low-productive GUI is killing me.

Now I understand why many people having a hard time switching back to Windows if they came from Mac OS. I have 2GB RAM. My ‘swappiness’ value was by default set to 60. Is it safe to make it lower, like 30 maybe. And if so, how low it too low? Your write: “… The higher the number, higher the possibility of the OS being using RAM more and using the ‘Swap space’ as less as possible. …”If the swappiness is very low the kernel tries to avoid swapping,if it is very high it swaps aggressively.Therefore I believe it should say: “The lower the number, higher the possibility of the OS being using RAM more and using the ‘Swap space’ as less as possible. …” This site uses Akismet to reduce spam. .Recent Posts

ContactAbout HecticGeekWelcome to HecticGeek, it is an independent blog founded back in 2010. We cover in-depth Linux OS, product review with other all technology-related software and tools. Our mission is to provide valuable and trustful technology-related content to our users.

Leave a Reply

Your email address will not be published.