Why use swap when there is more than enough free space in RAM?
This behaviour can be configured by setting the value of:
/proc/sys/vm/swappiness
The default value is 60. Setting it to 0 means to never use swap when there is still RAM left and 100 is swapping out memory as soon as possible.
To change the value temporarily (lost on reboot):
sudo sysctl vm.swappiness=10
To change the value permanently, edit the file:
/etc/sysctl.conf
as root (e.g. sudo nano /etc/sysctl.conf
) and change or add (if not there) the line:
vm.swappiness
to the desired value. If this file does not exist (e.g. in Arch Linux), then try /etc/sysctl.d/99-sysctl.conf
instead.
There has been some debate on whether swapping out with free memory available is good or bad, but the Ubuntu help does indeed recommend a value of 10 for Desktop systems. See also this tutorial on Digital Ocean for CentOS.
It is normal for Linux systems to use some swap even if there is still RAM free. The Linux kernel will move to swap memory pages that are very seldom used (e.g., the getty
instances when you only use X11, and some other inactive daemon).
Swap space usage becomes an issue only when there is not enough RAM available, and the kernel is forced to continuously move memory pages to swap and back to RAM, just to keep applications running. In this case, system monitor applications would show a lot of disk I/O activity.
For comparison, my Ubuntu 10.04 system, with two users logged in with X11 sessions both running GNOME desktop, uses ~600MB of swap and ~1GB of RAM (not counting buffers and fs cache), so I'd say that your figures for swap usage look normal.
Linux starts swapping before the RAM is filled up. This is done to improve performance and responsiveness:
Performance is increased because sometimes RAM is better used for disk cache than to store program memory. So it's better to swap out a program that's been inactive for a while, and instead keep often-used files in cache.
Responsiveness is improved by swapping pages out when the system is idle, rather than when the memory is full and some program is running and requesting more RAM to complete a task.
Swapping does slow the system down, of course — but the alternative to swapping isn't not swapping, it's having more RAM or using less RAM.