Skip to content

Optimizing Swap with ZRAM on Raspberry Pi OS

By default on older Raspberry Pi OS versions, the system used a swap file on the SD card. This caused poor performance and severe disk wear. Fortunately, starting with Raspberry Pi OS Bookworm (Debian 12), ZRAM swap is enabled by default on most installations.

ZRAM creates a compressed block device in RAM, routing swap pages into memory rather than writing them to the flash storage. This guide covers how to verify, manage, and optimize this default configuration, as well as how to set it up manually on legacy systems.

Understanding Swap on Raspberry Pi

When your Raspberry Pi runs out of physical RAM, the Linux kernel moves inactive memory pages to swap space — a designated area used as overflow. Without swap, out-of-memory (OOM) situations crash your programs.

Swap Technologies Compared

Feature SD Card Swap (Legacy Default) ZRAM (Modern Default) ZSWAP
Location SD card / USB drive Compressed in RAM RAM cache + disk backend
Speed Slow (5-15 MB/s random) Fast (memory speed) Fast (cache hit) / Slow (miss)
SD Card Wear High None Reduced
Effective Size 1:1 (100MB uses 100MB disk) ~2:1 to 4:1 compression ~2:1 to 3:1 compression
Complexity Simple Simple Moderate
Best For Legacy installations Pi 3, 4, 5, and Zero 2 W Pi 4/5 with SSD backend

Verifying Your Current ZRAM Setup

Since ZRAM is active by default on Bookworm, check if it is already running before modifying any files:

1
2
3
4
5
6
# Check active swap devices
swapon --show
# Output should display /dev/zram0 partition

# Check ZRAM configuration and statistics
zramctl

You should see output similar to this:

NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4          1.9G    0B     0B     4K       4 [SWAP]
If you see /dev/zram0 listed with a mountpoint of [SWAP], your system is already utilizing ZRAM swap!

Optimizing and Customizing ZRAM on Bookworm

If you want to adjust the ZRAM size or change the compression algorithm (e.g. from lz4 to the higher-ratio zstd), follow the configuration steps depending on the service managing your ZRAM.

Method 1: Using zram-generator (Modern Standard)

Modern installations use systemd-zram-generator to define swap parameters.

Step 1: Edit the configuration file

Create or edit /etc/systemd/zram-generator.conf:

sudo nano /etc/systemd/zram-generator.conf

Step 2: Configure ZRAM parameters

Add the following configuration (adjust sizes and algorithms as desired):

[zram0]
# Compression algorithm (zstd provides the best compression ratio)
compression-algorithm = zstd

# Size of the ZRAM device relative to total memory (e.g., ram / 2 for 50%)
# You can also use absolute values like 2048 (in megabytes)
zram-size = ram / 2

# Swap priority
swap-priority = 100

Step 3: Apply the changes

Reload the daemon and restart the ZRAM swap setup service:

1
2
3
4
5
sudo systemctl daemon-reload
sudo systemctl restart systemd-zram-setup@zram0

# Verify the changes
zramctl

Legacy Method: Setting Up ZRAM on Older OS Versions (Bullseye and earlier)

If you are running an older Raspberry Pi OS version that still relies on SD card swap (dphys-swapfile), perform the following steps to migrate:

Step 1: Disable the Default SD Card Swap

1
2
3
4
# Disable and remove the default swapfile
sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo systemctl disable dphys-swapfile

Step 2: Install and Configure zram-tools

1
2
3
4
5
6
# Install package
sudo apt update
sudo apt install zram-tools -y

# Configure options
sudo nano /etc/default/zramswap
Update /etc/default/zramswap with:
1
2
3
4
# Set compression algorithm and RAM allocation percentage
ALGO=lz4
PERCENT=50
PRIORITY=100
Start the service:
sudo systemctl restart zramswap

Tuning ZRAM Performance

Compression Algorithms

Algorithm Speed Compression Ratio CPU Usage Recommendation
lz4 ★★★★★ ★★★☆☆ (~2.0x) Very Low Best for Pi Zero 2 W / Pi 3 (limited CPU)
lzo ★★★★☆ ★★★☆☆ (~2.1x) Low Good all-around
zstd ★★★☆☆ ★★★★★ (~3.5x) Moderate Best for Pi 4/5 (high CPU headroom)
lzo-rle ★★★★☆ ★★★☆☆ (~2.1x) Low Default on many Linux distros

To check available compression algorithms supported by your kernel:

cat /sys/block/zram0/comp_algorithm
# [zstd] indicates the currently active algorithm
Model RAM ZRAM Size Effective Swap Config
Pi Zero 2 W 512MB 256MB ~512-750MB zram-size = ram / 2
Pi 3 1GB 512MB ~1-1.5GB zram-size = ram / 2
Pi 4 (2GB) 2GB 1GB ~2-3GB zram-size = ram / 2
Pi 4 (4GB) 4GB 1GB ~2-3GB zram-size = 1024
Pi 4/5 (8GB) 8GB 2GB ~4-6GB zram-size = 2048

Avoid over-allocating ZRAM size

Setting ZRAM larger than your physical memory size wastes memory space. The goal of ZRAM is to handle temporary spikes and prevent out-of-memory (OOM) crashes, not to act as a replacement for physical RAM.


Kernel Swappiness Tuning

The swappiness parameter controls how aggressively the kernel moves memory pages to swap. For ZRAM, you should swap moderately to prevent excessive CPU compression overhead:

1
2
3
4
5
6
7
8
# Check current swappiness (default: 60)
cat /proc/sys/vm/swappiness

# Temporary set to 20 (recommended for ZRAM)
sudo sysctl vm.swappiness=20

# Make persistent across reboots
echo "vm.swappiness=20" | sudo tee -a /etc/sysctl.d/99-zram.conf

Monitoring ZRAM in Real-Time

You can watch ZRAM compression metrics using the following script. Save it as ~/zram-status.sh and make it executable:

#!/bin/bash
# Monitor ZRAM Swap stats
echo "=== ZRAM Swap Status ==="
zramctl

if [ -f /sys/block/zram0/mm_stat ]; then
    ORIG=$(awk '{print $1}' /sys/block/zram0/mm_stat)
    COMP=$(awk '{print $2}' /sys/block/zram0/mm_stat)
    if [ "$COMP" -gt 0 ]; then
        RATIO=$(echo "scale=2; $ORIG / $COMP" | bc)
        echo "Compression ratio: ${RATIO}:1"
    fi
fi

Make executable and run:

chmod +x ~/zram-status.sh
./~/zram-status.sh