To disable swap on Linux immediately, run sudo swapoff -a. To keep it disabled after reboot, also remove or comment out its swap entry in /etc/fstab and disable any systemd swap unit or other service that recreates it.
What swap does and why disable it
Swap is disk space that Linux can use to hold memory pages that are not currently needed in physical RAM. It may be a dedicated partition, a swap file, or—in some setups—swap managed by a service. Swap can help a system keep running when RAM is under pressure, but storage is much slower than RAM. Heavy swapping can therefore make a machine feel sluggish.
Disabling swap can make sense for testing, troubleshooting, certain performance or latency requirements, or systems where the administrator has deliberately chosen to run without it. It is not automatically a performance improvement: swap can absorb occasional memory pressure, and removing it reduces the system’s options when RAM runs short. Linux does not require swap simply to boot or run, but the consequences of having none depend on workload and available memory.
Disable swap until the next reboot
For a temporary change, use:
sudo swapoff -a
The -a option tells swapoff to turn off all currently active swap areas. You can inspect the result with either command:
swapon --show
free -h
If swapon --show lists no active areas, swap is off. In free -h, the Swap row should show zero total space. These commands report current state; they do not make the change persistent, so swap can return at reboot.
Turning swap off may require Linux to move its contents back into RAM. If memory is already heavily used, the operation can fail or put the machine under severe pressure. Check available memory first, close memory-intensive applications if needed, and avoid forcing the change on a system that is close to exhausting RAM.
Keep swap disabled after reboot
On many traditional Linux installations, swap is enabled by an entry in /etc/fstab. Inspect the file before editing it:
grep -nE '\sswap\s' /etc/fstab
If you find a swap entry, back up the file and comment out that specific line by placing # at its beginning. For example, an entry such as /swapfile none swap sw 0 0 can be changed to #/swapfile none swap sw 0 0. A swap partition entry may instead use a UUID. Comment out only the relevant swap entry; do not remove unrelated mount lines.
Then disable active swap now, or reboot after saving the change:
sudo swapoff -a
swapon --show
Use sudoedit /etc/fstab or another editor with administrator privileges, and keep the backup until you have confirmed the system boots as expected. Editing fstab affects future activation; it does not by itself turn off swap already in use.
Check for systemd and other swap managers
Some systems activate swap through systemd units or generated configuration rather than a simple fstab entry. List active swap units with:
systemctl list-units --type=swap
A named unit may be managed by a service or distribution-specific setup. If it is a regular systemd unit, stopping it can disable it for the current session and disabling it can prevent activation at boot. Use the exact unit name shown by the command, and investigate how it was created before changing it. A generated unit may be recreated from its source configuration, so disabling the generated unit alone might not persist.
Other mechanisms can also enable swap, including a swap-management service, a desktop configuration tool, or a container or orchestration setup. If swap returns after reboot, inspect active units and configuration rather than repeatedly running swapoff. On systems using zram, swap may be created in compressed RAM and managed by a dedicated service; identify that service before disabling it.
Risks and situations to consider
Without swap, a sudden memory spike has less room to be accommodated. If the system cannot satisfy memory demand, Linux may invoke the out-of-memory (OOM) killer, which terminates processes to recover memory. That can cause an application or service to fail; it does not mean swap itself would have guaranteed recovery, but swap may provide a buffer in some situations.
- Check workload: Review memory use under normal and peak load, not just while the machine is idle. Servers, build jobs, databases, and virtual machines can have sharp peaks.
- Check hibernation needs: Some hibernation configurations rely on swap space large enough to store a memory image. Requirements depend on the system and setup. Disabling or removing swap may prevent hibernation from working.
- Check memory policy: Containers and services may have their own memory limits. Disabling host swap does not remove those limits or prevent a process from being killed when a limit is reached.
- Plan recovery: Keep a way to restore the previous configuration, especially on a remote server where a reboot or memory-related failure could cut off access.
The setting vm.swappiness is often confused with disabling swap. It influences how readily the kernel reclaims memory by swapping; it does not turn swap off. A low value is not equivalent to swapoff, and setting it to zero does not reliably mean that swap can never be used. To disable active swap, use swapoff and address the configuration that activates it.
Swap files, partitions, and zram
The command swapoff -a works across active swap areas regardless of whether they are files or partitions. To identify them, run swapon --show; the output includes their paths and types. You can also use cat /proc/swaps for a kernel view of active swap.
Do not delete a swap file or alter a swap partition while it is active. First turn it off, then remove or change its persistent configuration if that is your goal. A swap partition may also be referenced by UUID in /etc/fstab, so deleting the device alone does not clean up the configuration. For zram, disabling its service is usually more appropriate than trying to treat it like a disk-based swap file.
Related
Frequently Asked Questions
Does swapoff -a permanently disable swap?
No. It disables active swap for the current session. To persist the change, update the configuration that enables swap, commonly /etc/fstab or a systemd or zram service.
How do I check whether swap is disabled?
Run swapon --show. No listed active areas means swap is currently off. free -h also shows the total swap available to the system.
Will disabling swap make Linux faster?
Not necessarily. It may avoid slow disk swapping in a particular workload, but it also removes a buffer against memory pressure. Measure performance and memory use on the actual system rather than assuming swap is always harmful.
Can I disable swap on a server?
Yes, if the workload and memory capacity support that choice. Consider peak memory use, service limits, hibernation requirements, and the possibility of OOM-killer actions before making it permanent.
Why does swap come back after reboot?
A boot-time configuration is still enabling it. Check /etc/fstab, systemd swap units, and services that manage swap, including zram tools. Disable the source configuration rather than only stopping swap for the current session.