Linux 的 Swap 類似 Windows 下的虛擬記憶體, 當記憶體不足時, 把 Swap 的空間當作暫時記憶體使用, 從而解決記憶體不足的問題。如果 Swap 空間不夠用, 需要增加 Swap 空間的話, 可以使用 Swap Partition 或 Swap 檔案實現, 以下會介紹在 RHEL, CentOS 及 Fedora 透過 Swap 檔案增加 Swap 空間。
在開始前先檢查一些目前系統是否有設定 Swap 空間, 可以用 "swapon -s" 指令檢查:
上看到目前系統有 2 GB Swap 空間, 使用了 37MB.
然後再看看系統各分割區的可用空間:
# df -h
假設我要在 / 下面建立 4GB 的 Swap 檔案, 可以用 fallocate 指令, 語法如下:
# fallocate -l 4G /swapfile
建立好 /swapfile 檔案後, 然後需要格式化這個 Swap 檔案, 並設定系統使用這個檔案作為 Swap 空間:
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile
# mkswap /swapfile
# swapon /swapfile
設定完成後, 可以再用 "swapon -s" 檢查設定是否生效:
現在系統已經可以使用 /swapfile 用為 Swap 空間, 但當重新啟動系統後, 設定就會失效, 要在重新開機後自動使用 /swapfile 作為 Swap 空間, 需要修改 /etc/fstab 檔案:
# vi /etc/fstab
在最低下加下這行:
儲存後就完成設定了。
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases when running CentOS on a virtual machine a swap partition is not present so the only option is to create a swap file.
This tutorial explains how to add a swap file on CentOS 7 systems.
Before You Begin
Before proceeding with this tutorial, check if your CentOS installation already has swap enabled by typing:
sudo swapon --show
If the output is empty, it means that your system does not have swap space enabled.
Otherwise if you get something like below, you already have swap enabled on your machine.
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 1.5G 0B -1
Although possible, it is not common to have multiple swap spaces on a single machine.
Creating a Swap File
The user you are logged in as must have sudo privileges to be able to activate swap. In this guide, we will add
1G
of swap, if you want to add more swap, replace 1G
with the size of the swap space you need.
Follow the steps below to add swap space on a CentOS 7 system.
- First, create a file which will be used as swap space:
sudo fallocate -l 1G /swapfile
If thefallocate
utility is not available on your system or you get an error message sayingfallocate failed: Operation not supported
, use the following command to create the swap file:sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
- Ensure that only the root user can read and write the swap file:
sudo chmod 600 /swapfile
- Next, set up a Linux swap area on the file:
sudo mkswap /swapfile
- Run the following command to activate the swap:
sudo swapon /swapfile
Make the change permanent by opening the/etc/fstab
file:sudo nano /etc/fstab
and pasting the following line:/etc/fstab/swapfile swap swap defaults 0 0
- Verify that the swap is active by using either the
swapon
or thefree
command as shown below:sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 1024M 507.4M -1
sudo free -h
total used free shared buff/cache available Mem: 488M 158M 83M 2.3M 246M 217M Swap: 1.0G 506M 517M
Adjusting the Swappiness Value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value on CentOS 7 is 30. You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness
30
While the swappiness value of 30 is OK for desktop and development machines, for production servers you may need to set a lower value.
For example, to set the swappiness value to 10, type:
sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the
/etc/sysctl.conf
file:
/etc/sysctl.conf
vm.swappiness=10
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
Removing a Swap File
To deactivate and remove the swap file, follow these steps:
- Start by deactivating the swap space by typing:
sudo swapoff -v /swapfile
- Next, remove the swap file entry
/swapfile swap swap defaults 0 0
from the/etc/fstab
file. - Finally, delete the actual swapfile file:
sudo rm /swapfile
Conclusion
You have learned how to create a swap file and activate and configure swap space on your CentOS 7 system.
If you hit a problem or have feedback, leave a comment below.