最近工作中频繁遇到swap引发的问题,记得曾经读过当内存比较大的时候可以将swappiness调整成0。下面整理一下swappiness相关的知识。
1、什么是swap
Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。
2、swappiness干什么用
swappiness是控制系统使用swap分区的频率阀值,当swappiness=100的时候,系统会积极使用swap分区,而当swappiness=0的时候系统最大限度的有限使用物理内存。
Swappiness is a property for the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (decreasing response latency.)[1]
In short:
vm.swappiness = 0 - it will swap only to avoid an out of memory condition
vm.swappiness = 60 - default value
vm.swappiness = 100 - it will swap aggressvely
To temporarily set the swappiness in Linux, as root you set the value to 100 with the following command: echo 100 > /proc/sys/vm/swappiness
Permanent changes are made in /etc/sysctl.conf via the following configuration line (inserted if not present previously): vm.swappiness = 100
3、如何修改swappiness
查看
cat /proc/sys/vm/swappiness
修改
sysctl vm.swappiness=10
永久修改
echo "0" >> /etc/sysctl.conf