Centos系統如何臨時關閉/永久關閉交換分區swap虛擬內存?

為什么要關閉swap虛擬內存?

1:swap的用途?

swap 分區就是交換分區,(windows平臺叫虛擬內存)

在物理內存不夠用時,

操作系統會從物理內存中把部分暫時不被使用的數據轉移到交換分區,

從而為當前運行的程序留出足夠的物理內存空間

2:為什么要關閉swap?

swap啟用后,在使用磁盤空間和內存交換數據時,

性能表現會較差,會減慢程序執行的速度

有的軟件的設計師不想使用交換,

例如:kubelet 在 1.8 版本以后強制要求 swap 必須關閉

否則會報錯:

Running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false

或者kubeadm init時會報錯:

[ERROR Swap]: running with swap on is not supported. Please disable swap

查看swapon/swapoff所屬的rpm包

[root@centos8 ~]# whereis swapoff
swapoff: /usr/sbin/swapoff /usr/share/man/man8/swapoff.8.gz /usr/share/man/man2/swapoff.2.gz
[root@centos8 ~]# whereis swapon
swapon: /usr/sbin/swapon /usr/share/man/man8/swapon.8.gz /usr/share/man/man2/swapon.2.gz
[root@centos8 ~]# rpm -qf /usr/sbin/swapon
util-linux-2.32.1-17.el8.x86_64 
[root@centos8 ~]# rpm -qf /usr/sbin/swapoff
util-linux-2.32.1-17.el8.x86_64

如果誤刪除了命令可以用dnf安裝

[root@centos8 ~]# dnf install util-linux

查看swap當前的狀態

查看swap的空間,共4G

[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1224        1080          20        1452        2270
Swap:          4047           0        4047

用swapon查看所屬設備和大小:

#-v: –verbose? ? ? ? ? ? ? ? ?詳盡模式

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

臨時關閉swap

1:查看swapoff的版本

[root@centos8 ~]# swapoff --version
swapoff,來自 util-linux 2.32.1

2:關閉swap

#-a:all,禁用 /proc/swaps 中的所有交換區

[root@centos8 ~]# swapoff -a

3:檢查效果 :

[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1221        1083          20        1451        2273
Swap:             0           0           0

swap的大小為0,已關閉

4:用swapon檢查:

[root@centos8 ~]# swapon -v

輸出為空,表示swap已關閉

臨時關閉swap后啟用swap

1:查看swapon的版本

[root@centos8 ~]# swapon --version
swapon,來自 util-linux 2.32.1

2:啟用 swap

#-a:–all? ? ? ? ? ? ? ? 啟用 /etc/fstab 中的所有交換區

[root@centos8 ~]# swapon -a
查看效果:
[root@centos8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1225        1079          20        1452        2269
Swap:          4047           0        4047

可以看到swap的空間顯示為4G

用swapon -v查看

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

永久關閉swap

查看/etc/fstab

[root@centos8 ~]# more /etc/fstab

找到swap分區的記錄:

...
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=f384615e-7c71-43b0-876c-45e8f08cfa6e /boot                   ext4    defaults        1 2
/dev/mapper/cl-home     /home                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

把加載swap分區的那行記錄注釋掉即可

#/dev/mapper/cl-swap     swap                    swap    defaults        0 0

重啟機器

[root@centos8 ~]# systemctl reboot

再用free -m檢查一下

[liuhongdi@centos8 ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3757        1127        1906          17         722        2370
Swap:             0           0           0

查看linux的版本

[liuhongdi@centos8 ~]$ cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core)

搬瓦工推薦方案

溫馨提醒 如果您有選擇困難癥,直接選中間的 CN2 GIA-E方案,季付 $49.99,多達 12 個機房任意切換
方案 內存 CPU 硬盤 流量/月 帶寬 機房 價格 購買
CN2
(最便宜)
1GB 1核 20GB 1TB 1Gbps DC3 CN2
DC8 ZNET
$49.99/年 直達
CN2 2GB 1核 40GB 2TB 1Gbps $52.99/半年
$99.99/年
直達
CN2 GIA-E
(最推薦)
1GB 2核 20GB 1TB 2.5Gbps DC6 CN2 GIA-E
DC9 CN2 GIA
日本軟銀 JPOS_1
荷蘭 EUNL_9
$49.99/季度
$169.99/年
直達
CN2 GIA-E 2GB 3核 40GB 2TB 2.5Gbps $89.99/季度
$299.99/年
直達
HK 2GB 2核 40GB 0.5TB 1Gbps 中國香港 CN2 GIA $89.99/月
$899.99/年
直達
HK 4GB 4核 80GB 1TB 1Gbps $155.99/月
$1559.99/年
直達
TOKYO 2GB 2核 40GB 0.5TB 1.2Gbps 日本東京 CN2 GIA $89.99/月
$899.99/年
直達
TOKYO 4GB 4核 80GB 1TB 1.2Gbps $155.99/月
$1559.99/年
直達
搬瓦工優惠碼:BWH3HYATVBJW 2022年最新搬瓦工VPS購買圖文教程
贊(0)
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權, 轉載請注明出處。
文章名稱:《Centos系統如何臨時關閉/永久關閉交換分區swap虛擬內存?》
文章鏈接:http://www.lrracing.com/38044.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。