In traditional Linux architecture, “Swap” space is a dedicated partition (or file) on your hard drive that acts as an emergency overflow valve for your RAM. If your server has 8GB of RAM and an application tries to use 9GB, the Linux kernel will frantically move 1GB of inactive memory out of the fast RAM chips and write it onto the slow physical hard drive (into the Swap space) to prevent the system from crashing.
While this prevents an immediate Out-Of-Memory (OOM) crash, it creates a terrifying performance bottleneck known as “Thrashing.” Because hard drives (even NVMe SSDs) are exponentially slower than RAM, the server will grind to an absolute halt. The CPU becomes 100% locked up just trying to read and write memory back and forth to the disk. Your websites will timeout, SSH will become unresponsive, and you will ultimately have to hard-reboot the machine anyway.
If you are running a modern Kubernetes cluster, a high-performance database (like Elasticsearch), or if you simply have enough RAM (e.g., 64GB) and would rather an application quickly crash and restart than take down the entire server with I/O thrashing, you must completely disable Swap space.
Step 1: Verify Swap is Active
Before you disable it, check if the kernel is currently using it.
- Log into your server via SSH.
- Run the following command:
free -h - Look at the row labeled Swap:. If the “Total” column shows a number greater than 0, Swap is active.
Step 2: Disable Swap Instantly (Temporary)
You can command the kernel to immediately flush the hard drive and move everything back into RAM (if there is space).
- Execute this command as root:
sudo swapoff -a
Note: If your RAM is currently 99% full, running this command will instantly trigger the OOM killer, which will forcefully terminate your largest application to make room.
Run free -h again. The Swap row should now read 0 across the board. However, this is only temporary; if you reboot the server, Swap will turn back on.
Step 3: Disable Swap Permanently (Persistent)
To ensure Swap never activates on boot, you must remove its entry from the File System Table.
- Open the fstab file in a text editor:
sudo nano /etc/fstab - Carefully scroll down the list of hard drives until you find the line that contains the word swap (e.g.,
/swapfile none swap sw 0 0orUUID=xxx none swap sw 0 0). - Do not delete the line. Instead, place a
#symbol at the very beginning of the line to “comment it out” (deactivate it). - Press Ctrl+O then Enter to save, and Ctrl+X to exit.
The Result
Your Linux server is now running in a pure RAM environment. It will execute code significantly faster with zero risk of SSD thrashing. If a rogue PHP script attempts to consume 100% of your memory, the Linux OOM killer will instantly step in, instantly execute the rogue script, and keep the rest of the server running perfectly smoothly.