Speed up your system by avoiding the swap file

Most modern operating systems are capable of using a file or partition known as a swap or paging file. Most Linux distributions will also install one for you by default. This file is used to extend the amount of available RAM by writing some of it to your hard drive.

There's just one problem: hard drives are slow. We can't fix that problem yet, but we can avoid it. The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness. A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly.

My Ubuntu system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:

cat /proc/sys/vm/swappiness

My HDD is insanely slow and I have 2 GB of RAM, so I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent. To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 15

Reboot for the change to take effect. You can also change the value while your system is still running:

sysctl vm.swappiness=15

However, you won't get the full effect of rebooting because there is probably already memory stored in swap that won't instantly be moved out.

Update: Readers have noted that you can clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

Avatar for jacob Jacob Peddicord - http://jacob.peddicord.net/

Jacob is a web developer, student, and programmer from Ohio. He is a staff member at the Ubuntu Forums and is most likely a fanboy of the distribution. He loves to write in code and words, play video games, and rant about topics most would have abandoned long ago. Jacob uses GNOME and is never seen running stable software, much to the demise of his laptop.

Home » Articles »

Discussion: Speed up your system by avoiding the swap file

  1. Chris (guest)

    # Posted on 08 February 2009 at 03:45 AM



  2. Gavin (guest)

    # Posted on 08 February 2009 at 11:44 AM

    Is this available to Mac OSX?

  3. # Posted on 08 February 2009 at 12:59 PM

    [...]  Fonte: fosswire.com [...]

  4. Scott (guest)

    # Posted on 08 February 2009 at 01:21 PM

    This is an awesome tip. Thanks Jacob. How, from a command line, can I tell how much RAM my system has and how much it's using? Will "top" tell me all that? I find I have a difficult time telling what everything in "top" means.


  5. Reload (guest)

    # Posted on 08 February 2009 at 01:34 PM

    @Scott: You can try htop!

  6. Jacob (guest)

    # Posted on 08 February 2009 at 01:53 PM


    Or free -m, though you'll want to look up the meaning of cached memory somewhere as it doesn't count as in-use memory.

  7. Gavin (guest)

    # Posted on 08 February 2009 at 01:55 PM

    @Scott: free -m

    The middle line is the interesting line because it shows how much RAM is actually being used.

  8. rrh (guest)

    # Posted on 08 February 2009 at 05:16 PM

    You can always flush the swap with

    sudo swapoff -a; sudo swapon -a

    thus avoiding any need to reboot.

  9. Rob (guest)

    # Posted on 08 February 2009 at 05:33 PM

    As root: echo "10" > /proc/sys/vm/swappiness

    This will set swappiness in a non-persistent manner (i.e. won't survive a reboot), used in conjunction with rrh's suggestion it'll give you a feel for it without having to reboot first.

  10. Bob/Paul (guest)

    # Posted on 08 February 2009 at 05:51 PM

    >"However, you won’t get the full effect of rebooting because there is probably already memory stored in swap that won’t instantly be moved out."

    Don't reboot. Do "sudo swapoff -a" in the terminal. Once that completes, do "sudo swapon -a". This will turn your swap file off, forcing everything back into ram (if you don't have enough free memory, some processes will be automatically killed as the swap file is emptied, so be aware). The second command, quite obviously, turns your swap files back on.

  11. James (guest)

    # Posted on 09 February 2009 at 12:10 AM

    Additionally you can just reload sysctl (rather than echoing to proc or loading the command on the command line.)

    > sysctly -p /etc/sysctl.conf

  12. James (guest)

    # Posted on 09 February 2009 at 12:11 AM

    oops sorry for the typo that should have been

    > sysctl -p /etc/sysctl.conf

  13. Harry Simons (guest)

    # Posted on 09 February 2009 at 02:38 AM

    Hey Jacob, thanks a lot for this excellent tip! Please do share more of such in future, if possible. /HS

  14. djiezes (guest)

    # Posted on 09 February 2009 at 03:54 AM

    I've found that with the default swappiness setting of 60, my pc never uses swap at all. This also on a 2GB system and I multi-task a lot. I can't even remember using more than 1GB at any time.

  15. Leonid Volnitsky (guest)

    # Posted on 09 February 2009 at 06:19 AM

    This is not exactly use-or-not-use-swap parameter. This is more prefer-swapping-out-running process or-decreasing-buffer parameter.

  16. Christophe (guest)

    # Posted on 09 February 2009 at 11:40 AM

    Thanks, excellent tip. I didn't know about that. I will look further in the sysctl tool, I guess there are a lot of interesting things ^_^

  17. # Posted on 10 February 2009 at 11:18 PM

    [...] Quickies: optimize swap file use Filed under: Linux — 0ddn1x @ 2009-02-10 23:18:20 +0000 http://fosswire.com/2009/02/08/sysctl-swappiness/ [...]

  18. # Posted on 15 February 2009 at 09:44 AM

    [...] FOSSwire. Tags: linux, [...]

  19. # Posted on 20 February 2009 at 06:08 AM

    [...] Tăng tốc hệ thống bằng cách hạn chế dùng file swap.:Swap file dùng hỗ trợ cho RAM khi không đủ bộ nhớ nhưng cũng làm chậm hệ thống vì ổ cứng vốn chậm. Trong nhân Linux có một thông số điều khiển mức độ sử dụng swap file là swapiness. swapiness=0 nghĩa là swap file chỉ được dùng khi thật sự cần, swapiness=100 thì swap file luôn được dùng đến. Kiểm tra trị số đặt sẵn của swapiness bằng lệnh cat /proc/sys/vm/swappiness. [...]

  20. Sudipto Sarkar (guest)

    # Posted on 03 March 2009 at 05:49 PM

    Cool article... Btw, I have written one article about speeding up Ubuntu Linux distros by using xfce desktop environment. That is by installing the Xubuntu. It's still here: http://opensourcethefuture.blog.co.in/2009/03/02/speed-up-ubuntu

  21. Skaf (guest)

    # Posted on 19 March 2009 at 11:46 PM

    I agree with Leonid Volnitsky. It's not as though the kernel does not use all of your memory. Its default (and very reasonable) behaviour is to only swap out when absolutely necessary. On my system, this only happens if I have around 13 (!) MBs of free RAM left. If you look at what tools like gnome-system-monitor tell you, you may think that your system is swapping prematurely, while you (appear to) have plenty of RAM left. That's not entirely true. Use console tools like free -m for detailed or cat /proc/meminfo for very detailed memory information.

    Decreasing swappiness will tell the kernel to decrease the space allocated for caches and buffers in favour of running apps (again, only if the need arises). However, there is nothing less useful than having a bloated, little-used app take up RAM which could serve so much a better as a cache for things more frequently used. Whatever your swappiness, if you find that your apps often make your system grind to halt, there is no solution other than adding RAM.

  22. Dmitri (guest)

    # Posted on 28 April 2009 at 01:17 AM

    Simply increasing RAM by 2-8Gb (just little $$ cost) will decrease swapping level to zero.

Home » Articles » Speed up your system by avoiding the swap file

Sign In