如何在 Linux 内存管理中配置 swappiness?

GAD*_*D3R 14 linux performance swap

swappiness 参数控制内核将进程从物理内存移到交换磁盘上的趋势。默认设置是什么以及如何配置它以提高整体性能

ARe*_*ddy 11

Linux 内核提供了一个可调整的设置来控制 swappiness

$ cat /proc/sys/vm/swappiness
60  
Run Code Online (Sandbox Code Playgroud)

/etc/sysctl.conf以 root 身份打开。然后,将此行更改或添加到文件中:

vm.swappiness = 10
Run Code Online (Sandbox Code Playgroud)

要临时更改 swappiness 值,请尝试以下命令:

$ echo 50 > /proc/sys/vm/swappiness
Run Code Online (Sandbox Code Playgroud)

  • 您是否还需要“sudo sysctl -p /etc/sysctl.conf”才能使更改生效? (2认同)

Key*_*ate 11

来自Swappiness,维基百科

您可以在虚拟文件中设置此值/proc/sys/vm/swappiness 提到的值将决定如何使用交换空间,以下是其意图的值。

vm.swappiness = 0   # Swap is disabled. In earlier versions, this meant that the kernel would swap only to avoid an out of memory condition, but in later versions this is achieved by setting to 1.
vm.swappiness = 1   # Kernel version 3.5 and over, as well as kernel version 2.6.32-303 and over: Minimum amount of swapping without disabling it entirely.
vm.swappiness = 10  # This value is sometimes recommended to improve performance when sufficient memory exists in a system, this value *10* could be considered for the performance being expected. 
vm.swappiness = 60  # The default value.
vm.swappiness = 100 # The kernel will swap aggressively.
Run Code Online (Sandbox Code Playgroud)

虽然实际上也要看需求,如果可用的物理内存足够,交换空间的需求可能不是很大,用外行的话来说,不需要更改 的默认值60