SimpleDao
Linux设置虚拟内存
2019-05-04, 访问数: 1085

决定修改swap大小

首先在空间合适处创建用于分区的swap文件

  1. dd if=/dev/zero of=/swap/swap bs=1024 count=4194304

将目的文件设置为swap分区文件

  1. mkswap /swap/swap

激活swap,立即启用交换分区文件

  1. swapon /swap/swap

查看

  1. free -m

开机时自启用

修改文件/etc/fstab中的swap行

  1. /swap/swap swap swap defaults 0 0

swappiness

check

  1. cat /proc/sys/vm/swappiness

edit

  1. sudo vim /etc/sysctl.conf

apply

  1. sudo sysctl -p

Ubuntu: What is swappiness

  1. The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

swappiness can have a value between 0 and 100.

  • swappiness=0:
    • Kernel version 3.5 and newer: disables swapiness.
    • Kernel version older than 3.5: avoids swapping processes out of physical memory for as long as possible.
  • swappiness=1:
    Kernel version 3.5 and over: minimum swappiness without disabling it entirely.
  • swappiness=100:
    Tells the kernel to aggressively swap processes out of physical memory and move them to swap cache.

References:

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment.

In server, set swappiness=1.