Swap partition function:
Swap partition when the system's physical memory is not enough, the part of the space in the hard disk space is released for use by the currently running program to use.
实际上使用硬盘实现虚拟内存,即当系统内存使用率比较高的时候,内核会自动使用swap分区来模拟内存
Basic command:
the mkswap / Devices: format formatted swap
swapon / devices: activating swap, the swap area is added to the
power added automatically start a new swap partition: / etc / fstab / devices swap swap defaults 0 0
Experimental Procedure:
1. Create a partition
[root@localhost ~]# gdisk /dev/sdb
Command (? for help): n ##新建分区
Partition number (2-128, default 2): ##回车
First sector (34-41943006, default = 10487808) or {+-}size{KMGTP}: ##回车
Last sector (10487808-41943006, default = 41943006) or {+-}size{KMGTP}: +1G #给1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): ##回车
Changed type of partition to 'Linux filesystem'
Command (? for help): w #保存
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
2, see the need to reboot before restarting the machine, and then see if success
[root@localhost ~]# reboot ##重启完成后 查看
[root@localhost ~]# ll /dev/sd*
brw-rw---- 1 root disk 8, 16 Feb 27 01:31 /dev/sdb
brw-rw---- 1 root disk 8, 17 Feb 27 01:31 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Feb 27 01:31 /dev/sdb2 ##创建成功
3, the formatter
formatted activation swap, swap added to the partition. free -m viewed by 2047 becomes 3071
4, increasing the swap file space?
Ali cloud no default swap space, how to increase swap?
1)[root@localhost ~]# dd if=/dev/zero of=swap_file bs=1M count=500
500+0 records in # 记录了500+0 的读入
500+0 records out # 记录了500+0 的写出
524288000 bytes (524 MB) copied, 16.7804 s, 31.2 MB/s
[root@localhost ~]# ll -h swap_file
-rw-r--r-- 1 root root 500M Feb 27 01:38 swap_file
[root@localhost ~]# chmod 0600 swap_file
2)把500M的文件格式化成swap
[root@localhost ~]# mkswap -f swap_file
Setting up swapspace version 1, size = 511996 KiB #正在设置交换空间版本 1,大小511996 KiB
no label, UUID=c183db40-fcee-45eb-8c86-40c0985ca745 #无标签
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 976 84 262 6 630 724
Swap: 3071 0 3071
[root@localhost ~]# swapon /root/swap_file ##激活swap空间
查看是否被激活
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 976 84 261 6 630 723
Swap: 3571 0 3571