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¶
Find installed packages and active units:
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:
- Continuous
siandsoinvmstatindicate swap traffic. - PSI
somemeans at least one task is stalled on memory. - PSI
fullmeans 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:
Edit its configuration:
Start conservatively:
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:
If the service fails, inspect:
Method 2: systemd-zram-generator¶
Use this method when you deliberately want generator-managed devices:
Example bounded configuration:
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:
Reboot to apply it cleanly:
Then verify:
Do not run both managers¶
Check again after configuration:
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¶
Important fields:
DISKSIZE: logical maximum capacityDATA: uncompressed size of stored pagesCOMPR: compressed payload sizeTOTAL: 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:
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:
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:
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:
For the generator, first confirm no critical pages depend on its swap, then:
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¶
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:
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.