Skip to content

Raspberry Pi ZRAM Setup and Benchmark Guide

ZRAM provides a compressed block device in RAM that Linux can use as swap. It can reduce flash-backed swap I/O and prevent some out-of-memory failures, but it consumes CPU and physical RAM. Do not assume ZRAM is enabled by default or that one size fits every Raspberry Pi OS image: inspect the running system first.

Quick answer: ZRAM on Raspberry Pi OS and Armbian

ZRAM can improve responsiveness when memory pressure causes storage-backed swap, but it is not an overclocking switch. Check swapon --show, zramctl, PSI, CPU temperature, and OOM events first; then compare a baseline against one controlled configuration. The same measurement order applies to Raspberry Pi OS Trixie and Armbian, although package names and defaults may differ.

Quick setup decision

Current state Action
/dev/zram0 already appears in swapon --show Identify the owning package and benchmark before changing it
No ZRAM, simple Raspberry Pi OS setup Use zram-tools
No ZRAM, systemd generator configuration preferred Use systemd-zram-generator
Both implementations installed Stop one and remove the overlap before tuning
No measured memory pressure Keep the baseline; ZRAM may not help

Debian Trixie provides both zram-tools and systemd-zram-generator. Choose one implementation, not both.

Inspect the current swap configuration

1
2
3
4
free -h
swapon --show --output=NAME,TYPE,SIZE,USED,PRIO
zramctl
lsmod | grep zram

Find installed packages and active units:

1
2
3
dpkg-query -W -f='${Package}\t${Status}\t${Version}\n' \
  zram-tools systemd-zram-generator 2>/dev/null
systemctl list-units --all | grep -Ei 'zram|swap'

If /dev/zram0 already exists, do not install another manager over it. Save its current configuration and measure the workload first.

Confirm that memory is the bottleneck

Reproduce the slow task while observing:

1
2
3
vmstat 1
watch -n 1 'cat /proc/pressure/memory'
journalctl -k -b --no-pager | grep -Ei 'out of memory|oom-kill|killed process'
  • Continuous si and so in vmstat indicate swap traffic.
  • PSI some means at least one task is stalled on memory.
  • PSI full means all non-idle tasks are stalled together.
  • An OOM message identifies an allocation failure, but the root cause may be a leak or cgroup limit.

Use the memory pressure and OOM diagnosis before choosing a size.

Method 1: zram-tools

This is the simplest Debian package for a single compressed swap device:

sudo apt update
sudo apt install zram-tools

Edit its configuration:

sudo nano /etc/default/zramswap

Start conservatively:

1
2
3
ALGO=lz4
PERCENT=50
PRIORITY=100

PERCENT=50 sets the maximum ZRAM device capacity to half of physical RAM; it does not reserve half of RAM immediately. lz4 favors speed. Test zstd only as a separate experiment when compression ratio matters more than CPU cost and the installed kernel supports it.

Apply and verify:

1
2
3
4
sudo systemctl restart zramswap
systemctl status zramswap --no-pager
swapon --show --output=NAME,TYPE,SIZE,USED,PRIO
zramctl

If the service fails, inspect:

journalctl -b -u zramswap --no-pager

Method 2: systemd-zram-generator

Use this method when you deliberately want generator-managed devices:

1
2
3
sudo apt update
sudo apt install systemd-zram-generator
sudo nano /etc/systemd/zram-generator.conf

Example bounded configuration:

1
2
3
4
[zram0]
zram-size = min(ram / 2, 2048)
compression-algorithm = lz4
swap-priority = 100

The size expression is in MiB where a bare number is used. This example caps the device at 2 GiB. Confirm syntax against the installed manual because generator versions can differ:

man zram-generator.conf

Reboot to apply it cleanly:

sudo reboot

Then verify:

1
2
3
systemctl status systemd-zram-setup@zram0.service --no-pager
swapon --show --output=NAME,TYPE,SIZE,USED,PRIO
zramctl

Do not run both managers

Check again after configuration:

dpkg -l zram-tools systemd-zram-generator 2>/dev/null
systemctl list-units --all | grep -Ei 'zram|swap'

Two tools can compete for /dev/zram0, create unexpected devices, or apply different priorities. Select one, record its configuration, and remove the unused package only after confirming which service owns the active device.

Choose a starting size

Use a percentage as a test starting point, not a universal recommendation:

Workload Starting experiment What to watch
512 MiB interactive system 50% RAM CPU cost, PSI, browser/application latency
1–2 GiB service host 25–50% RAM OOM events, request p95, compression ratio
4–8 GiB system Enable only after pressure is observed Whether swap is used at all
Incompressible media or encrypted buffers Small test or no ZRAM Poor DATA to TOTAL efficiency

