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