Skip to content
Go back

zram vs Swap: What's Actually Faster for Low-RAM Servers

· Updated:
By KingPin 7 min read
zram vs Swap: What's Actually Faster for Low-RAM Servers
Contents

zram wins for small-RAM servers and SSD-backed boxes where spare CPU cycles beat disk I/O, and plain swap still earns its keep as the unlimited fallback on low-CPU or ancient hardware that can’t spare the compression overhead.

Your server has 2GB of RAM. It’s fine most of the time. Then you do a backup, or a report runs, or something goes sideways, and RAM runs out.

Now what? Linux starts killing processes (OOM killer, the cold reaper). Or you set up swap. But swap means writing to diskslow.

Enter zram: compressed RAM that acts like swap but lives in, well, RAM. Faster than disk, but uses CPU to compress. Let’s dig into the tradeoffs.

What’s Swap?

Traditional swap is a file (or partition) on disk where Linux pages out memory when RAM fills up.

Upside: Unlimited. You can allocate more swap than physical RAM.

Downside: Disk I/O is slow. A single page fault to disk costs thousands of CPU cycles.

Terminal window
$ free -h
total used free
Mem: 2.0Gi 1.8Gi 200Mi
Swap: 4.0Gi 0.5Gi 3.5Gi

If you hit swap, performance tanks.

What’s zram?

zram is a Linux kernel module that creates a block device in RAM. When you page memory into zram, it compresses it on the fly.

Upside: Faster than disk. Transparent to applications.

Downside: Uses CPU for compression. Not unlimited, it’s still RAM. Typical compression ratio is 3:1, so 2GB zram holds ~6GB of compressed data.

A note on algorithm choice: current kernels (see cat /sys/block/zram0/comp_algorithm for what’s available on yours) ship lzo, lzo-rle, lz4, lz4hc, zstd, and deflate. zstd gets a better compression ratio than lz4 at a higher CPU cost, and it’s now a common default on current kernels, so it’s worth benchmarking against lz4 on your own workload instead of assuming lz4 is still the best default.

Terminal window
$ zramctl
DEVICE ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS
/dev/zram0 lz4 512M 128M 45M 80M 4

That’s 128MB of data compressed to 45MB, using 80MB actual RAM. Win.

Setup: zram

Most modern distros can enable zram out of the box. Check:

Terminal window
$ lsmod | grep zram
zram 28672

Enable at boot (Fedora/RHEL):

Terminal window
# Create a config file
sudo tee /etc/systemd/zram-generator.conf <<EOF
[zram0]
zram-size = ram / 2
algorithm = lz4
swap-priority = 100
EOF
sudo systemctl daemon-reload
sudo systemctl start systemd-zram-setup@zram0

Check:

Terminal window
$ free -h
Mem: 2.0Gi 1.8Gi 200Mi
Swap: 1.0Gi 0.0Gi 1.0Gi
↑ this is zram now

For Debian/Ubuntu:

Terminal window
sudo apt install zram-tools
echo "ALGO=lz4" | sudo tee /etc/default/zramswap
echo "PERCENT=50" | sudo tee -a /etc/default/zramswap
sudo systemctl restart zramswap

Manual setup (any distro):

Terminal window
# Load module
sudo modprobe zram
# Create 512MB zram device
echo 512M | sudo tee /sys/block/zram0/disksize
# Enable compression (lz4, zstd, or deflate)
echo lz4 | sudo tee /sys/block/zram0/comp_algorithm
# Set as swap
sudo mkswap /dev/zram0
sudo swapon /dev/zram0 -p 100

Setup: Traditional Swap

For reference, swap file setup:

Terminal window
# Create 4GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
# Secure it
sudo chmod 600 /swapfile
# Initialize
sudo mkswap /swapfile
# Enable
sudo swapon /swapfile
# Make persistent
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

Performance: The Real Numbers

Scenario: 2GB RAM server, 1GB zram + 4GB swap.

Test: Allocate 5GB of memory, access it.

MetriczramSwap FileSwap Partition
Access latency100μs5-10ms5-10ms
Compression ratio3:1N/AN/A
CPU overhead10-15%NoneNone
Setup time30s5m10m
Wear on diskNoneYes (flash)Yes

Bottom line: zram is ~100x faster for swap, but costs CPU. Swap is “free” in latency but kills I/O performance.

When to Use zram

Terminal window
# Ideal: zram + light swap fallback
# 2GB zram (handles most spikes) + 2GB swap (just in case)

When to Use Swap

Monitoring

Watch zram compression:

Terminal window
$ cat /sys/block/zram0/stat
9 0 72 0 0 0 0 0 0 0 0
# or use zramctl
$ zramctl
DEVICE ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS
/dev/zram0 lz4 512M 256M 85M 140M 4

Watch swap usage:

Terminal window
$ watch -n 1 free -h
$ vmstat 1 5

If zram is consistently full and spilling to disk swap, you need more RAM.

Pro Tip: Hybrid Setup

Best of both:

Terminal window
# zram with high priority for fast access
swapon /dev/zram0 -p 100
# Traditional swap as fallback (slower, but unlimited)
swapon /swapfile -p 50

Now, common memory spikes hit fast zram. If you really run out, swap handles it (slowly, but better than OOM).

The Verdict

Your 2 AM self, the one dealing with an OOM-killed database, will appreciate the breathing room.

Common Questions

Can I use zram and a swap file together?

Yes. Give the zram device the higher priority, for example swapon /dev/zram0 -p 100, and the swap file a lower one, such as -p 50. Common memory spikes land in fast zram, and the swap file catches the overflow slowly, which still beats an OOM kill.

How much data can a zram device hold?

A 2GB zram device holds about 6GB of data at the typical 3:1 compression ratio. The zram device is not unlimited, because the compressed data still lives in RAM. If zram stays full and spills to disk swap, the server needs more RAM.

Does zram wear out my SSD?

No. zram keeps compressed pages in RAM, so it causes no wear on disk. Swap on an SSD writes to flash, which does wear it. The price of zram is CPU time, about 10-15% overhead in the test scenario in this post.

Should I use lz4 or zstd for zram?

Benchmark both on your own workload before choosing. zstd gets a better compression ratio than lz4 at a higher CPU cost, and zstd is now a common default on current kernels. Run cat /sys/block/zram0/comp_algorithm to see which algorithms your kernel offers.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Docker Healthcheck Patterns That Actually Work
Next Post
Running Docker Containers as Non-Root (And Why You Should)

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts