最後更新: 2020-07-15
介紹
將硬盤空間當作 RAM 使以, 以此防止系統出現 "out of memory" 情況
"out of memory" 會觸發 Kernel "OOM killer mechanism"
目錄
- 建立 swap (mkswap)
- 啟用 swap (swapon / swapoff)
- Priority 小實驗 - 自動 priority
- swap on file
- fstab swap settings
- 某 Procces 的 swap 使用情況
- 把 swap 推回 RAM
- Check What's In Swap
- Notes
- More
建立 swap (mkswap)
mkswap -L [label] [partition]
i.e.
mkswap /data/swapfile
Output:
Setting up swapspace version 1, size = 200700 KiB no label, UUID=60ef3f84-a5e4-4ad4-b867-0c8ca39c6ba5
啟用 swap (swapon / swapoff)
/sbin/swapon [-v] [-p priority] <specialfile/device>
顯示當時 swap 情況
swapon # 不帶參數時
NAME TYPE SIZE USED PRIO /dev/sdd6 partition 7.8G 77.7M -2 /dev/zram0 partition 2G 512K 1
Opts:
-a # All devices marked as "swap'' in /etc/fstab
-e # 與 -a 一起用, 功能是略過不存在的 swap device
-p, --priority <prio> # 0 (Lowest) ~ 32767 (Highest); 在 fstab 內用 "pri=value" 設定它
# 當沒有設定時, 就會自動指定一個遞減 -n 值 (-2, -3, -4 ...)
-s # Display swap usage summary by device ( 相當於 cat /proc/swap )
# 單位 byte Filename Type Size Used Priority /var/swap file 102396 0 -1
* PRIO = -1 => Default Value
Column:
- swap file name
- the type of swap space
- the total size
- the amount of space in use (in kilobytes)
- the priority (useful when multiple swap files)
Priority 小實驗 - 自動 priority
swapon
NAME TYPE SIZE USED PRIO
/dev/sdd6 partition 7.8G 9M -2
/dev/zram0 partition 2G 0B -3
swapoff /dev/sdd6 && swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 2G 1.2M -2
swapon /dev/sdd6 && swapon
NAME TYPE SIZE USED PRIO
/dev/sdd6 partition 7.8G 0B -3
/dev/zram0 partition 2G 12.5M -2
swapoff /dev/zram0 && swapon -p 1 /dev/zram0 && swapon
NAME TYPE SIZE USED PRIO
/dev/sdd6 partition 7.8G 4.8M -2
/dev/zram0 partition 2G 6.8M 1
swapoff /dev/sdd6 && swapon /dev/sdd6 && swapon
NAME TYPE SIZE USED PRIO /dev/sdd6 partition 7.8G 0B -2 /dev/zram0 partition 2G 15.6M 1
swap on file
1. 建立一個2GB 的 swap file
dd if=/dev/zero of=/swapfile bs=1M count=2048
Notes
不應用 fallocate, dmesg 會有
swapon: swapfile has holes
2. 保護它
chown root:root /swapfile
chmod 0600 /swapfile
3. format 它
mkswap /swapfile
output
Setting up swapspace version 1, size = 2147479 kB no label, UUID=56adce50-fd10-4a36-ac81-550d891c1eb1
4. 使用它
swapon /swapfile
# checking
dmesg | tail
... Adding 2097148k swap on /swapfile. Priority:-2 extents:2 across:3334140k SSFS
free -m
fstab swap settings
/etc/fstab:
# swap settings UUID=ccda3242-1561-402a-baf6-ea13b883fa2b none swap sw 0 0 UUID=/dev/sdb3 none swap noauto 0 0 /swapfile none swap pri=10 0 0
* pri 值愈高就會愈被使用先
* 多個 Swap 的好處: The kernel itself can stripe swapping on several devices (if you just give them the same priority)
* All devices marked as "swap'' in /etc/fstab are made available, except for those with the "noauto" option.
* the 'sw' option = placeholder = 沒有其他意思
不確定 Device 是否存在時
# SWAP
UUID=ID none swap nofail,x-systemd.device-timeout=15s 0 0
某 Procces 的 swap 使用情況
By /proc
# 找出 pid
pidof kvm
4819
# 找出 swap 的用量
grep VmSwap /proc/4819/status
VmSwap: 12644 kB
By smem
dnf install smem
smem -t -k # -t, --totals; -k, --abbreviate = Show unit suffixes (K, M)
PID User Command Swap USS PSS RSS
456 root /sbin/udevd -d 472.0K 0 1.0K 44.0K
1660 root /sbin/udevd -d 468.0K 0 1.0K 44.0K
...
例子: 有 process 嚴重 swap
把 swap 推回 RAM
* You must have enough physical memory available on the system
swapoff -a & swapon -a
Notes
* You should not use swapon on a file with holes
* Swap over NFS(not allowed by the kernel/need kernel patch), btrfs(cow) do not work
More