Last Updated on 2023-12-10 by Clay
What is Swap?
Swap space plays an important role in Linux operating systems. It can be understood as a virtual memory space, where its name implies the act of swapping. Swap allows for temporarily writing less frequently used data to a fixed hard disk space to ensure stable system operation when the physical memory (RAM) is insufficient — this space is Swap.
If the Swap space is set too small, when the system’s physical memory is completely exhausted, not to mention the reduction in system performance, it might even lead to programs not running or system crashes.
Commands to Expand Swap Space
The system may be used the same name space to be swap, so we need to turn it off first. (Assume it is named /swapfile
)
sudo swapoff /swapfile
And then we can use fallocate
to create a swap file to overwrite it. For example, if we want to create a 4GB Swap:
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
Additionally, to convert a file or device into Swap space, you can use mkswap
.
sudo mkswap /swapfile
sudo swapon /swapfile
And to ensure the Swap space remains effective after system reboot, we might need to edit the /etc/fstab
file and add the following setting: /swapfile swap swap defaults 0 0
The above steps end, you will see the swap is expanded.