ZRAM

ZRAM

- 5 mins

Running Out of RAM on Linux? Use ZRAM Before Upgrading

If your Linux system often freezes, swaps heavily, or even invokes the OOM (Out of Memory) killer, you may be thinking about buying more RAM. But before you spend money, try ZRAM — a kernel feature that creates a compressed swap device in RAM, effectively extending your memory capacity while keeping performance snappy.


What This Guide Covers


What is ZRAM?

ZRAM is a Linux kernel module that creates a compressed block device in RAM. This device is then used as swap space, which helps your system avoid slow disk-based swap (especially on HDDs or SSDs).

Because the data is compressed, you can often fit 3–4x more into RAM than physically available.

On modern CPUs, Zstd compression is recommended because it strikes the best balance between compression ratio and speed.

Example:

With 16 GB RAM and 8 GB allocated to ZRAM (zstd), you may effectively hold 32–40 GB of data in memory before swapping to disk.

ZRAM Compression Algorithm Benchmarks
Algorithm Time Data Compressed Ratio
lzo 4.6s 1.1G 387.8M 2.68
lzo-rle 4.5s 1.1G 388M 2.68
lz4 4.5s 1.1G 403.4M 2.58
lz4hc 14.6s 1.1G 362.8M 2.87
zstd 7.8s 1.1G 285.3M 3.96

Source: linuxreviews.org/Zram


Step 1: Check Your Current RAM Usage

Before adding ZRAM, check how much RAM you’re using:

free -h

zram

This shows your total, used, and available memory. You’ll later compare this after enabling ZRAM.


Step 2: Install ZRAM on Debian/Ubuntu

On Debian 12+, Ubuntu 22.04+, and derivatives (Linux Mint, etc.), install the ZRAM tools:

sudo apt update
sudo apt install zram-tools

This package automatically manages ZRAM setup on boot.

Verify the module is loaded:

lsmod | grep zram

zram

Step 3: Configure ZRAM Swap

Edit the configuration file:

sudo vim /etc/default/zramswap

Recommended settings for modern CPUs:

ALGO=zstd
PERCENTAGE=50
PRIORITY=100

zram

Save and restart the service:

sudo systemctl restart zramswap

Check if ZRAM is active:

zramctl
swapon --show

zram

You should see /dev/zram0 listed.

Linux defaults may still cause unnecessary disk swapping. Adjust kernel parameters with a sysctl config:

sudo vim /etc/sysctl.d/99-zram-tweaks.conf

Add:

vm.swappiness=80
vm.vfs_cache_pressure=50
vm.dirty_background_ratio=5
vm.dirty_ratio=10

Apply changes:

sudo sysctl --system

zram

Explanation of Key Settings:


Step 5: Monitor ZRAM Usage

To monitor usage:

free -h
zramctl

zram

You should see increased available memory and ZRAM usage instead of immediate disk swapping.

Real-World Results

With ZRAM enabled and tuned:


Conclusion

If you’re hitting memory limits on Linux, try ZRAM before buying more RAM:

ZRAM is especially useful on:


Thanks for reading!

Guneycan Sanli

Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus