最近使用Ubuntu是发现经常读硬盘,导致速度非常的慢的。搜索了一下发现在可以调整内存的交换设置来改善这种情况。
代码如下,值可以是0~100,当我们把swappiness设置成0后,基本上就没有什么交换的情况发生了。
速度就非常快了,真正的感觉到使用Linux快感了。
1 | sudo sysctl vm.swappiness=0 |
当然要想把这个设置保存下了,需要把这个值保存到文件
1 | /etc/sysctl.conf |
中,添加如下的语句:
1 | vm.swappiness=0 |
下面我把wiki上的内容复制一下,
http://en.wikipedia.org/wiki/Swappiness
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 (by 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 aggressively
To temporarily set the swappiness in Linux, write the desired value (e.g. 10) to /proc/sys/vm/swappiness using the following command, running as root user: echo 10 > /proc/sys/vm/swappiness
Permanent changes are made in /etc/sysctl.conf via the following configuration line (inserted if not present previously): vm.swappiness = 10