Compress first, buy later: zram gets considerably more out of 32 GB of memory
Summary
Dutch software developer Therry van Neerven advises switching on zram before buying more RAM when memory runs short, and demonstrates it on a 32 GB machine that he fills with more than a hundred Firefox tabs. Afterwards zramctl reports 5.7 GB of data in 1.7 GB of memory, a ratio of 1:3.35 with zstd. This post adds how to set up zram on Debian, how to read the ratio correctly and which kernel settings belong with it.
What the author measured
zram creates a block device in memory that compresses everything written to it. Used as swap, the kernel moves rarely used memory pages there instead of writing them to the SSD. Windows, macOS and many Android phones have compressed their memory for a long time; on Linux, according to the author, Fedora and Pop!_OS enable zram by default, Ubuntu does not, and Amazon Linux only on small virtual machines.
For the test, van Neerven opens 104 websites in Firefox in a loop, from news sites and YouTube to online shops. The running applications then request more memory than the machine has; part of it sits in zram swap. zramctl shows a device with zstd and 20.7 GB capacity holding 5.7 GB of data (DATA), compressed to 1.7 GB (COMPR), with 1.7 GB of memory used in total (TOTAL). He describes the cost as a bit more CPU load and some added latency for certain processes.
Setting up zram on Debian
Debian 13 offers two ways. The package systemd-zram-generator (1.2.1) already ships a default configuration in /usr/lib/systemd/zram-generator.conf which, after installation and a reboot or daemon-reload, creates a device zram0 as swap: half the size of RAM, at most 4 GiB, with the kernel's default algorithm and swap priority 100. If you want more, create /etc/systemd/zram-generator.conf with a [zram0] section, for example with zram-size = ram / 2 and compression-algorithm = zstd, and start systemd-zram-setup@zram0.service.
The older package zram-tools (0.3.7) sets up zram via /etc/default/zramswap; by default with lz4, 50 percent of RAM and priority 100. lz4 is faster, zstd packs tighter. Hibernation to zram swap is not possible; if you need it, keep an additional swap partition with lower priority.
According to the Arch wiki, two kernel settings belong with it: vm.swappiness well above the default of 60, because swapping into memory is cheap (Pop!_OS uses 180, the kernel documentation considers values above 100 sensible for in-memory swap), and vm.page-cluster = 0, so the kernel does not needlessly read back several pages at once. zswap should be off, otherwise it intercepts pages before they reach zram. On a Debian 13 machine with kernel 6.12, zswap was off by default when we checked.
Reading the ratio correctly
With zram, DISKSIZE is only the upper limit for uncompressed data, not reserved memory. The 20.7 GB in the test therefore do not occupy 20.7 GB of RAM, only what TOTAL shows. For the ratio, DATA divided by TOTAL is what counts, because unlike COMPR, TOTAL also includes metadata and allocator fragmentation. In the test both are level at 1.7 GB, so the author's 1:3.35 is honestly calculated.
The same values are in /sys/block/zram0/mm_stat: the first field (orig_data_size) is the uncompressed data, the third (mem_used_total) the memory actually used. From these two the ratio can also be recorded continuously in monitoring.
Ideas
- Before buying RAM, switch on zram as compressed swap in memory first.
- Windows, macOS and Android have compressed memory by default for years.
- Fedora and Pop!_OS enable zram by default, Ubuntu and Debian do not.
- Compression costs some CPU time but often gains three times the space.
- On idle cloud machines zram can make a larger instance unnecessary.
- zramctl shows raw data, compressed size and memory actually used side by side.
Insights
- Memory is expensive and soldered in many notebooks; compression is then the only lever that costs nothing but idle CPU time.
- zram only moves where rarely used memory pages live. The more a machine holds idle data such as browser tabs, the more it helps; an application that constantly touches all of its memory gains little.
- The packages' defaults are cautious: a 4 GiB limit is only a fraction on a 32 GB machine of what the author uses with 20.7 GB.
Quotes
- “5.7G of data in 1.7G of RAM. That’s a compression ratio of 1:3.35.” (Therry van Neerven)
Facts
- In the test: 32 GB of RAM, 104 websites in Firefox, zram with zstd, 5.7 GB of data in 1.7 GB of used memory.
- Without its own configuration, systemd-zram-generator creates zram0 with min(ram / 2, 4096) MiB and swap priority 100.
- zram-tools defaults to lz4, 50 percent of RAM and priority 100.
- Pop!_OS sets vm.swappiness = 180 and vm.page-cluster = 0 for zram.
- Hibernation to zram swap is not supported.
References
- Therry van Neerven: Do not buy RAM, compress!
- Arch Linux Wiki: Zram
- systemd/zram-generator: zram-generator.conf(5)
- Linux kernel documentation: zram, compressed RAM-based block devices
- Linux kernel documentation: /proc/sys/vm, swappiness
- Debian: package systemd-zram-generator (trixie)
- Hacker News: Do not buy RAM, compress
Critique
- The post does not back up the claim that the downsides are hardly noticeable: it only shows one memory snapshot with browser tabs, no measurement of CPU load, latency or behaviour under sustained memory pressure.
Remarks
- How the author sized the device at 20.7 GB is not stated in the post; for setup the author refers to the Arch wiki.
Recommendations
- On Debian, install systemd-zram-generator, set zram-size = ram / 2 and compression-algorithm = zstd under [zram0] in /etc/systemd/zram-generator.conf, then run systemctl daemon-reload and systemctl start systemd-zram-setup@zram0.service.
- Use zramctl and swapon --show to check that zram0 runs as swap with a higher priority than any existing swap partition.
- Put vm.swappiness = 180 and vm.page-cluster = 0 into /etc/sysctl.d/99-zram.conf and load them with sysctl --system.
- Watch the ratio from /sys/block/zram0/mm_stat (field 1 divided by field 3) before deciding on more RAM.
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.