What you will achieve
zram compressed RAM swap on low-memory Raspberry Pi or VPS instances — reduces OOM kills without hammering SD card I/O like a huge swapfile.
1) Ubuntu zram-generator
sudo apt install zram-generator
# /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
sudo systemctl daemon-reload
sudo systemctl start /dev/zram0
2) Manual zram (generic)
echo zstd > /sys/block/zram0/comp_algorithm
echo 2G > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon -p 100 /dev/zram0
3) Trade-offs
zram uses CPU for compression — fine on 2+ cores with 1 GB RAM; less ideal under sustained memory pressure on heavy databases.
Verify
zramctl
swapon --show
free -h
5) Check if zram already active
cat /proc/swaps
zramctl
Ubuntu 24.04 may enable zram by default — avoid double swap layers.
6) Size tuning
zram-size = ram / 2 is common starting point. Monitor compression ratio in /sys/block/zram0/mm_stat.
7) Disable disk swap when using zram
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
Not for every workload
Database servers with strict latency SLAs should size RAM properly rather than rely on compressed swap — zram helps constrained edge nodes and Pis more than 64 GB DB hosts.
8) zram generator on Fedora
Fedora enables zram swap by default on many spins — verify before adding duplicate swapfile.
Prerequisites
Low RAM device (≤4 GB typical). Kernel zram module. Check existing swap — avoid duplicate layers. CPU headroom for compression — very CPU-bound workloads may suffer.
Verify swappiness with zram
cat /proc/sys/vm/swappiness
Default 60 may push to zram early — lowering to 100 on zram-only setups is sometimes recommended in Pi docs; test with your workload.
zram on Raspberry Pi OS
Pi OS enables zram via init — check dmesg | grep zram before manual zram-generator config duplicates effort.
zram generator size formula
min(ram/2, 4096) MB cap on small boards prevents zram consuming disproportionate CPU compressing gigabytes rarely swapped anyway — tune per workload measurements not forum defaults.
Memory cgroup interaction
systemd MemoryMax= on service limits RSS — zram swap still counts toward pressure — tune both not one in isolation on systemd 252+.
zram stats sysfs
cat /sys/block/zram0/comp_algorithm
cat /sys/block/zram0/mm_statCompare orig_data_size vs compr_data_size for compression ratio — tune algorithm zstd vs lzo based on CPU budget.
disable zram test
Benchmark workload with zram off vs on same hardware — micro benchmarks lie, real app memory footprint tells truth for your Pi or VPS tier.