Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux Swap Off and On: Commands, Effects, and Safety

To turn Linux swap off temporarily, run sudo swapoff -a; to turn configured swap back on, run sudo swapon -a. These commands affect active swap areas without removing their configuration, but disabling swap can fail or put a memory-constrained system at risk if RAM cannot hold the data moved back from swap.

What swap off and on mean

Swap is disk space that Linux can use to hold memory pages that are not currently needed in physical RAM. It can be a dedicated partition or a file. Swap is not a replacement for adequate RAM: disk access is much slower, and heavy swapping can make a computer sluggish. It can nevertheless help absorb memory pressure and is commonly used to support hibernation, depending on system configuration.

Turning swap off tells the kernel to stop using the selected swap area and, where possible, move its contents back into RAM. Turning it on makes an eligible, configured area available again. Neither operation changes the size of the swap area or permanently edits the system’s configuration by itself.

Check swap before changing it

Before acting, identify which swap areas are active and how much memory is available:

  • swapon --show lists active swap areas, their types, sizes, and use.
  • free -h shows RAM and swap totals and current usage in readable units.
  • cat /proc/swaps provides another view of active swap devices and files.

For example, if swapon --show reports a swap file at /swapfile, you can target just that area with sudo swapoff /swapfile. Check the output and memory figures rather than assuming swap is unused. A system may have several areas, such as a swap partition and a swap file, and disabling one is not the same as disabling all of them.

Commands to turn swap off and on

To disable every currently configured swap area that the system can identify, use:

sudo swapoff -a

To enable swap areas listed in the system’s swap configuration, commonly /etc/fstab, use:

sudo swapon -a

These commands typically need administrator privileges. To act on a specific area instead, provide its path or device, for example sudo swapoff /dev/sda3 or sudo swapon /swapfile. Use the actual path shown by swapon --show; device names vary between machines.

The -a option means “all configured swap areas” for these operations. It does not create a missing swap file, repair an invalid configuration, or necessarily activate an area that is not configured for automatic activation. Afterward, verify the result with swapon --show or free -h.

What happens when swap is disabled

When swap is turned off, Linux must bring pages stored there back into RAM. If available memory is insufficient, the operation may take time or fail with an out-of-memory error. In severe cases, the kernel may terminate processes to recover memory. Closing memory-heavy applications or adding capacity can reduce this risk, but do not assume that a machine with some free memory can safely reclaim all swap: the kernel also needs memory for active workloads and system services.

Disabling swap does not automatically make a system faster. If the machine is already under memory pressure, removing swap can make performance worse or cause processes to be killed. If swap is barely used, turning it off may have little visible effect. Swap usage alone also does not prove that the system is currently short of memory; pages can remain in swap after an earlier period of pressure.

Make swap changes temporary or persistent

The swapoff and swapon commands change the current running system. On a typical system, swap configured in /etc/fstab can be activated again during boot, even if you turned it off manually before rebooting. Thus, a temporary test generally needs no configuration-file edit.

For a lasting change, an administrator must alter the relevant system configuration or the mechanism that manages swap. For an /etc/fstab entry, disabling or removing the entry can prevent swapon -a and boot-time activation from enabling that area. Make a backup, confirm which entry corresponds to the intended swap area, and understand that desktop tools or system services may manage swap separately. Do not delete a swap file or repartition a disk merely to turn swap off for the current session.

Common problems and safety considerations

  • “Cannot allocate memory” or an operation that will not complete: There may not be enough RAM to reclaim the swap contents. Reduce memory use or leave swap enabled; forcing the change is not a safe workaround.
  • “Invalid argument” or “not found”: Check the exact device or file path and whether it is currently active. A stale path or typo will not identify the intended area.
  • Swap turns on again after reboot: That is usually because it remains configured for boot-time activation. A temporary command does not change that configuration.
  • Hibernation stops working: Many Linux hibernation setups need swap space to store the saved memory image, and some require a particular swap device or resume configuration. Check your distribution’s hibernation setup before disabling or changing swap.

On production servers, remote machines, or systems running databases and other memory-intensive workloads, schedule changes carefully and monitor memory and service health. If the goal is to troubleshoot performance, inspect memory pressure and application behavior rather than treating swap use itself as a fault.

Related

Frequently Asked Questions

Does swapoff -a delete my swap file?
No. It deactivates swap areas for the current running system; it does not delete their files or partitions. Deleting one is a separate, potentially destructive operation.

How do I enable swap again?
Run sudo swapon -a to activate configured areas, or sudo swapon /path/to/swapfile for one specific area. Confirm activation with swapon --show.

Can I turn swap off while it is in use?
You can request it, but Linux must reclaim the occupied pages, which requires sufficient RAM. If memory is tight, the command can fail or create serious memory pressure. Check usage and available memory first.

Will turning swap off make Linux faster?
Not necessarily. It may have little effect when swap is mostly idle; under memory pressure, it can worsen responsiveness or cause processes to be killed. Find and address the source of memory pressure before deciding.

Does swap turn back on after restarting?
Usually, if it remains configured for automatic activation, such as through /etc/fstab or a system management service. The command alone normally changes only the current session.

Linux Swap Off and On: Commands, Effects, and Safety
Scroll to top