The Purpose of Swap Space
In a Linux environment, Swap space acts as a safety valve for your physical RAM. When a server runs out of physical memory, the Linux kernel begins moving inactive memory pages from RAM to the hard drive (into the Swap space). This prevents the dreaded “Out of Memory” (OOM) killer from randomly terminating critical services like your web server or database.
While relying heavily on Swap will severely degrade system performance (because hard drives are infinitely slower than RAM), having no Swap at all is highly dangerous. System administrators frequently need to add, remove, or modify Swap space dynamically without rebooting the server. This is accomplished using the swapon and swapoff commands.
Checking Current Swap Status
Before making any changes, you must see exactly what Swap space is currently active on your system.
sudo swapon --show
This command outputs a table showing the active Swap file or partition, its total size, and how much is currently in use.
Disabling Swap Safely
If you need to resize a Swap partition, or if you simply want to flush all the stale data currently sitting in Swap back into your physical RAM (assuming you have enough free RAM), you must disable the Swap space.
To forcefully disable all active Swap spaces on the server, use the -a (all) flag:
sudo swapoff -a
Warning: This command may take several minutes to execute. The kernel must read every single byte of data currently stored in the Swap file on the hard drive and write it back into physical RAM. Do not interrupt this process. If your physical RAM is completely full, this command will fail and crash the system.
Creating a New Swap File
Modern Linux systems generally use Swap files rather than dedicated Swap partitions, as files are much easier to resize. Let’s create a brand-new 4 Gigabyte Swap file.
First, allocate the 4GB file using the fallocate command:
sudo fallocate -l 4G /swapfile
Second, you must secure the file. Swap contains raw memory dumps, which could include plaintext passwords. Only the root user should be able to read it.
sudo chmod 600 /swapfile
Third, you must format the raw file specifically as a Linux Swap structure using the mkswap command:
sudo mkswap /swapfile
Enabling the New Swap
Now that the 4GB file is formatted and secured, you must tell the Linux kernel to begin using it.
Use the swapon command targeting the specific file:
sudo swapon /swapfile
If you run sudo swapon --show again, you will instantly see your new 4GB capacity added to the system pool.
Making it Permanent
The swapon command only activates the Swap for the current session. If you reboot the server, the Swap space will be disabled. To make the change permanent, you must add an entry to the Filesystem Table (fstab).
Open /etc/fstab in a text editor (like nano) and append the following line to the bottom of the file:
/swapfile none swap sw 0 0
Your server will now automatically mount the Swap file upon every subsequent boot, ensuring your memory safety net is always active.