Your Search Bar For Shrewd Tips

How Do I Clear Swap Memory In Linux?


Swap memory in Linux is disk space—or, in some setups, compressed memory—used when the system needs more working memory than it can keep in RAM. If you want to clear swap, the usual method is to turn swap off and then turn it back on. Linux will attempt to move swapped pages back into RAM before deactivating the swap area.

The basic command is sudo swapoff -a && sudo swapon -a. However, clearing swap can fail or cause problems if there is not enough available RAM. Before running it, check how much swap is in use, identify the active swap areas, and make sure the system can handle the temporary memory demand. This guide explains how to clear swap memory safely, what the commands do, and when you should avoid doing it.

What Does It Mean to Clear Swap Memory?

Linux uses swap as an extension of physical memory. When RAM is under pressure, the kernel can move less frequently used memory pages from RAM into a swap area. That area may be a swap partition, a swap file, or compressed RAM such as zram. Moving pages out of RAM helps the system continue running when applications need more memory than is readily available.

Clearing swap does not mean deleting files or erasing the contents of RAM. It means deactivating one or more swap areas so the kernel brings their in-use pages back into RAM. When swap is enabled again, the kernel can use it as needed. The data in swap may remain on the underlying disk after the area is turned off; disabling swap is not a secure erase operation.

Swap usage on its own is not necessarily a sign of a problem. Linux may retain pages in swap even after RAM becomes available, because moving those pages back and forth without a reason can waste system resources. If the computer is responsive and has enough available memory, there may be no need to clear swap.

Check Swap Usage Before Clearing It

Start by checking RAM and swap usage with free -h. The human-readable output includes total, used, and available memory, along with the amount of swap in use. For example:

free -h

To see the active swap areas and their sizes, run:

swapon --show

You can also inspect /proc/swaps, which lists active swap devices and files:

cat /proc/swaps

These checks help you confirm whether swap is enabled and whether the system has enough available RAM to take back the pages currently stored there. If swap usage is high and available RAM is low, do not immediately run swapoff. First consider closing memory-intensive applications or reducing the workload.

How to Clear Swap Memory in Linux

To turn off all active swap areas, run:

sudo swapoff -a

This tells Linux to deactivate all currently active swap areas. The kernel attempts to move pages stored in swap back into RAM. The operation may take time, particularly if a large amount of swap is in use or the storage device is slow. Avoid interrupting the process or restarting the machine while it is running.

After swap has been turned off, enable the configured swap areas again with:

sudo swapon -a

The -a option tells swapon to enable swap entries configured in /etc/fstab. On many systems, the combined command is:

sudo swapoff -a && sudo swapon -a

The && means that swapon -a runs only if swapoff -a succeeds. This is generally safer than placing the commands on separate lines and enabling swap regardless of whether deactivation completed successfully.

Once the commands finish, check the result:

free -h
swapon --show

Swap usage may show as zero immediately after the operation, but Linux can use swap again whenever memory pressure makes it useful. Clearing swap does not prevent future swapping.

Clear a Specific Swap Area

If your system has multiple swap areas, you may want to turn off only one. First find its name with swapon --show. A swap area might appear as a device such as /dev/sda3 or as a file such as /swapfile. Then use its exact path:

sudo swapoff /swapfile
sudo swapon /swapfile

Replace /swapfile with the path shown on your system. For a swap partition, use the displayed device path instead. Turning off a single area may reduce risk when you have several areas available, but the system still needs enough RAM to move that area’s in-use pages back into memory.

If you turn off a swap area permanently, update the relevant configuration as well. For traditional setups, swap may be listed in /etc/fstab. Removing or commenting out an entry changes what swapon -a enables, but edit system configuration carefully and keep a backup. On systems managed by other tools, such as a zram service or systemd generator, use the appropriate configuration for that setup.

Important Safety Considerations

The main risk when clearing swap is running out of RAM. Turning off swap requires Linux to bring swapped pages back into physical memory. If there is not enough available memory, the operation may fail. In a severe memory shortage, the kernel’s out-of-memory handling may terminate processes, or the system may become unresponsive.

Before clearing swap on a busy server or workstation, check the available memory and consider whether important applications can be paused. It is safer to clear swap during a quiet period than during a memory-intensive workload. Keep a way to access the machine if it is remote, since a system under memory pressure may become difficult to manage.

Use these precautions:

  • Check free -h and swapon --show before starting.
  • Close or stop applications that are consuming large amounts of memory, if practical.
  • Do not clear swap during heavy workloads unless you understand the memory requirements.
  • Run the commands with appropriate administrative privileges.
  • Check swap status again after the operation.

If swapoff reports that there is not enough memory, do not repeatedly retry without changing the situation. Reduce memory use, add or enable additional capacity where appropriate, or wait until the system has more available RAM. On a system where swap is essential to keeping workloads alive, turning it off may be the wrong solution.

What If Swap Is Still in Use Afterward?

If swap usage returns after you run swapon -a, the kernel may have moved pages into swap again because the system is under memory pressure. Review the workload and check memory use with tools such as top or htop. Look for applications consuming unusually large amounts of memory, but avoid stopping processes unless you know what they do.

You can also check system logs for memory-related events, including out-of-memory messages. The exact command depends on the distribution and logging setup; on many systemd-based systems, journalctl -k displays kernel messages. If swap usage grows continually, investigate the cause instead of repeatedly clearing swap. The underlying issue could be a workload that needs more memory, a memory leak, or a configuration that does not match the machine’s available RAM.

Do not confuse clearing swap with clearing the Linux page cache. Commands that drop filesystem caches, such as writing to /proc/sys/vm/drop_caches, do not clear swap and are not a general memory-cleanup solution. Linux uses available RAM for useful caching and normally reclaims cache when applications need memory.

Does Clearing Swap Improve Performance?

Not necessarily. If a system is slow because it is repeatedly swapping pages between RAM and storage, the real issue is ongoing memory pressure. Clearing swap may temporarily change the swap usage figure, but it does not provide additional physical memory or fix the workload that caused the pressure. The system may start using swap again soon afterward.

On the other hand, an administrator may choose to clear swap during troubleshooting or maintenance to observe memory behavior from a known starting point. That can be useful when done carefully, but it should not be treated as routine optimization. Monitor the system before and after the change, and make decisions based on responsiveness, available memory, and workload needs—not just a goal of showing zero swap usage.

Can You Securely Erase Swap Contents?

swapoff disables swap; it does not guarantee that old data has been erased from the storage device. If your goal is to protect sensitive information, treat swap as potentially containing sensitive data and plan for encryption. Many Linux installations support encrypted swap, and some systems use random encryption keys for swap at boot. The correct approach depends on your distribution and storage configuration.

Do not assume that deleting a swap file or running swapoff securely removes its previous contents. Secure deletion on modern storage can be complicated by journaling filesystems, solid-state drive behavior, snapshots, and wear leveling. For security-sensitive systems, use a properly configured encryption strategy and follow the storage vendor’s or distribution’s guidance.

Conclusion

To clear swap memory in Linux, check current memory usage, then run sudo swapoff -a && sudo swapon -a. This turns off active swap areas, attempts to move their pages back into RAM, and re-enables the swap areas configured on the system. Verify the result with free -h and swapon --show.

Only clear swap when the system has enough available memory to handle the operation. If swap quickly fills again, investigate memory pressure rather than repeatedly resetting it. Remember that disabling swap is not a secure wipe, and that swap can be useful for keeping a Linux system stable under memory pressure.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →