When memory runs low, Linux pushes rarely used pages out to swap. The problem is that swap usually lives on disk. The moment disk swap gets busy, the system noticeably stutters. zram puts that swap in compressed RAM instead of on disk: rather than writing pages to disk, it compresses and keeps them in RAM.

The trade is simple. You spend a little extra CPU compressing and decompressing, and in return you turn disk I/O into RAM access. Text-like data typically compresses 2:1 to 4:1, so you get back the physical RAM that pages would have consumed, at less than half the cost.

Checking current swap

To see what zram replaces, first check where the current swap lives.

swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   4G   4G   -1

This machine’s swap is a 4GB disk file (/swap.img) at priority -1. Every time swap is used, pages travel to and from disk. free -h shows that swap is already full.

               total        used        free      shared  buff/cache   available
Mem:            38Gi        29Gi       447Mi       1.0Gi       9.3Gi       8.2Gi
Swap:          4.0Gi       4.0Gi       1.2Mi

What zram does is add a higher-priority swap device that lives in RAM. The kernel uses higher-priority swap first, so pages go out to zram (usually priority 100) before the disk file (-1). Compressed RAM becomes the first-line swap instead of disk.

zram is a compressed swap block device

zram is a Linux kernel feature that turns a portion of RAM into a virtual block device (/dev/zram0). Data written to this device is compressed before being stored in memory and decompressed on read. Register that device as swap, and swap pages head to compressed RAM instead of disk.

You can choose the compression algorithm among LZO, LZ4, ZSTD, and others (the supported list zramctl reports: lzo, lz4, lz4hc, deflate, 842, zstd). Ubuntu defaults to LZ4 for its balance of speed and ratio. Because compression is fast, pages come back with far less latency than waiting on disk I/O.

Installing on Ubuntu

On Ubuntu, the zram-config package is all you need. No hand-loading kernel modules, no scripts.

sudo apt install zram-config

Once installed, it creates a zram device at boot and registers it as swap. The default configuration is:

  • zram size set to about 50% of RAM (since data is compressed, actual RAM use is less than that)
  • LZ4 compression algorithm
  • swap priority 100, so it is used before disk swap (-1/-2)
  • automatically enabled at boot

For most desktop workloads these defaults are enough.

Verifying it and reading the compression ratio

After installing, run swapon --show again and the zram device appears above the disk file at a higher priority.

swapon --show
# NAME       TYPE  SIZE  USED  PRIO
# /dev/zram0 partition  ...    100   ← higher priority, used first
# /swap.img  file  4G    ...   -1

How much it actually compresses is shown by zramctl.

zramctl

The columns mean the following.

ColumnMeaning
DISKSIZEThe uncompressed capacity limit of the device
DATAThe uncompressed size of stored data (the original size)
COMPRThe compressed size that data actually occupies
TOTALReal RAM use, including metadata and fragmentation overhead

Read the compression ratio as DATA ÷ COMPR. If DATA is 1.2G but COMPR is 400M, that is about 3:1: 1.2GB worth of data occupies only 400MB in RAM. The physical RAM actually saved is DATA − TOTAL.

When it helps a lot, and when it barely does

zram only matters when swap is actually being used, because its whole point is rerouting pages that would have gone to disk into compressed RAM.

  • Where it helps a lot: 8GB RAM or less, heavy browser-tab usage, memory-hungry IDEs and VMs. The stutter from disk swap turns into compressed-RAM access.
  • Where it barely helps: systems with plenty of RAM (e.g. 32GB+) where swap is rarely touched. If swap never happens, zram never gets to step in.

The CPU cost is smaller than you might expect. Compression and decompression overhead is usually a single-digit percentage, and the disk I/O wait it eliminates is far larger. On a workload that already saturates the CPU at 100%, the compression cost can become a burden.

If you need hibernation, look at zswap rather than zram. zram is pure RAM, so its contents vanish when power is cut. zswap acts as a compression cache that can spill to disk swap when needed, so it works alongside disk-swap-based hibernation.