DEV Community

Cover image for How to Enable zram on Linux (and Why Keeping Your Swap File Makes It Worse)
Alan West
Alan West

Posted on Originally published at blog.authon.dev

How to Enable zram on Linux (and Why Keeping Your Swap File Makes It Worse)

A Kingston NV3 1TB cost €50 in September 2025. In September 2026 it was €144. That's from ComputerBase's price check as summarised by DriveVault, which also cites TrendForce saying NAND only moves "back into balance in the second half of 2027", with DRAM staying tight through 2027. So before you order another 16GB stick for a Linux box that keeps swapping, compress the RAM you already own. zram on a normal desktop routinely holds about 3x more than it costs in memory.

I'll go through enabling zram on Ubuntu, Fedora, Arch and Raspberry Pi OS, reading the real compression ratio with zramctl, and why keeping your old swap file next to zram, which most guides leave you with, makes things worse.

How much RAM zram actually saves

zram is a compressed block device in RAM that you use as swap.

The kernel docs assume a 2:1 compression ratio when sizing it. Real desktops often do better:

Not everything compresses. In the Hacker News thread, throw-qqqqq pointed out that a Q4_K_M quantization "is often around +7.90 bits/byte and so is virtually incompressible". So if your RAM is full of local LLM weights, zram won't save you, but browser tabs, Electron apps and a couple of Docker containers compress fine.

zram vs zswap: pick one

A lot of the arguing in the HN comments is about this, and it decides which commands you run, so sort it out first.

zswap is a compressed cache in front of a disk swap device. Pages get compressed in RAM first, and when the pool fills, the coldest ones get evicted to disk. zram is a swap device of its own, with no disk behind it unless you configure writeback.

Chris Down, who has worked on kernel memory management and swap code for the better part of a decade, published Debunking zswap and zram myths in March. His tl;dr: "If in doubt, prefer to use zswap." His strongest point is LRU inversion. Put zram at high priority next to a disk swap file and the kernel fills zram first, then sends everything after that to disk. zram ends up holding the oldest, coldest pages, and the stuff you're using right now goes to the SSD. In his words: "warm pages drift to disk, cold pages calcify in zram".

He's right, and that's exactly the setup most tutorials leave you with: install zram-tools, keep the old swapfile.

My take: on a laptop or desktop where you don't hibernate, zram with no disk swap is the simpler and better setup. Fedora has done exactly this since Fedora 33, and there's nothing to invert against. If you want disk swap, for hibernation or because you run huge workloads, use zswap and skip zram. Running both just piles up two caches that work against each other.

Setup Needs disk swap Good for Watch out for
zram only No Desktops, laptops, Raspberry Pi, SD cards No hibernation, needs an OOM daemon
zswap + swap file Yes Servers, hibernation, big memory spikes Disk writes when the pool fills
zram + swap file Yes Almost nothing LRU inversion
Swap file only Yes Hibernation, nothing else Slow, writes to flash

Chris Down also warns that if you run zram you should pair it with systemd-oomd or earlyoom, or "the kernel's OOM killer can leave the system hung for minutes before acting". He cites Matt Fleming at Cloudflare reporting "20 to 30 minute brownouts" on production machines with the OOM killer never firing, and traces it to how zram reports free swap.

First, check what you already have

swapon --show
zramctl
cat /sys/module/zswap/parameters/enabled
Enter fullscreen mode Exit fullscreen mode

If swapon shows a /dev/zram0 plus a /swapfile or /swap.img, you have the bad combination. If zswap prints Y and you're about to set up zram, turn zswap off. The Arch wiki is blunt about it: "If the related zswap kernel feature remains enabled, it will prevent zram from being used effectively." Add zswap.enabled=0 to your kernel command line.

How to enable zram on Ubuntu

Ubuntu doesn't turn it on by default. zram-tools and systemd-zram-generator are both in universe for 24.04 and 26.04. The zram-tools route, following CNX Software's steps:

sudo apt install zram-tools
Enter fullscreen mode Exit fullscreen mode

Edit /etc/default/zramswap:

ALGO=zstd
PERCENT=75
PRIORITY=100
Enter fullscreen mode Exit fullscreen mode

Then:

sudo systemctl restart zramswap.service
Enter fullscreen mode Exit fullscreen mode

