zram and Swap Tuning: Compressed Memory Instead of Disk
Swapping to disk is slow enough that a machine under memory pressure becomes unusable rather than merely slower. zram changes the arithmetic: swap lives in RAM, compressed, so retrieving a page costs a decompression rather than a disk read.
How it works
zram creates a compressed block device in memory and uses it as swap. When the kernel decides to swap a page, it compresses it and stores it in that device rather than writing to disk.
Typical compression is two to three times for ordinary application memory. So a zram device given 4GB of RAM holds roughly 8 to 12GB of swapped pages while consuming at most 4GB.
The result is that a machine with 8GB of RAM behaves more like one with 12 to 16, in exchange for CPU spent compressing. On any modern processor that trade is heavily favourable, because CPU is abundant and memory bandwidth is enormous compared with disk.
Checking what you have
Most current distributions enable it by default.
swapon --show
zramctl
cat /proc/swaps
zramctl shows the device, its algorithm, its configured size, the data stored, and the compressed size, which lets you see the compression ratio you are actually getting.
Setting it up
systemd-zram-generator, the modern approach used by Fedora and others:
sudo nano /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100
sudo systemctl daemon-reload
sudo systemctl start systemd-zram-setup@zram0
zram-tools on Debian and Ubuntu:
sudo apt install zram-tools
sudo nano /etc/default/zramswap
ALGO=zstd
PERCENT=50
PRIORITY=100
The priority matters when you also have disk swap. A higher number is used first, so zram at 100 and disk swap at -2 means the kernel fills zram before touching the disk, which is what you want.
Swappiness with zram
The conventional advice to lower vm.swappiness assumes swap is slow. With zram it is not.
sudo tee /etc/sysctl.d/99-zram.conf <<'CONF'
vm.swappiness = 150
vm.page-cluster = 0
CONF
sudo sysctl --system
vm.swappiness above 100 is permitted on modern kernels and is appropriate here: swapping is cheap, so let the kernel do it readily.
vm.page-cluster = 0 disables read-ahead on swap-in. Read-ahead exists to amortise disk seeks, and there are no seeks in RAM, so reading extra pages is pure waste.
Our sysctl guide covers making these persist properly.
Sizing
Between half and all of your physical RAM is the normal range.
The device does not reserve memory. It consumes only what is actually stored in it, so an oversized zram device on a machine that never swaps costs nothing.
For a machine with 8GB, ram / 2 gives a 4GB device holding perhaps 8 to 12GB of pages. For a 4GB machine, sizing at full RAM is reasonable and makes a very noticeable difference.
Hibernation
This is the one thing zram cannot do. Hibernation writes memory contents to disk and powers the system off, and swap in RAM disappears when RAM does.
If you hibernate, keep a disk swap area at least the size of your RAM, at a lower priority so zram is used first for ordinary swapping:
sudo swapon --priority -2 /swapfile
Related mechanisms
zswap is a different thing that people confuse with zram. It is a compressed cache in front of real disk swap: pages are compressed in memory first and written to disk only when that cache fills. It needs disk swap to exist; zram replaces it. Use one or the other.
zcache is obsolete and no longer relevant.
Does it help you
Yes, clearly: machines with 8GB or less, older laptops, single board computers, anything running many browser tabs.
Marginally: machines with 32GB or more that never approach full, where it mostly changes behaviour in an edge case you rarely reach.
Not at all: workloads dominated by large sequential I/O rather than memory pressure.
Since most distributions now enable it by default, the practical question for many people is not whether to add it but whether to tune the size and swappiness, which is where the remaining gains are.
Our swap space explainer covers the fundamentals, and monitoring memory usage covers reading what is actually happening.
Frequently Asked Questions
What is zram?
A compressed block device in RAM used as swap. Pages swapped out are compressed and kept in memory rather than written to disk, so you fit more into the same physical RAM at the cost of some CPU time for compression.
Does zram replace disk swap entirely?
For most desktops and laptops, yes. The exception is hibernation, which needs a real disk swap area large enough to hold memory contents, because RAM is powered off. Many systems run zram with a small disk swap kept for that purpose.
How much zram should I configure?
Between half and all of your physical RAM is the usual range. Because it compresses, typically two to three times, a zram device sized at half your RAM effectively gives you more than that in swap while consuming much less.
Which compression algorithm is best?
zstd is the sensible default, giving a good ratio at high speed. lzo-rle is faster with a worse ratio and suits very old CPUs. lz4 sits between them. On modern hardware zstd is almost always right.
Does zram help on a machine with plenty of RAM?
Less, but it is not harmful. It mostly changes behaviour under pressure, so a machine that never approaches full memory sees little difference. It costs nothing when unused because the device only consumes memory for pages actually stored in it.
Should I still set vm.swappiness with zram?
Yes, and higher than you would with disk swap. Because zram is fast, swapping is cheap, so values around 100 or above are common and appropriate. The old advice to lower swappiness assumes swap is slow, which zram is not.