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