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.
$ free -h total used freeMem: 2.0Gi 1.8Gi 200MiSwap: 4.0Gi 0.5Gi 3.5GiIf 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.
$ zramctlDEVICE ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS/dev/zram0 lz4 512M 128M 45M 80M 4That’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:
$ lsmod | grep zramzram 28672Enable at boot (Fedora/RHEL):
# Create a config filesudo tee /etc/systemd/zram-generator.conf <<EOF[zram0]zram-size = ram / 2algorithm = lz4swap-priority = 100EOF
sudo systemctl daemon-reloadsudo systemctl start systemd-zram-setup@zram0Check:
$ free -hMem: 2.0Gi 1.8Gi 200MiSwap: 1.0Gi 0.0Gi 1.0Gi ↑ this is zram nowFor Debian/Ubuntu:
sudo apt install zram-toolsecho "ALGO=lz4" | sudo tee /etc/default/zramswapecho "PERCENT=50" | sudo tee -a /etc/default/zramswapsudo systemctl restart zramswapManual setup (any distro):
# Load modulesudo modprobe zram
# Create 512MB zram deviceecho 512M | sudo tee /sys/block/zram0/disksize
# Enable compression (lz4, zstd, or deflate)echo lz4 | sudo tee /sys/block/zram0/comp_algorithm
# Set as swapsudo mkswap /dev/zram0sudo swapon /dev/zram0 -p 100Setup: Traditional Swap
For reference, swap file setup:
# Create 4GB swap filesudo dd if=/dev/zero of=/swapfile bs=1G count=4
# Secure itsudo chmod 600 /swapfile
# Initializesudo mkswap /swapfile
# Enablesudo swapon /swapfile
# Make persistentecho "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstabPerformance: The Real Numbers
Scenario: 2GB RAM server, 1GB zram + 4GB swap.
Test: Allocate 5GB of memory, access it.
| Metric | zram | Swap File | Swap Partition |
|---|---|---|---|
| Access latency | 100μs | 5-10ms | 5-10ms |
| Compression ratio | 3:1 | N/A | N/A |
| CPU overhead | 10-15% | None | None |
| Setup time | 30s | 5m | 10m |
| Wear on disk | None | Yes (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
- Small RAM servers (< 4GB): Every compression cycle saves disk I/O.
- SSDs: Swap writes cause wear. zram avoids it.
- Bursty workloads: A report spikes memory for 10 seconds, then drops. zram keeps it in fast RAM.
- Low-latency requirements: Game servers, databases, real-time systems.
# Ideal: zram + light swap fallback# 2GB zram (handles most spikes) + 2GB swap (just in case)When to Use Swap
- Large available space: Disk is cheap. RAM is not.
- Predictable workloads: You know exactly how much memory you’ll need.
- Low-CPU systems: Compression overhead matters on ARM or embedded.
- Ancient hardware: Older kernels may not support zram well.
Monitoring
Watch zram compression:
$ cat /sys/block/zram0/stat9 0 72 0 0 0 0 0 0 0 0
# or use zramctl$ zramctlDEVICE ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS/dev/zram0 lz4 512M 256M 85M 140M 4Watch swap usage:
$ watch -n 1 free -h$ vmstat 1 5If zram is consistently full and spilling to disk swap, you need more RAM.
Pro Tip: Hybrid Setup
Best of both:
# zram with high priority for fast accessswapon /dev/zram0 -p 100
# Traditional swap as fallback (slower, but unlimited)swapon /swapfile -p 50Now, common memory spikes hit fast zram. If you really run out, swap handles it (slowly, but better than OOM).
The Verdict
- zram: Use it. It’s faster, saves disk wear, and the CPU cost is worth it on small servers.
- swap: Keep 1-2GB as a safety net, but don’t rely on it for performance.
- Best practice: Start with zram. If CPU usage spikes during memory pressure, add traditional swap as a fallback.
Your 2 AM self—the one dealing with an OOM-killed database—will appreciate the breathing room.