Why the Pi keeps swapping
I was running a memory‑hungry Python data‑pipeline on a 4 GB Raspberry Pi 4 when the system started thrashing every few minutes. top was showing swap usage climbing to 90 % while my script was still busy. The Pi’s 4 GB RAM was the hard limit, and the default swap file on the SD card was the culprit.
The culprit
The SD card is the only storage medium on the Pi, but it’s also the slowest. When the kernel starts paging, it writes to the swap file on the card, creating I/O bottlenecks that stall the Python interpreter. The swap file is defined in /etc/fstab:
# /etc/fstab
/swapfile none swap sw 0 0
The default size is 2 GB—fine for light workloads, but a script that loads large dataframes or builds in‑memory indexes will chew through it fast.
Swap on USB
A USB‑3 0 flash drive or SSD can give you a ten‑fold higher throughput than an SD card. Moving swap there frees the SD card for the OS and data, and the kernel can page much faster. The swap handling is agnostic to the underlying block device, so it’s just a configuration tweak.
Implementation
-
Prepare the USB device
# Identify the device lsblk -f # Suppose /dev/sda1 is the USB partition sudo mkfs.ext4 -F /dev/sda1 sudo mkdir /mnt/usb-swap sudo mount /dev/sda1 /mnt/usb-swap -
Create a new swap file
sudo dd if=/dev/zero of=/mnt/usb-swap/swapfile bs=1M count=2048 sudo chmod 600 /mnt/usb-swap/swapfile sudo mkswap /mnt/usb-swap/swapfile -
Update
/etc/fstab# /etc/fstab /mnt/usb-swap/swapfile none swap sw 0 0 -
Activate the new swap
sudo swapoff -a sudo swapon -a -
Verify
swapon --show free -h
Now the Pi uses the USB swap, and the Python script runs without hitting the swap threshold.
Trade‑offs & security
-
Speed vs. wear – USB flash drives have limited write cycles. SSDs mitigate this, but a 2 GB swap file writes only when RAM is exhausted. For most workloads, the benefit outweighs the wear.
-
Persistence – If the USB is removed while the system is running, the kernel will panic. Mount the device with
nofailin/etc/fstabto avoid boot failures:/mnt/usb-swap/swapfile none swap sw,nofail 0 0 -
Encryption – Swap can leak sensitive data. If you’re handling secrets, encrypt the swap file with
cryptsetup:sudo cryptsetup luksFormat /mnt/usb-swap/swapfile sudo cryptsetup open /mnt/usb-swap/swapfile swap_enc sudo mkswap /dev/mapper/swap_enc sudo swapon /dev/mapper/swap_encAdd the mapping to
/etc/crypttaband the swap entry to/etc/fstabaccordingly. -
Kernel support – The swap mechanism is part of the Linux kernel; see the official documentation on swap handling at kernel.org.
Bottom line
Moving swap off the SD card onto a fast USB device is a quick, low‑risk fix that keeps the Pi responsive during heavy Python workloads. It’s a common pattern in homelab setups and a reminder that the default configuration is tuned for general use, not for specialized scripts.
See also
- Rescuing a system that boots to emergency mode because /boot ran out of space
- Solving the USB‑C monitor flicker on my 2022 Dell XPS 13 with Wayland and Xorg
- Silencing the USB Plug‑in Spam That Floods Journalctl.
- When systemd‑journald ate my 2 GB SSD: how I capped it to 200 MB in a single config line
- Why my Borg backup kept failing after upgrading to Ubuntu 24.04 and how I fixed it