Why my 4GB Raspberry Pi 4 keeps swapping during a Python script – and how I fixed it by moving swap to USB

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

  1. 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
    
  2. 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
    
  3. Update /etc/fstab

    # /etc/fstab
    /mnt/usb-swap/swapfile none swap sw 0 0
    
  4. Activate the new swap

    sudo swapoff -a
    sudo swapon -a
    
  5. 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 nofail in /etc/fstab to 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_enc
    

    Add the mapping to /etc/crypttab and the swap entry to /etc/fstab accordingly.

  • 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