Blog

Disable swap on Raspberry Pi OS (Bookworm)

Linux16 November 2024

Swap is a useful feature in Linux that extends memory by using disk space, but on a Raspberry Pi with limited storage and high I/O wear concerns, disabling swap can improve system longevity and performance. This guide will show you how to disable swap on Raspberry Pi OS (Bookworm).

Why Disable Swap?

  1. Preserve SD Card Lifespan Swap relies heavily on disk writes, which can wear out your SD card over time. Disabling swap is especially important if your Raspberry Pi is using a microSD card, as frequent writes can lead to data corruption or card failure over time.

  2. Improve Performance On systems with adequate RAM, using swap can slow down performance due to slower disk access speeds.

  3. Simplify Resource Management Removing swap ensures that all operations stay within the available RAM, avoiding unpredictable delays caused by disk I/O.

How to Disable Swap

Follow these steps to permanently disable swap on Raspberry Pi OS, ensuring that it remains disabled even after the Raspberry Pi is rebooted.

1. Temporarily Disable Swap

First, turn off the swap functionality for the current session using the following command:

sudo dphys-swapfile swapoff

This stops the system from using the swap file immediately. However, this change will not persist after a reboot.

2. Permanently Disable Swap

To ensure swap is disabled every time the system boots:

a. Stop the dphys-swapfile Service

Disable the service that manages swap files:

sudo systemctl disable dphys-swapfile

b. Comment Out the Swapfile Configuration

Edit the dphys-swapfile configuration file:

sudo nano /etc/dphys-swapfile

Locate the following line:

CONF_SWAPFILE=/var/swap

Comment it out by adding a # at the beginning of the line (if not already commented out):

#CONF_SWAPFILE=/var/swap

Save the file and exit (Ctrl+O, Enter, then Ctrl+X).

3. Remove the Swap File (Optional)

If you want to reclaim the disk space used by the swap file, delete it:

sudo rm /var/swap

4. Reboot the System

Reboot your Raspberry Pi to apply the changes:

sudo reboot

5. Verify Swap Is Disabled

After the reboot, confirm that swap is disabled using the free command:

free -h

The output should show 0B for both total and used under the "Swap" section:

              total        used        free      shared  buff/cache   available
Mem:          416Mi       161Mi       106Mi       2.6Mi       204Mi       255Mi
Swap:           0B          0B          0B

Conclusion

Disabling swap on Raspberry Pi OS (Bookworm) is a straightforward process that can enhance your system performance and protect your microSD card from potential corruption. If you find that your system needs more memory, consider upgrading your Raspberry Pi or optimizing your applications to reduce RAM usage.

Pro Tip: Monitor your system's RAM usage with htop to ensure it runs smoothly without swap.

By disabling swap, you can enjoy a more reliable and efficient Raspberry Pi setup, especially when using a microSD card.