How to Configure Compressed RAM Swap Using zram-tools in Ubuntu

On resource-constrained cloud VPS instances, edge compute nodes, or developer workstations running memory-intensive workloads like Docker containers, running out of physical RAM triggers aggressive system thrashing. Traditional disk-based swap files (on SSDs or mechanical hard drives) introduce severe I/O bottlenecks and accelerate drive wear through constant read-write write amplification.

The Linux kernel features zram, an integrated memory driver that creates compressed block devices directly inside physical RAM. Through the Ubuntu zram-tools package, pages that would otherwise be swapped out to slow disk storage are compressed on the fly using high-performance algorithms (such as LZ4 or Zstandard), effectively doubling or tripling available working memory without purchasing additional hardware.

How zram Compresses Memory

Unlike standard swap files located on /swapfile or physical partitions, a zram swap device resides in volatile system RAM:

  • When physical memory fills up, the Linux virtual memory manager pages out idle anonymous memory to the designated /dev/zram0 device.
  • The kernel compresses these pages using ultra-fast CPU-accelerated algorithms with typical compression ratios between 2:1 and 3:1.
  • A page of 4 KB data occupies only ~1.5 KB in RAM once compressed, allowing your system to hold significantly more active data in physical memory before triggering the Out-of-Memory (OOM) killer.

Installing zram-tools on Ubuntu

Ubuntu provides official packages to automatically configure and initialize zram devices during boot:

  1. Open a terminal on your Ubuntu machine.
  2. Update your package lists and install the utility:
    sudo apt update && sudo apt install zram-tools

Configuring zram Compression Settings

The default behavior of zram-tools is governed by its central configuration file:

  1. Open /etc/default/zramswap in an administrative text editor:
    sudo nano /etc/default/zramswap
  2. Customise the key parameters according to your server’s hardware capacity:
    # Percentage of total RAM to allocate to the zram swap device
    PERCENT=50
    
    # Compression algorithm (lz4, zstd, or lzo)
    # zstd offers higher compression ratios, while lz4 provides lower CPU overhead
    ALGO=zstd
    
    # Priority of the zram swap device relative to physical disk swap
    PRIORITY=100
  3. Setting PRIORITY=100 ensures that the kernel always prioritises the lightning-fast compressed RAM device over any legacy disk-based swap partition (which typically runs at priority -2).
  4. Save your changes and exit the editor.

Starting and Verifying zram Swap

Restart the zram systemd service to initialize the virtual block device:

sudo systemctl restart zramswap

To inspect your active swap spaces and confirm that zram is operational, execute:

swapon --show

Output:

NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition 3.8G   0B  100

You can query detailed compression statistics using the zramctl utility:

zramctl

Output:

NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd          3.8G   0B    0B   12K       4 [SWAP]

Tuning Kernel Swappiness for zram

Because swapping to zram operates at RAM bus speeds rather than disk speeds, you can configure the kernel to aggressively offload idle pages into compressed memory:

  1. Add a high swappiness value to /etc/sysctl.d/99-zram.conf:
    echo "vm.swappiness = 100" | sudo tee /etc/sysctl.d/99-zram.conf
  2. Apply the setting immediately:
    sudo sysctl --system

Configuring zram provides instantaneous resilience against out-of-memory lockups, maximizing RAM density on production servers and developer laptops.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.