If you had the old zram-config package from years ago, purge it first (sudo apt purge --autoremove zram-config) so the two don't fight. Then remove the disk swap file from /etc/fstab and run sudo swapoff on it. Removing the swap file is the step that fixes the LRU problem.

Fedora: it's already on, but capped at 8 GiB

Fedora 33 shipped zram at 50% of RAM capped to 4 GiB. Fedora 34 raised that, and the zram-generator-defaults config now says zram-size = min(ram, 8192): the size of your RAM, but never more than 8 GiB. On a 32GB machine that's still pretty timid. Override it:

# /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
Enter fullscreen mode Exit fullscreen mode
sudo systemctl daemon-reload
sudo systemctl restart [email protected]
Enter fullscreen mode Exit fullscreen mode

Don't leave that file empty. The Fedora change page says sudo touch /etc/systemd/zram-generator.conf is how you disable zram, because the file in /etc wins over the one in /usr/lib and an empty one has no [zram0] section.

Arch Linux

sudo pacman -S zram-generator
Enter fullscreen mode Exit fullscreen mode

Same config file as Fedora. The zram-generator README notes the defaults only kick in if "the configuration file with at least one section" exists, so [zram0] alone gives you min(ram / 2, 4096) MB. Then daemon-reload and start [email protected].

Raspberry Pi OS

Current Raspberry Pi OS already has zram on. rpi-swap replaced dphys-swapfile, and its auto mechanism currently means zram+file. That's zram with a file on the SD card that only gets idle pages written back on a timer, which is the sane version of zram plus disk. It also sets vm.page-cluster=0 for you.

The zram default is RamMultiplier=1 with MaxSizeMiB=2048. To change it, add a drop-in under /etc/rpi/swap.conf.d/ with a [Zram] section and reboot (daemon-reload doesn't restart the swap units). The man page warns that multiplier values above 2.0 aren't recommended.

zram sysctl tuning: swappiness and page-cluster

The kernel default vm.swappiness is 60, on a scale of 0 to 200, and the docs say that for in-memory swap like zram "values beyond 100 can be considered". 60 assumes swap is expensive disk I/O. zram isn't. The Arch wiki block, which it says matches what Pop!_OS ships:

# /etc/sysctl.d/99-vm-zram-parameters.conf
vm.swappiness = 180
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0
Enter fullscreen mode Exit fullscreen mode
sudo sysctl --system
Enter fullscreen mode Exit fullscreen mode

Don't skip page-cluster = 0. The default of 3 reads 8 pages per swap-in, which Chris Down describes as "polluting the swap cache with 7 pages you don't need every time you want 1" when there's no disk seek to amortise.

On priority: zram-generator uses 100 if you don't set one, which outranks a swap file with no explicit priority (CNX's old one sat at -2). That's the LRU inversion setup again, so the real fix is to remove the swap file. zswap avoids the problem entirely because it isn't a separate swap device.

How to check your real compression ratio with zramctl

zramctl
Enter fullscreen mode Exit fullscreen mode

DATA is what got swapped in, uncompressed. COMPR is the compressed size. TOTAL is what it really costs in RAM, including allocator overhead. Your honest ratio is DATA / TOTAL, not DATA / COMPR. To get it straight from the kernel's mm_stat fields (orig_data_size and mem_used_total):

awk '{printf "%.2fx\n", $1/$3}' /sys/block/zram0/mm_stat
Enter fullscreen mode Exit fullscreen mode

Check after a full workday, not five minutes after boot. CNX's 2GB Pi 5 showed 181.9M of data in 22.7M compressed. That's 8x, and a sample that small doesn't tell you much about a real workload.

The checklist

  1. Run swapon --show. If zram and a swap file are both there, drop one.
  2. Desktop, no hibernation: zram only, zswap off, an OOM daemon on.
  3. Need disk swap: zswap on, no zram.
  4. Set swappiness to 180 and page-cluster to 0 if you're on zram.
  5. Use zstd, and size zram at 50 to 100% of RAM.

I'm with Fedora on this one: zram-only on desktops, zswap everywhere else. Chris Down would tell me to use zswap on my laptop too, and the HN thread splits roughly the same way. What compression ratio does zramctl show on your machine? Post DATA, TOTAL, your RAM size and what you were running, especially if you've measured zswap against zram on the same box.

Sources

Top comments (0)