zram is a Linux kernel module that creates block devices inside RAM and compresses everything written to them. Used as swap, it keeps the pages the system pushes out of memory compressed in RAM instead of sending them to a much slower disk. On machines with little memory or slow drives it is one of the cheapest improvements available, and several distributions already enable it.
zram in 30 seconds
- zram compresses in RAM the pages the kernel would otherwise swap to disk.
- It costs some CPU but avoids most slow disk I/O during memory pressure.
- Fedora uses it by default; on Debian and Ubuntu, zram-tools or systemd-zram-generator set it up in minutes.
- It does not replace RAM, does little for already compressed data and cannot be used for hibernation.
When RAM fills up, the kernel moves pages it does not expect to need soon into swap. If swap lives on disk, every time those pages are needed again they come back from a device that is orders of magnitude slower than memory, and when that happens constantly the machine thrashes: the CPU waits on the disk and everything stutters. zram changes the destination: the page is compressed and kept in RAM, so getting it back costs a decompression measured in microseconds rather than a disk read.
Check where you stand
free -h # RAM and swap in use; watch the available column
swapon --show # which swap exists, its type and priority
zramctl # active zram devices, if any
vmstat 1 5 # si/so (swap) and wa (I/O wait) columnsCode language: PHP (php)
If si and so stay high and wa climbs, the machine is waiting on the disk to swap, exactly the case zram addresses. If a /dev/zram0 swap device already shows up, your distribution has done the work.
Setting it up
On Debian and Ubuntu there are two persistent options. The zram-tools package is configured in /etc/default/zramswap:
sudo apt install zram-tools
# In /etc/default/zramswap:
ALGO=zstd
PERCENT=50
sudo systemctl restart zramswapCode language: PHP (php)
The other is systemd-zram-generator, the approach Fedora uses, configured in /etc/systemd/zram-generator.conf:
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
sudo systemctl daemon-reload
sudo systemctl start [email protected]
To try it by hand without surviving a reboot, load the module and prepare the device. Pick the algorithm before setting the size, since it cannot be changed afterwards:
sudo modprobe zram
cat /sys/block/zram0/comp_algorithm # available algorithms
echo zstd | sudo tee /sys/block/zram0/comp_algorithm
echo 2G | sudo tee /sys/block/zram0/disksize
sudo mkswap /dev/zram0
sudo swapon --priority 100 /dev/zram0Code language: PHP (php)
The high priority makes the kernel prefer zram over any disk swap. To undo it: sudo swapoff /dev/zram0 and echo 1 | sudo tee /sys/block/zram0/reset.
Tuning and limits
Algorithm. lz4 and lzo-rle are very fast and light, good for old CPUs and laptops on battery; zstd compresses harder for a bit more CPU. Which ones you can use depends on the kernel, and comp_algorithm lists them.
Size. zram only consumes memory for the data it actually holds, so a large empty device costs next to nothing. Between half and all of RAM is a common starting point. zramctl shows the real ratio: DATA is the uncompressed size, COMPR the compressed size and TOTAL the memory used including overhead.
Swappiness. vm.swappiness (60 by default) controls how eagerly the kernel swaps. With zram, swap is cheap and many guides suggest higher values such as 100; recent kernels accept up to 200. Change it gradually and measure.
zram, zswap or disk swap. zswap is a related kernel feature that puts a compressed cache in front of a real disk swap device; it is the better choice if you need hibernation, because zram lives in RAM and vanishes at power-off. Do not run zswap and zram together; check with cat /sys/module/zswap/parameters/enabled.
zram is no miracle: it adds no memory, costs CPU and gains little on data that is already compressed, such as video, images or archives. If the workload needs more memory than fits even after compression, the system will still struggle and the kernel will eventually turn to the OOM killer. Measure before and after with free and vmstat under a real workload. For more on the compressors themselves, see our LZ4 1.11.0 comparison with zstd and xz.
FAQ
What is zram?
A Linux kernel module that creates compressed block devices in RAM. Used as swap, it avoids most trips to a slow disk.
Which compression algorithm should I pick?
lz4 or lzo-rle for slow CPUs or battery-powered machines; zstd for better compression when the CPU can afford it.
Can I hibernate with zram?
No, because zram lives in RAM. Hibernation needs disk swap, which can coexist with zram at a lower priority, or you can use zswap instead.
Sources:
