Chris Dzombak

sharing preview • dzombak.com

Stop using the Raspberry Pi's SD card for swap

Using your Pi's SD card for swap space will, in time, kill the SD card.

Stop using the Raspberry Pi's SD card for swap

Part of the Raspberry Pi Reliability series.

Raspberry Pi OS, on a fresh installation, will allocate some amount of swap space on the SD card by default.

Using your Pi’s SD card for swap space will, in time, kill the SD card. On any Pi, I recommend disabling swap on the SD card.

If the system needs swap space to function properly, I allocate swap space on an inexpensive USB thumb drive, with the knowledge that this use will eventually kill the thumb drive.

If the system does not need swap

To completely disable swap on Raspberry Pi OS:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

If you’re certain you won’t want to reenable swap in the near future, you can completely remove the dphys-swapfile manager with sudo apt purge dphys-swapfile -y.

To verify this change, reboot the Pi and run free -m, which should show all 0s in the swap row:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:            430          33         333           6          63         342
Swap:             0           0           0

If the system needs swap

If this system needs swap to function properly, we’re going to use an external USB thumb drive for it.

This will absolutely eventually kill the USB drive, but at least that’ll be easy to replace without rebuilding the entire Pi.

Run cat /boot/cmdline.txt. If the file includes the word noswap, edit /boot/cmdline.txt and remove noswap.

Connect your sacrificial USB thumb drive and use lsblk to figure out its device name. On the Pi I’m using, this is /dev/sda; if yours differs, use its name in place of /dev/sda in the following commands.

Run sudo cfdisk /dev/sda and:

  1. Select gpt if prompted to select a partition table type.
  2. Create a new primary Linux filesystem partition.
  3. Write & quit.

(This article covers those steps in more detail, including screenshots. If you follow that guide, stop after step 7; don’t proceed to mkfs.ext4.)

We’re going to turn that partition into a classic Linux swap partition using mkswap. Run sudo mkswap -f /dev/sda1, and note the UUID included in its output:

# sudo mkswap -f /dev/sda1
Setting up swapspace version 1, size = 7.3 GiB (7810555904 bytes)
no label, UUID=1c1b6b47-ad57-43ad-a6ab-f1c2a64a88d1

Then, edit /etc/fstab to add a line for this new swap partition. Replace the UUID below with the one you noted in the previous step:

UUID=1c1b6b47-ad57-43ad-a6ab-f1c2a64a88d1 swap	swap	defaults	0	0

You can enable the swap partition immediately with sudo swapon /dev/sda1, or reboot the Pi and wait. In either case, verify it’s operational using the free command:

$ free -mh
               total        used        free      shared  buff/cache   available
Mem:           999Mi       231Mi       522Mi        42Mi       244Mi       700Mi
Swap:          7.8Gi          0B       7.8Gi

On Raspberry Pi OS, we’ll disable the service that manages the SD card-based swap file. We don’t need it if we’re using an external drive:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove
sudo apt purge dphys-swapfile -y

Finally, we’ll adjust the system’s vm.swappiness setting. Reducing this from its default value (of 100, IIRC) will force the system to use swap only when it really needs to.

Create a new file, /etc/sysctl.d/90-swappiness.conf, with the following content:

vm.swappiness=5

This setting will take effect after a reboot; to apply it immediately to a running system, run sudo sysctl -w vm.swappiness=5.

A note on Armbian

Armbian, an alternative Linux distribution for Raspberry Pi and other single-board computers, does not allocate any SD card-based swap by default, so unless you’ve customized your installation there’s nothing to disable.

There is a zram-based swap partition, which I recall seeing in /etc/fstab. That is a RAM-based compressed block storage device; using it as swap space effectively tiers your RAM into two parts: fast+uncompressed and slower+compressed. I believe the intent here is to increase the effective available memory on RAM-limited boards without touching the SD card, and on my Orange Pis running Armbian I leave this setup as-is.


See Also: Considerations for a long-running Raspberry Pi.