Linux Performance: Almost Always Add Swap Space — Part 3: No SWAP

Previous articles covered traditional swap and alternative methods like ZRAM to improve Linux memory management and performance. However, sometimes removing swap altogether can sometimes work well, especially in systems with plenty of RAM dedicated to critical workloads. This article looks at scenarios where disabling swap makes sense, then walks through setting the swappiness parameter to zero and what that means.

Removing Swap

Swap space typically adds flexibility. When memory runs low, the kernel can move some data from RAM to disk-based swap. For resource-constrained systems, swap is very beneficial.

But in certain environments with plenty of free RAM and workloads tightly tuned for memory, removing swap can give you predictable performance, reduce disk I/O overhead and keep applications in active memory.

Removing swap completely on Linux

With plenty of free RAM and workloads tightly tuned for memory, removing swap can improve Linux performance.

Removing swap on Linux typically involves a few steps. First, turn it off in the running system by entering:

swapoff -a

Then edit the /etc/fstab file and remove or comment out any entries that refer to a swap partition or swap file. This prevents swap from being enabled the next time the server reboots.

Disabling swap eliminates one of the safety nets against unexpected memory demands.

If you use a dedicated swap partition, you can optionally reclaim or repurpose that partition after it’s disabled. If you use a swap file, remove (or rename) the file once it’s no longer in use.

Remember to ensure your system has enough RAM to handle its workload before going swap-free!

Setting the Swappiness Parameter to Zero

Swappiness controls how aggressively the kernel moves application pages to swap. Setting it to zero means pages stay in memory as long as possible. To change the swappiness parameter at runtime, enter:

echo 0 >/proc/sys/vm/swappiness

This setting takes effect immediately, but is temporary and won’t survive a reboot. To make it permanent, open or create a file in /etc/sysctl.d/ (or edit /etc/sysctl.conf) and add:

vm.swappiness=0

Finally, apply the setting:

sysctl -p

From that point forward, swappiness remains zero after every reboot.

I recommend using swappiness=0 as the safer baseline. Avoid removing swap completely unless you are certain your environment can meet all memory demands without a swap safety net.

When swappiness is zero (swappiness = 0), the kernel will try to reclaim memory from cache before moving application pages to disk. This avoids early swapping and ensures if your system has enough memory, pages stay in RAM, free from swap-induced latency.

Depending on the application, this can give you a performance boost if your workload benefits from maximum in-memory data and can cause issues if memory demand unexpectedly exceeds capacity.

When Does No Swap Help?

Three benefits of removing swap or setting swappiness to 0. First, systems with plenty of memory for apps and disk caching — like big database servers or high-performance computing clusters — get very little benefit from having swap. These systems have so much unused RAM that paging to disk never happens anyway.

Second, if your workload is stable and well-defined — like a production environment that’s been well-tuned, and memory usage is predictable — the risk of OOM is very low. In this case, disabling swap eliminates the overhead of unnecessary disk paging.

Third, latency sensitive or near real-time processes suffer from unpredictable I/O delays whenever the kernel swaps. By keeping data in physical memory, you eliminate this source of jitter and for more consistent performance.

Also see: Are you measuring Linux web server memory usage correctly?

When Does No Swap Hurt?

How does your system manage being low on free memory?
Do not remove swap or set swappiness to 0 if your system has low available memory.

Running a completely swapless system removes the safety net if RAM usage spikes unexpectedly. Hitting the memory limit without swap can result in the kernel’s OOM killer terminating critical processes. For hardware with limited memory or high variability in workload memory demands, you should rely on swap space or alternative solutions like ZRAM to avoid system instability.

Also see: Free vs. Available Memory in Linux.

Conclusion

This wraps up our look at instances of where you should “almost always add swap space”, as well as when “no swap” is beneficial.

Previous articles

Removing swap may seem risky, but for certain servers or applications with ample memory and predictable usage, it can boost performance and predictability. Setting swappiness = 0 effectively prevents application pages from being swapped to disk, ensuring that disk I/O remains minimized in normal operations. Keep a close eye on memory usage and be prepared to reintroduce swap if demand changes.

As always, monitor your system metrics, run tests, and adjust your approach based on real-world data.

Tags: , , ,



Top ↑