Skip to content
Go back

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

By SumGuy 5 min read
zram vs Swap: What's Actually Faster for Low-RAM Servers

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 disk—slow.

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.

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.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it may appear here.


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

Related Posts