In Ubuntu Linux, the operating system uses a concept called “swap” space to prevent out-of-memory crashes. If your server runs out of physical RAM (Memory), the Linux kernel begins moving chunks of idle memory (pages) from the fast RAM onto a dedicated file or partition on your slower hard drive. This prevents the server from crashing instantly when under heavy load.
While swap is a vital safety net on low-end hardware, it is often detrimental on high-performance servers, Kubernetes clusters, or systems running massive in-memory databases like Redis or Elasticsearch. If your server starts aggressively swapping data to a slow mechanical hard drive (or even an SSD), the entire system will become incredibly sluggish (a state known as “thrashing”). Furthermore, for container orchestration systems like Kubernetes, having swap enabled completely breaks the scheduler’s ability to accurately monitor and restrict memory limits. If you have plenty of physical RAM and want to force the kernel to strictly manage memory (killing processes when RAM is full rather than letting them slowly thrash the disk), you must completely disable swap.
Disabling Swap Temporarily
If you just want to test your server’s performance without virtual memory, you can turn off swap for the current session. It will turn back on when you reboot.
- Open a Terminal session (or connect to your server via SSH).
- Run the following command with root privileges to instantly unmount all swap space:
sudo swapoff -a - You can verify that swap is completely disabled by checking the system memory usage:
free -h - Look at the row labelled “Swap.” The “Total” value should now be exactly
0B.
Disabling Swap Permanently
To ensure that the Linux kernel never mounts a swap file or partition again upon reboot, you must remove its entry from the central filesystem table (fstab).
- Open the
/etc/fstabconfiguration file in a text editor with root privileges:sudo nano /etc/fstab - Carefully scroll through the file and look for any line containing the word
swap. - Instead of deleting the line, type a # symbol at the absolute beginning of the line to comment it out. (For example:
#/swap.img none swap sw 0 0). - Save the file (Ctrl + O, then Enter) and exit the editor (Ctrl + X).
Upon your next reboot, your Ubuntu server will run entirely in physical RAM. Ensure that your critical applications have strict memory limits configured, as the kernel’s Out-Of-Memory (OOM) killer will immediately terminate any process that attempts to consume more RAM than is physically available.