1、swap 介绍
swap交换分区是系统RAM的补充,swap 分区支持虚拟内存。当没有足够的 RAM 保存系统处理的数据时会将数据写入 swap 分区,当系统缺乏 swap 空间时,内核会因 RAM 内存耗尽而终止进程。配置过多 swap 空间会造成存储设备处于分配状态但闲置,造成浪费,过多 swap 空间还会掩盖内存泄露,为优化性能,可以将swap 分布存放,或高性能磁盘存放。
![图片[1]-交换分区-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/11/image-41.png)
2、交换分区实现过程
- 创建交换分区或者文件
- 使用mkswap写入特殊签名
- 在/etc/fstab文件中添加适当的条目
- 使用swapon -a 激活交换空间
#启用swap分区
swapon [OPTION]... [DEVICE]
#选项
-a #激活所有的交换分区
-p PRIORITY #指定优先级(-1到32767之间),值越大,优先级越高.也可在/etc/fstab文
件中的第4列指定:pri=value
#范例
#创建swap分区
[root@centos79_test ~]# echo -e 'n\np\n\n\n+2G\nt\n82\nw\n' | fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): Partition number (1-4, default 1): First sector (2048-104857599, default 2048): Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): Selected partition 1
Hex code (type L to list all codes): Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos79_test ~]# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=03b0325f-fa52-4c9d-889c-a01ea769a43c
[root@centos79_test ~]# blkid
/dev/sda1: UUID="f4a18182-b3a6-4cba-94cd-46ce44c002ce" TYPE="xfs"
/dev/sda2: UUID="7xxTOI-R5pJ-U2QW-66nH-8eQj-dOxd-Nkb6MZ" TYPE="LVM2_member"
/dev/sdb1: UUID="03b0325f-fa52-4c9d-889c-a01ea769a43c" TYPE="swap"
/dev/sr0: UUID="2020-11-04-11-36-43-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="295ac3d3-c339-4264-8674-e321476e2923" TYPE="xfs"
/dev/mapper/centos-swap: UUID="b7933299-7c57-42f9-a452-a8b92d281b6f" TYPE="swap"
[root@centos79_test ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Nov 8 14:51:00 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=f4a18182-b3a6-4cba-94cd-46ce44c002ce /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=03b0325f-fa52-4c9d-889c-a01ea769a43c swap swap defaults 0 0
[root@centos79_test ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 229M 3.3G 11M 129M 3.3G
Swap: 2.0G 0B 2.0G
[root@centos79_test ~]# swapon -a
[root@centos79_test ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 231M 3.3G 11M 129M 3.3G
Swap: 4.0G 0B 4.0G
[root@centos79_test ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -2
/dev/sdb1 partition 2097148 0 -3
#禁用swap分区
swapoff [OPTION]... [DEVICE]
#范例
#禁用swap分区
[root@centos79_test ~]# sed -i.bak '/swap/d' /etc/fstab
[root@centos79_test ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Nov 8 14:51:00 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=f4a18182-b3a6-4cba-94cd-46ce44c002ce /boot xfs defaults 0 0
[root@centos79_test ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 230M 3.3G 11M 129M 3.3G
Swap: 4.0G 0B 4.0G
[root@centos79_test ~]# swapoff -a
[root@centos79_test ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 228M 3.3G 11M 128M 3.3G
Swap: 0B 0B 0B
[root@centos79_test ~]# cat /proc/swaps
Filename Type Size Used Priority
[root@centos79_test ~]#
3、SWAP的优先级
- 可以指定swap分区0到32767的优先级,值越大优先级越高
- 如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一
- 先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)
4、swap的使用策略
/proc/sys/vm/swappiness 的值决定了当内存占用达到一定的百分比时,会启用swap分区的空间
- 当内存使用率达到100-swappiness时,会启用交换分区
- 这个参数定义了系统对swap的使用倾向,此值越大表示越倾向于使用swap。
- 可以设为0,这样做并不会禁止对swap的使用,只是最大限度地降低了使用swap的可能性
- CentOS7和8默认值为30,内存在使用到100-30=70%的时候,就开始出现有交换分区的使用。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END