swap分区相关操作

交换分区swap

用途:swap 分区就是交换分区,(windows平台叫虚拟内存)

在物理内存不够用时,操作系统会从物理内存中把部分暂时不被使用的数据转移到交换分区,

从而为当前运行的程序留出足够的物理内存空间

关闭swap原因:swap启用后,在使用磁盘空间和内存交换数据时,性能表现会比较差,会减慢程序执行的速度。

查看swap当前的状态

[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         172        1510           9         137        1500
Swap:             0           0           0

用swapon查看所属设备和大小:

[root@localhost ~]# swapon -v
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2

临时关闭swap

查看swapoff的版本

[root@localhost ~]# swapoff -V
swapoff from util-linux 2.23.2

关闭swap

root@localhost ~]# swapoff -a

临时关闭swap后启用swap

[root@localhost ~]# swapon -a

永久关闭swap

通过修改/etc/fstab文件永久关闭swap,找到swap分区的记录并且注释掉即可

[root@localhost ~]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Tue Nov  8 21:08:57 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=9173f045-3f4b-42e8-b9ee-e8c46cdc4c6b /boot                   xfs     defaults        0 0
# /dev/mapper/centos-swap swap                    swap    defaults        0 0

重启机器查看是否关闭成功

[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         174        1503           9         141        1496
Swap:             0           0           0