swap分區概念
當物理內存不夠用的時候,Linux會將內存中暫時不用的數據,放到swap分區中
swap分區一般在安裝好Linux系統時,系統會自己分配好,大小一般是內存的兩倍。如果遇到還不夠用,就需要我們手動來劃分swap分區
查看swap分區
我們系統現在的swap內存空間大小是7.9G左右。
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 15G 10G 2.0G 352M 2.5G 3.9G
Swap: 7.9G 0B 7.9G
增加swap分區大小
配置步驟
1、分區
2、內核重新讀取磁盤
3、格式化
4、掛載(臨時和永久)
1、分區
[root@localhost ~]# fdisk /dev/sdb
//省略部分輸出
Command (m for help): n //新建一個分區
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p //選擇主分區
Partition number (2-4, default 2): //分區編號2
First sector (209717248-419430399, default 209717248): //設置起始扇區
Using default value 209717248
Last sector, +sectors or +size{K,M,G} (209717248-419430399, default 419430399): +10G //給swap分區10個G
Partition 2 of type Linux and of size 10 GiB is set
Command (m for help): t //更改分區類型
Partition number (1,2, default 2):
Hex code (type L to list all codes): L //查看分區類型
//省略部分輸出
Hex code (type L to list all codes): 82 //82表示swap分區
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): w //保存並且退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2、內核讀取
[root@localhost ~]# partprobe /dev/sdb
3、格式化
注意sdb2是我的二塊磁盤
[root@localhost ~]# mkswap /dev/sdb2
4、臨時掛載
[root@localhost ~]# swapon /dev/sdb2
查看,原來7.9G,現在17G左右
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 15G 10G 2.0G 352M 2.5G 3.9G
Swap: 17G 0B 17G
5、卸載
[root@localhost ~]# swapoff /dev/sdb2
查看,原來17G,現在7.9G左右
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 15G 11G 191M 368M 4.1G 3.7G
Swap: 7.9G 0B 7.9G
6、永久掛載
修改配置文件/etc/fstab,增加如下內容:
/dev/sdb2 swap swap defaults 0 0
重新讀取配置文件,或者重啓服務器,來查看是否生效
[root@localhost ~]# swapon /dev/sdb2
查看,原來7.9G,現在17G左右
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 15G 10G 2.0G 352M 2.5G 3.9G
Swap: 17G 0B 17G