zram creates a swap device that lives in RAM, compressed. When memory gets tight, the kernel swaps pages into zram instead of to disk. They’re compressed (often 2–4× for typical server data), so 1 GB of RAM can hold 2–3 GB of swapped pages, at memory speed rather than disk speed. On small VPS plans it’s an easy way to survive memory spikes with less slowdown than disk swap.

How it compares

Disk swap zram
Speed Slow (disk I/O) Fast (RAM + compression CPU)
Capacity Limited by disk Limited by RAM × compression ratio
CPU cost None Some, for compression
Wear and I/O Disk writes None
Survives reboot Irrelevant (swap is temporary anyway) Same

zram doesn’t create memory from nothing. Incompressible data (already compressed images, encrypted data) gains little. It shines with text-like memory: interpreters, databases, caches and idle processes.

Check the kernel support

modinfo zram | head -3
lsmod | grep zram

Every mainstream distribution kernel includes the module. On a KVM VPS you can load it freely.

Setup on Ubuntu / Debian: zram-tools

sudo apt install zram-tools

Configure /etc/default/zramswap:

ALGO=zstd
PERCENT=50
PRIORITY=100
  • PERCENT — the size of the zram device as a share of RAM (uncompressed capacity). 25–50% is typical.
  • ALGO — zstd gives a good ratio, lz4 is faster with less compression.
  • PRIORITY — higher than disk swap, so zram is used first.
sudo systemctl restart zramswap
swapon --show

Setup with zram-generator (Fedora, RHEL family, also Debian/Ubuntu)

Fedora enables zram by default. Elsewhere:

sudo dnf install zram-generator          # or: sudo apt install systemd-zram-generator
# /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100
sudo systemctl daemon-reload
sudo systemctl start [email protected]
swapon --show

Manual setup (to understand or test)

sudo modprobe zram
sudo zramctl --find --size 1G --algorithm zstd     # prints e.g. /dev/zram0
sudo mkswap /dev/zram0
sudo swapon -p 100 /dev/zram0

This isn’t persistent. Use one of the packages above for real setups.

Check that it works

zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd          1.9G  612M  168M  176M       2 [SWAP]
  • DATA — uncompressed data stored.
  • TOTAL — RAM actually used, including overhead.

Here 612 MB of swapped memory takes only 176 MB of RAM, a ratio of about 3.5×.

swapon --show        # zram with a higher priority than /swapfile
free -h

zram plus disk swap

A good layout for small servers:

  1. zram with a high priority (100) gets used first, and fast.
  2. A disk swap file with a lower priority (default −2) acts as the overflow if zram fills up.

The kernel uses the higher-priority device until it’s full. See Linux swap usage.

With zram, many people raise swappiness, since swapping to zram is cheap:

vm.swappiness = 100        # or even up to 180 on recent kernels, zram-only setups

Leave it lower (10–60) if you also rely on disk swap. See sysctl tuning.

When zram helps, and when it doesn’t

Helps:

  • 1–4 GB VPS plans running WordPress, small databases or Docker stacks with occasional spikes;
  • builds and package installs (npm, Composer, compilers) that briefly need more memory;
  • avoiding OOM kills during short peaks. See OOM killer.

Doesn’t help:

  • workloads that need much more memory all the time. You trade CPU for memory and still run out, so get more RAM;
  • CPU-starved servers, where compression adds load;
  • memory full of incompressible data.

zswap: the alternative

zswap is a compressed cache in front of disk swap, rather than a separate device. Pages go to compressed RAM first, and get written to disk when the pool fills. It’s built into many kernels (cat /sys/module/zswap/parameters/enabled). Use either zswap or zram, not both.

Quick reference

Task Command
Install (Ubuntu/Debian) apt install zram-tools
Config /etc/default/zramswap
Install (Fedora/RHEL) dnf install zram-generator
Status and ratio zramctl
Swap devices swapon --show
Algorithm zstd (ratio) or lz4 (speed)

Related: free command and vmstat.

  • Linux
  • Memory
  • Performance