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:
You should see output similar to this:
/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:
Step 2: Configure ZRAM parameters¶
Add the following configuration (adjust sizes and algorithms as desired):
Step 3: Apply the changes¶
Reload the daemon and restart the ZRAM swap setup service:
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¶
Step 2: Install and Configure zram-tools¶
/etc/default/zramswap with:
Start the service:
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:
Recommended ZRAM Sizes¶
| 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:
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:
Make executable and run:
Related Guides¶
- System Settings Configuration — General OS tuning
- Boot Time Optimization — Speed up boot processes
- CPU Frequency Scaling — Core performance scaling