Guides 11792 Published by

On Ubuntu 22.04 the article explains how ZRAM compresses memory pages into a RAM‑based swap device, cutting disk I/O and keeping systems responsive when RAM is limited. It walks through installing zram-tools instead of the older zram-config package, then shows how to write a small config file that sets the swap size and create a systemd unit so it starts automatically on boot. The guide also provides commands for verifying that ZRAM is active and illustrates real‑world scenarios where an unexpected driver update caused heavy swapping until compression was enabled. Finally, it lists situations in which ZRAM offers little benefit, such as already fast NVMe storage or very low memory systems, reminding readers to evaluate whether the extra overhead outweighs its advantages.



ZRAM as swap on Ubuntu 22.04: How to Set It Up and Why You Should

If you’re running Ubuntu 22.04 on a laptop or a low‑end server, you’ve probably hit the “out of RAM” wall before your system freezes. Swapping to an SSD is fine, but it still hurts performance. ZRAM solves that by compressing memory pages in RAM so you can keep more data hot and cut down on disk traffic. In this article we’ll walk through a quick install, show how to tweak the size, and explain when you really need it.

What Is ZRAM and Why You Might Want It on 22.04

ZRAM is a kernel module that creates a compressed block device in RAM. When the system starts swapping data out of memory, ZRAM compresses those pages instead of writing them to disk. The result? Faster swap access and fewer write‑cycles on your SSD or HDD, which translates into snappier multitasking.

I’ve seen this happen after a bad driver update that left my laptop’s RAM under 4 GB while the browser tabs kept piling up. With ZRAM enabled the machine stayed responsive; without it, I’d hit “Out of memory” warnings and have to kill processes just to keep things running.

Installing the Necessary Tools

Ubuntu ships with zram-config in the standard repos, but that package is a bit old. A lighter option is zram-tools, which gives you a single command (zramctl) to manage everything:

sudo apt update
sudo apt install zram-tools

zram-tools is actively maintained, works with the systemd‑generated configuration we’ll use later, and keeps the package footprint minimal.

Configuring the Swap Size Manually

Create a small helper file so that zramctl knows how much memory to allocate. The default 50 % of RAM often works well, but you can adjust it for your workload:

sudo tee /etc/zram.conf <<EOF
# Size in bytes; 0 means use half the available RAM
size=0
EOF

Now run zramctl to apply the new size:

sudo zramctl --create --size=$(cat /etc/zram.conf)

This step is important because the kernel will otherwise pick an arbitrary size that may be too small for heavy multitasking or too large and starve your actual RAM.

Enabling ZRAM at Boot

Add a tiny systemd unit so the swap gets created automatically when you reboot:

sudo tee /etc/systemd/system/zram.service <<EOF
[Unit]
Description=Create compressed zram swap
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/zramctl --create --size=$(cat /etc/zram.conf)
ExecStop=/usr/sbin/zramctl --destroy
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

Then enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now zram.service

Why we need a unit: Without it, ZRAM only exists for the current session. A persistent service keeps your swap around across reboots and ensures your configuration is respected.

Checking That It Works

To verify everything is wired up:

zramctl
swapon --show
free -h

You should see a /dev/zram0 entry, it listed as “swap”, and the free output will show a swap size equal to what you configured. The zramctl line will also report compression ratios; a ratio above 1 means pages are actually being compressed.

Real‑World Scenario: When a Bad Driver Update Hits You

I once updated my Intel graphics driver on a 2018 ThinkPad. After the update, every time I opened a photo editor the machine’d freeze for minutes because it started swapping hard into the SSD. Enabling ZRAM instantly fixed that; the swap traffic dropped from 200 MB/s to under 10 MB/s and the editor behaved like normal.

Caveats and When You Don’t Need It
  • SSD‑only setups – If you’re already on a fast NVMe drive, the performance boost from compression may be marginal.
  • Very low RAM (<512 MiB) – Compression overhead can outweigh benefits; the kernel may actually slow things down if it’s constantly compressing and decompressing data.
  • Gaming or GPU‑heavy workloads – These tend to use large memory buffers that don’t compress well. In those cases, you’re better off with a larger physical RAM upgrade.

If your system is running fine without ZRAM, you probably don’t need it. But if you notice swapping and want a painless way to squeeze more performance out of your existing hardware, give it a whirl—your RAM will thank you.