Size: 10870
Comment: added SwapSpace
|
Size: 11651
Comment: Tidying up my swappiness changes
|
Deletions are marked like this. | Additions are marked like this. |
Line 223: | Line 223: |
== Performance (advanced) == There is a parameter which defines the attitude of kernel to swap. This parameter is controlling how soon the kernel begin to fill the swap. This parameter is called "swappiness" and has a value in the 0-100 range. swappiness=0 means it will use swap as last as possible. swappiness=100 means it will begin to swap very early. Currently the default value is 60, but lowering to 10 or 5 would cause the machine to be better responsive. |
== Performance tuning with ''swappiness'' == The ''swappiness'' parameter controls the tendency of the kernel to move processes out of physical memory and on to the swap disk. As disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory. * ''swappiness'' can have a value of between 0 and 100 * ''swappiness=0'' tells the kernel to avoid swapping processes out of physical memory for as long as possible * ''swappiness=100'' tells the kernel to aggressively swap processes out of physical memory and move them to swap cache * Ubuntu uses a default setting of ''swappiness=60'' There seems to be some consensus that reducing the default value of ''swappiness'' will improve the responsiveness of operating system and applications for a 'typical' Ubuntu desktop installation. A value of ''swappiness=10'' is one that is often recommended. Note: this setting may not be suitable for Ubuntu server installations, and the default value of ''60'' may provide a better balance for performance. |
Line 236: | Line 236: |
{{{ $ cat /proc/sys/vm/swappiness |
* {{{ cat /proc/sys/vm/swappiness |
Line 241: | Line 241: |
* edit the /etc/sysctl.conf:{{{ $ sudo gedit /etc/sysctl.conf }}} * and add this line in the end of the file (change 10 to the value you want):{{{ |
1. a temporary change, which will be lost on reboot, can be made with:{{{ echo 10 > /proc/sys/vm/swappiness }}} 1. a permanent change can be made with:{{{ gksudo gedit /etc/sysctl.conf }}} 1. now add the following line to the end of the file just opened (the value ''10'' can be changed to your preference):{{{ |
Line 249: | Line 250: |
* save and reboot |
1. save and reboot |
ContentsBRTableOfContents(2) |
Introduction
This FAQ is aimed at Linux novices.
- People always wonder how much swap they should put on install, or after installing without a clue think "oh my god", have I put enough swap? Maybe I should just reinstall with more swap?
- This FAQ will tell you how much swap you need and how to add more swap after installation.
- You will be given very simple answers (so that you do not have to lose too much time reading this FAQ) and some explanations that may help you make your own opinion.
How much swap do I need?
- If you have n Mb of ram, you need between n and 2*n Mb of swap.
- If you have a disk big enough, just put 2*n Mb swap.
What is swap for?
- As we'll see, there are basically four different purposes for swap :
- Some programs really are memory-consuming.
- Extra memory might come in handy.
- Optimizing memory usage.
- Hibernation (suspend-to-disk)
- To begin with, let's say that computers have changed a lot since swap was first used :
- At first, swap was needed to extend real memory capacity. You would use swap so that the available memory would be the addition of the ram space and the swap space.
- Nowadays, ram are often big enough so that we could use the computer without any swap at all.
Some programs really are memory-consuming :
Sometimes, something big (like OpenOffice, Neverwinter Nights or some video editor) make the entire system need extra memory.
- In these cases, swap will be used to make the system able to handle the extra load.
Extra memory might come in handy :
- Unforeseeable events, might and will happen (a program going crazy, some action needing much more space than you thought, or any other unbelievable combination of events).
- In these cases, swap will give you an extra delay to figure out what happens or to finish something.
Swap can optimize memory usage :
Hard drives are considerably slower than RAM. So when you need a file (be it a data file, like this video you're watching again and again, executables, like Firefox, or libraries), Linux reads the file into RAM and keeps it there so than the next time you need it again, it's already in RAM and data access is much faster (thousands of times faster). We call these portions of RAM that accelerate disk read "cached memory." They make a huge difference in terms of responsiveness.
- Linux automagically moves RAM reserved by programs but not really used in swap so that this ram can serve the better purpose of having more cached memory.
Hibernation needs swap
The hibernation feature (suspend-to-disk) writes out the contents of the memory to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your RAM size. The hibernation implementation currently used in Ubuntu, swsusp, needs a swap or suspend partition, and can not use a swap file on an active file system.
Should I reinstall with more swap?
Definitely no.
- If you need more swap, just read the answer to "How do I add more swap ?".
How do I add more swap?
- Usually, people associate swap with a swap partition, maybe because they've been proposed to create a swap partition on install. In fact any file can be used as a swapping device, be it a partition or a conventional file.
With the 2.6 kernel, "a swap file is just as fast as a swap partition."([http://en.wikipedia.org/wiki/Paging#Swapping_in_Linux Wikipedia:Paging], [http://lkml.org/lkml/2006/5/29/3 LKML]). If you're considering responsiveness, my advice: add more RAM. Swapping to a partition or a file won't change anything.
- We will add more swap by adding a swap file.
Adding more swap is a four-step process :
- a- Creating a file the size you want.
- b- Formatting that file to create a swapping device.
- c- Adding the swap to the running system.
- d- Making the change permanent.
We will consider (as an example) a 512 Mb swap need.
a- Creating a file the size you want :
We will create a /mnt/512Mb.swap swap file.
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
What is important here is count=512, which means we want our file to contain 512 blocks of bs=1M, which means block size = 1 MegaBytes.
b- Formatting that file to create a swapping device :
sudo mkswap /mnt/512Mb.swap
c- Adding the swap to the running system :
sudo swapon /mnt/512Mb.swap
You can see with "cat /proc/meminfo" that your additionnal swap is now available.
d- Making the change permanent :
edit your /etc/fstab:
sudo gedit /etc/fstab
and add this line at the end of the file:
/mnt/512Mb.swap none swap sw 0 0
- save and reboot
Troubleshooting
Note:this regards mainly swap on hard disk partitions, but it could help you anyway. Also replace /dev/hda8 with your configuration.
Help! The swap is not being used! When I issue the free command, it shows something like this:
tom@tom:~$ free total used free shared buffers cached Mem: 515980 448664 67316 0 17872 246348 -/+ buffers/cache: 184444 331536 Swap: 674688 0 674688
1- First try, if it is because the system cannot use swap or because it just does not need it. Start many memory consuming applications (e.g. Gimp, web browsers, OpenOffice etc) and then issue the free command again. Is swap being used now?
2- See if there is a swap partition at all:
sudo fdisk -l
you should be able to see something like this in the output
/dev/hda8 4787 4870 674698+ 82 Linux swap / Solaris
If not, you either need to create a swapfile (see above as to how to do it) or create a swap partition. In order to do that, you can for example
- boot from your Ubuntu install CD (rather usin the expert bot option), create a swap partiton out of the free space on your hardddisk and then interrupt your installation
- or just use cfdisk (lookup its man page for more info).
However, let us just suppose you have a swap partition. There are several ways of enabling it.
2.1- First make sure you have something like this
/dev/hda8 none swap sw 0 0
in /etc/fstab. It enables swap on boot.
2.2- Then try to disable all swap, recreate it and then enable it again
swapoff -a /sbin/mkswap /dev/hda8 swapon -a
- 2.3 Some people also reported this to help:
- 1) cfdisk : erase the old Swap partition and recreate a new one
- 2) reboot (mandatory)
- 3) mkswap /dev/hda8
- 4) swapon -a
Example of making a swap file
This is an example of making and using a swap file on a computer with no swap partition.
user@computer:~$ sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512 Password: 512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 35.3802 seconds, 15.2 MB/s user@computer:~$ sudo mkswap /mnt/512Mb.swap Setting up swapspace version 1, size = 536866 kB no label, UUID=dd6a01c8-93f0-41e0-9b7a-306956d8821b user@computer:~$ sudo swapon /mnt/512Mb.swap user@computer:~$ cat /proc/meminfo MemTotal: 499496 kB MemFree: 9156 kB Buffers: 4748 kB Cached: 233140 kB SwapCached: 724 kB Active: 254432 kB Inactive: 157920 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 499496 kB LowFree: 9156 kB SwapTotal: 524280 kB SwapFree: 523556 kB Dirty: 128 kB Writeback: 0 kB Mapped: 243420 kB Slab: 20672 kB CommitLimit: 774028 kB Committed_AS: 648680 kB PageTables: 2224 kB VmallocTotal: 524280 kB VmallocUsed: 5708 kB VmallocChunk: 518176 kB user@computer:~$ gksudo gedit /etc/fstab user@computer:~$ free total used free shared buffers cached Mem: 499496 479488 20008 0 8256 215892 -/+ buffers/cache: 255340 244156 Swap: 524280 3856 520424 #####Then after running a few more programs... user@computer:~$ free total used free shared buffers cached Mem: 499496 492768 6728 0 1240 142336 -/+ buffers/cache: 349192 150304 Swap: 524280 53384 470896 #####Next reboot to make sure it will work consistently. user@computer:~$ free total used free shared buffers cached Mem: 499496 493136 6360 0 7528 174700 -/+ buffers/cache: 310908 188588 Swap: 524280 17148 507132 #####I followed the instructions to the letter, and it worked :-)
Performance tuning with ''swappiness''
The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and on to the swap disk. As disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.
swappiness can have a value of between 0 and 100
swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache
Ubuntu uses a default setting of swappiness=60
There seems to be some consensus that reducing the default value of swappiness will improve the responsiveness of operating system and applications for a 'typical' Ubuntu desktop installation. A value of swappiness=10 is one that is often recommended. Note: this setting may not be suitable for Ubuntu server installations, and the default value of 60 may provide a better balance for performance.
How to check the swappiness value
cat /proc/sys/vm/swappiness
How to change the swappiness value
a temporary change, which will be lost on reboot, can be made with:
echo 10 > /proc/sys/vm/swappiness
a permanent change can be made with:
gksudo gedit /etc/sysctl.conf
now add the following line to the end of the file just opened (the value 10 can be changed to your preference):
vm.swappiness=10
- save and reboot
Advanced Topics
Author
Yannick Le Saint (kyncani) <y.lesaint@gmail.com>
Tomas Hnyk <thnyk@seznam.cz> - troubleshooting section
Max Randor <max (DOT) randor <at>googlemail.com>
About the document
TODO :
- Determine the more standard emplacement for swap files (FHS).
- Add power suspend considerations.
- 15/05/2005, 01:42
- First version.