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.
翻译:Linux在物理内存用完之前使用swap,,这样有助于提高系统性能和响应能力1 性能可以提升的原因是:一些内存放磁盘缓存比方内存更合适。所以,最好交换出暂时不使用的内存,而把经常使用的文件放内存里面
2 提升响应能力通过,当系统空闲的时候swap page out,而不是当内存忙的时候,正在执行程序的时候执行swap
swaping确实会是系统性能下降,当然,通过swap可以有更多的可用内存
如何在有内存可用的时候,不使用swap呢?
This behaviour can be configured by setting the value of /proc/sys/vm/swappiness
. The default value is 60, setting it to 0 means “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 the line vm.swapiness=...
to the desired value.
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:https://help.ubuntu.com/community/SwapFaq
centos swappiness配置的路径 /proc/sys/vm/swappiness
为什么不建议关闭swap
It is NOT recommended to turn off swap even if you have enough memory. If your server needs more memory and it did not get it, it will crash. However, this can be prevented (to some extent) when you have a swap area.
Yes, your server performance will degrade when using swap, but at least it will be operational and accessible. Then, you can plan for adding more memory when needed if your server starts using swap.
I found this page talking about swap. Have a look at 3rd section.
Instead of turning off swap, you can control the swapiness.
参考
http://unix.stackexchange.com/questions/2658/why-use-swap-when-there-is-more-than-enough-ram
swap知识 https://help.ubuntu.com/community/SwapFaq
http://linux.cloudibee.com/2007/11/linux-performance-tuning-vm-swappiness/