You know that feeling when your laptop has been fine all morning, then you open one more thing — a Slack call, a build, another browser tab — and the whole system slows to a crawl? That’s swap thrashing: the kernel shoving memory pages to disk because RAM ran out. zram makes that hurt less. It’s compressed swap that lives in RAM, not on disk — dramatically faster than disk swap, zero SSD wear, and a few gigabytes of extra headroom before the OOM killer fires.
Setting it up takes about five minutes with systemd-zram-generator, the upstream-supported tool for zram on any modern systemd-based distro.
This article shows the clean setup.
Why zram?
- Compressed pages stay in RAM instead of going to disk. lz4 typically gets ~3:1 compression on dirty memory.
- Orders of magnitude faster than NVMe swap.
- Zero SSD wear — nothing actually hits the disk.
- More breathing room before the OOM killer fires.
Why not just write a script?
You can. The trade-off is that you take on synchronization between the kernel’s zram module, udev, and your code. The generator handles that, which is why it’s the recommended approach on systemd-based distros.
The most common pitfall is a timing race between layers: the kernel emits a “device added” event but udev hasn’t processed it yet, and the script’s next sysfs write fails with Device or resource busy. It’s solvable in shell with careful udevadm settle calls, but it’s the kind of bug that often surfaces only at boot — which is what pushed me to switch from my own setup script to the generator.
Prerequisites
- Ubuntu 22.04 / 24.04 (or any modern systemd-based distro)
sudoaccess
Step 1: Install the package
sudo apt update && sudo apt install systemd-zram-generator
This installs the generator binary plus a helper service template (systemd-zram-setup@.service). Nothing activates yet — you need a config first.
Step 2: Write the config
sudo tee /etc/systemd/zram-generator.conf > /dev/null << 'EOF'
[zram0]
zram-size = 6144
compression-algorithm = lz4
swap-priority = 5
EOF
What each option means:
zram-size = 6144— size in MB. As a rule of thumb, pick a fraction of your total RAM. Examples below use a 16 GB laptop:- ~1/8 of RAM → conservative (e.g.
2048on 16 GB) - ~1/4 of RAM → balanced (e.g.
4096on 16 GB) - ~3/8 of RAM → aggressive, good for browsers + IDEs + VMs (e.g.
6144on 16 GB — the value used above) - ~1/2 of RAM → max recommended (e.g.
8192on 16 GB)
Don’t exceed 50% of total RAM. zram still consumes physical memory to store its compressed pages, so an oversized zram device defeats the purpose.
- ~1/8 of RAM → conservative (e.g.
compression-algorithm = lz4— fast, RAM-efficient.zstdcompresses harder but costs more CPU;lz4is the better default for desktops.swap-priority = 5— higher than your disk swap (default-2), so the kernel reaches for zram first.
You can also size dynamically:
zram-size = ram / 2
Step 3: Activate without rebooting
sudo systemctl daemon-reload && sudo systemctl start /dev/zram0
daemon-reload re-runs systemd’s generators, which produces the auto-generated unit dev-zram0.swap under /run/systemd/generator/. Starting /dev/zram0 activates it.
Step 4: Verify
zramctl && swapon --show
Expected output:
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 6G 4K 69B 20K 12 [SWAP]
NAME TYPE SIZE USED PRIO
/dev/nvme0n1p2 partition 2G 0B -2
/dev/zram0 partition 6G 0B 5
(The /dev/nvme0n1p2 line is an existing disk swap — yours will appear here too if you have one. If not, you’ll just see the zram entry.)
Look for [SWAP] in the MOUNTPOINT column and priority 5 in swapon --show. That means the kernel will prefer zram over disk swap.
Step 5: Reboot test
Reboot to confirm zram comes up automatically — any boot-time issues would surface here, not during runtime testing.
sudo reboot
After the machine comes back:
zramctl
swapon --show
systemctl status dev-zram0.swap --no-pager | head -10
You should see zram active at the configured size and priority, with the unit reporting Active: active since ....
Useful operations
Resize:
sudo $EDITOR /etc/systemd/zram-generator.conf
sudo systemctl daemon-reload && sudo systemctl restart dev-zram0.swap
Inspect the auto-generated unit:
systemctl cat dev-zram0.swap
Note the path is under /run/systemd/generator/. Don’t edit it directly — it’s regenerated on every boot.
Disable zram:
sudo systemctl stop dev-zram0.swap
sudo rm /etc/systemd/zram-generator.conf
sudo systemctl daemon-reload
Notes
- The generator only runs at boot or on
daemon-reload. If you change the config, always reload before restarting the swap unit. swappinessstill controls how eagerly the kernel uses swap. On laptops with zram, raising it (e.g.vm.swappiness=80) often helps because compressed-RAM swap is cheap to reach for.- If you previously had a hand-rolled zram setup, disable the old service and remove the unit file before installing this — they don’t conflict at boot, but cleanup avoids confusion.
That’s it. Four lines of config and zram comes up automatically on every boot.
Related: if you’re tuning a Linux laptop for stability, you might also like my walkthrough on fixing random shutdowns on the Acer Aspire A515-57 by disabling Turbo Boost — same kind of kernel-level laptop fix, different layer of the stack.