A ZRAM device can advertise more logical capacity than the physical memory it eventually consumes. If the workload fills it with poorly compressible pages, the system can still run out of RAM.

Read ZRAM statistics

zramctl --output NAME,ALGORITHM,DISKSIZE,DATA,COMPR,TOTAL,STREAMS,MOUNTPOINT
cat /sys/block/zram0/mm_stat

Important fields:

  • DISKSIZE: logical maximum capacity
  • DATA: uncompressed size of stored pages
  • COMPR: compressed payload size
  • TOTAL: physical memory consumed, including allocator overhead

Use TOTAL, not only COMPR, when judging memory cost. Compare the figures at the same point in the workload.

Swap priority and disk fallback

Display priorities:

swapon --show --output=NAME,TYPE,SIZE,USED,PRIO

Linux normally selects higher-priority swap first. If a disk swap remains as an emergency fallback, ZRAM should usually have the higher priority. A disk fallback can prevent an immediate OOM but may create severe latency and additional writes; test it under the real service-level objective.

Do not disable an existing swap file until you have console access and a controlled memory test. On a remote-only device, an OOM during reconfiguration can end the session.

Test compression algorithms

Show algorithms supported by the running kernel:

cat /sys/block/zram0/comp_algorithm

The active algorithm appears in brackets. Common trade-offs are:

Algorithm General tendency Test when
lz4 Lower CPU cost, fast compression/decompression Interactive latency matters
zstd Often higher compression, more CPU work Working set compresses well and CPU headroom exists

Do not switch an active device's algorithm while it holds swap. Change the manager configuration and reboot or recreate the device through that manager's documented procedure.

Treat vm.swappiness as a separate experiment

Inspect the current value:

sysctl vm.swappiness

Do not automatically set it to 10, 20, or 100. Its effect depends on workload and kernel behaviour. Keep the current value for the first ZRAM comparison. If testing it later, change only that variable, record it, and compare application latency, PSI, swap traffic, and OOM events.

Repeatable benchmark

Run at least five repetitions of the same task in each configuration:

Metric Baseline ZRAM
Workload completion time, seconds
p95 application latency, ms
Peak MemAvailable, MiB
Peak PSI some / full, %
Swap-in / swap-out, KiB/s
ZRAM DATA / COMPR / TOTAL, MiB n/a
OOM kills
Maximum temperature, °C

Keep board, OS, workload, cooling, storage, services, and starting temperature constant. Publish raw data rather than a single best run. See the benchmark methodology.

Remove ZRAM safely

Do this only with sufficient free memory and recovery access. Stop the manager you selected, remove its configuration, and uninstall that package. For zram-tools:

1
2
3
4
free -h
sudo systemctl stop zramswap
swapon --show
sudo apt remove zram-tools

For the generator, first confirm no critical pages depend on its swap, then:

1
2
3
4
free -h
sudo swapoff /dev/zram0
sudo apt remove systemd-zram-generator
sudo reboot

swapoff can fail or trigger OOM when RAM is insufficient. Stop memory-heavy applications first and do not force it on a remote production system.

Troubleshooting

/dev/zram0 does not appear

1
2
3
systemctl status zramswap systemd-zram-setup@zram0.service --no-pager
journalctl -b | grep -i zram
modinfo zram

Confirm that only one manager is installed and its configuration syntax matches the installed version.

Device or resource busy

Another service may already own the device. Inspect units, mounts, swap, and packages instead of resetting it manually:

1
2
3
zramctl
swapon --show
systemctl list-units --all | grep -i zram

Performance became worse

Check CPU saturation, temperature, throttling, compression efficiency, and PSI. Reduce the device size or return to the baseline if compression overhead exceeds the avoided storage latency.

OOM still occurs

ZRAM cannot fix an unbounded leak or a workload whose active set exceeds available memory. Inspect the killed process, systemd cgroup limits, and application concurrency with the OOM guide.

FAQ

Is ZRAM enabled by default on Raspberry Pi OS Trixie?

Do not assume it. Raspberry Pi OS images and upgraded systems can differ. swapon --show, zramctl, package state, and systemd units give the authoritative answer for the running device.

Does ZRAM wear out RAM?

No. RAM does not have flash write endurance. ZRAM does consume memory bandwidth and CPU time.

Does ZRAM replace physical RAM?

No. It stores compressible inactive pages more densely. Active and incompressible working sets still require physical memory.

Which algorithm is best?

Start with lz4 for a latency-oriented baseline, then compare another supported algorithm using the same workload. The best choice depends on compressibility and CPU headroom.