Swap分区 作用:
Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。
实际上使用硬盘实现虚拟内存,即当系统内存使用率比较高的时候,内核会自动使用swap分区来模拟内存
基础命令:
mkswap /devices : 格式化成swap格式
swapon /devices : 激活swap ,加入到swap分区中
开机自动启动新添加的swap分区: /etc/fstab /devices swap swap defaults 0 0
实验步骤:
1、建立分区
[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、 查看前需要reboot 重启机器,再查看是否成功
[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、格式化
格式化后 激活swap,加入到swap分区中。free -m 查看 由2047变为3071
4、使用 文件增加swap空间?
阿里云默认的没有swap空间,如何增加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