When you install Ubuntu Linux, the operating system usually sets aside a portion of your hard drive to act as “Swap” space. If your server or desktop runs out of physical RAM, the Linux kernel moves idle data from RAM into the Swap file on the hard drive. This prevents the system from crashing, but because hard drives (even fast NVMe SSDs) are exponentially slower than physical RAM, relying on Swap causes the entire system to grind to an unbearable halt.
For modern systems with massive amounts of RAM (32GB, 64GB, or more), or for Kubernetes clusters and specialized database servers that require strict memory management, Swap is entirely unnecessary and often detrimental. If you want to force your system to rely 100% on physical RAM—ensuring maximum performance and allowing the Out of Memory (OOM) killer to cleanly terminate runaway processes rather than letting them choke the system with disk I/O—you must completely disable Swap.
Disabling Swap Temporarily
You can instantly turn off all active Swap partitions and files without rebooting the system. This is useful for testing how your server performs purely on RAM.
- Open a Terminal session (or connect via SSH).
- Run the following command to immediately disable all Swap spaces:
sudo swapoff -a - To verify that Swap is no longer active, check your memory usage:
free -h - Look at the “Swap:” row. The Total, Used, and Free columns should all read
0B.
Disabling Swap Permanently
The swapoff command only lasts until the next reboot. To permanently strip Swap from your system, you must edit the filesystem table (fstab) so the kernel never mounts it during boot.
- Open the fstab file in a text editor with root privileges:
sudo nano /etc/fstab - Look through the file for any line containing the word
swap. It will usually look something like this:/swapfile none swap sw 0 0 - Comment out the line by placing a hash symbol (
#) at the absolute beginning of it:#/swapfile none swap sw 0 0 - Save the file (Ctrl + O, then Enter) and exit (Ctrl + X).
Upon your next reboot, Ubuntu will boot exclusively into physical RAM. If your system ever exceeds its physical memory limit, the kernel will immediately kill the offending process rather than thrashing your hard drive.