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 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’. A higher value makes the OS use the swap space more.

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.

Step 1:

First, let’s ‘fetch’ the current ‘swappiness’ value. For that, open your Terminal and enter the below command.

cat /proc/sys/vm/swappiness


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).

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.conf

Step 3:

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).

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).


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 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.

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.

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

  1. 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.

    • You are welcome!.

      As for the ‘indicator-cpufreq’, there is actually one thing that you could try and that is installing a tool called ‘laptop-mode-tools’. Then, once installed, I think you should be able to manually set CPU frequencies (while running on Battery or AC) by using its main configuration file.

      I have written about it in the past, if you prefer then you can use the below link for more info.

      Once opened its configuration file, you might be able to find this CPU frequencies related options inside it. Here’s a very useful link from the ‘Arch Linux’ documentation.

      BTW, if you want to learn about GNU/Linux, then I highly recommend reading the ‘Wiki’ pages by the Arch team, they’re excellent! (there, I just gave you one of my best guarded secrets, kidding :P).

  2. 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?

    • Hi Will,
      Since you’ve 2GB RAM, it should be okay to make it 30.

      For the second question, well, I’m no expert in memory management, but if I’m not mistaken, it should heavily depend on what type of applications that you frequently use on your OS & their memory requirements, thus, it’s very difficult to give a ‘minimum’ value.

  3. 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. …”


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.