目录
Linux内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存)。即使你的程序运行结束后,Cache Memory也不会自动释放。这就会导致你在Linux系统中程序频繁读写文件后,你会发现可用物理内存变少。当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。小内存VPS升级的情况下就有可能出现OOM的情况,因此可以将Swap分区调整大一些。
查看Swap
[root@local ~]# swapon -s
Filename Type Size Used Priority
/dev/vda2 partition 131068 45928 -2
[root@local ~]# swapon -s
Filename Type Size Used Priority
/dev/vda2 partition 131068 45928 -2
[root@local ~]# swapon -s Filename Type Size Used Priority /dev/vda2 partition 131068 45928 -2
在添加新的交换分区前需要删除掉原来的:
[root@local ~]# swapoff -a
[root@local ~]# swapon -s
[root@local ~]# swapoff -a
[root@local ~]# swapon -s
[root@local ~]# swapoff -a [root@local ~]# swapon -s
添加Swap
swapon(选项)(参数)
交换空间:指定需要激活的交换空间,可以是交换文件和交换分区,如果是交换分区则指定交换分区对应的设备文件。
[root@local ~]# dd if=/dev/zero of=/var/swapfile bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 5.27565 s, 102 MB/s
//创建一个512M的交换分区文件,文件大小=bs*count
swapon(选项)(参数)
交换空间:指定需要激活的交换空间,可以是交换文件和交换分区,如果是交换分区则指定交换分区对应的设备文件。
[root@local ~]# dd if=/dev/zero of=/var/swapfile bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 5.27565 s, 102 MB/s
//创建一个512M的交换分区文件,文件大小=bs*count
swapon(选项)(参数) 交换空间:指定需要激活的交换空间,可以是交换文件和交换分区,如果是交换分区则指定交换分区对应的设备文件。 [root@local ~]# dd if=/dev/zero of=/var/swapfile bs=1M count=512 512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 5.27565 s, 102 MB/s //创建一个512M的交换分区文件,文件大小=bs*count
tips-dd:
- if 代表输入文件。如果不指定if,默认就会从stdin中读取输入
- of 代表输出文件。如果不指定of,默认就会将stdout作为默认输出
- bs 代表字节为单位的块大小
- count 代表被复制的块数
- /dev/zero 是一个字符设备,会不断返回0值字节(\0)
单元大小 | 代码 |
字节(1B) | c |
字节(2B) | w |
块(512B) | b |
千字节(1024B) | k |
兆字节(1024KB) | M |
吉字节(1024MB) | G |
添加完毕后需要重新设置交换分区文件权限,系统建议设置成600,完毕后即可建立交换分区:
[root@local ~]# chmod 600 /var/swapfile
[root@local ~]# mkswap /var/swapfile
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=07d898d7-5619-465a-9e07-a0f02b50f7c7
[root@local ~]# swapon /var/swapfile
[root@local ~]# swapon -s
Filename Type Size Used Priority
/var/swapfile file 524284 0 -2
[root@local ~]# chmod 600 /var/swapfile
[root@local ~]# mkswap /var/swapfile
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=07d898d7-5619-465a-9e07-a0f02b50f7c7
[root@local ~]# swapon /var/swapfile
[root@local ~]# swapon -s
Filename Type Size Used Priority
/var/swapfile file 524284 0 -2
[root@local ~]# chmod 600 /var/swapfile [root@local ~]# mkswap /var/swapfile Setting up swapspace version 1, size = 524284 KiB no label, UUID=07d898d7-5619-465a-9e07-a0f02b50f7c7 [root@local ~]# swapon /var/swapfile [root@local ~]# swapon -s Filename Type Size Used Priority /var/swapfile file 524284 0 -2
为了保证每次系统重启后Swap分区可用设置/etc/fstab以便在系统引导期间完成添加:
[root@local ~]# vi /etc/fstab
/dev/vda1 / ext4 defaults 1 1
/var/swapfile swap swap defaults 0 0
none /dev/shm tmpfs defaults 0 0
[root@local ~]# reboot
[root@local ~]# free -mh
total used free shared buff/cache available
Mem: 234M 61M 85M 4.4M 88M 160M
Swap: 511M 0B 511M
[root@local ~]# vi /etc/fstab
/dev/vda1 / ext4 defaults 1 1
/var/swapfile swap swap defaults 0 0
none /dev/shm tmpfs defaults 0 0
[root@local ~]# reboot
[root@local ~]# free -mh
total used free shared buff/cache available
Mem: 234M 61M 85M 4.4M 88M 160M
Swap: 511M 0B 511M
[root@local ~]# vi /etc/fstab /dev/vda1 / ext4 defaults 1 1 /var/swapfile swap swap defaults 0 0 none /dev/shm tmpfs defaults 0 0 [root@local ~]# reboot [root@local ~]# free -mh total used free shared buff/cache available Mem: 234M 61M 85M 4.4M 88M 160M Swap: 511M 0B 511M
其他参数
- swappiness:表示使用swap的权重,设置范围为0-100,0表示最大限度的使用内存,尽量不使用swap,100表示积极使用swap,腾讯和京东云主机默认值为30。可以通过cat /proc/sys/vm/swappiness查看。我们设置成10即可:
[root@local ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.swappiness=10
[root@local ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.swappiness=10
[root@local ~]# vi /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). vm.swappiness=10
保存退出重启VPS即可。
转载请注明:雪后西塘 » Linux VPS添加Swap分区