Configure zRAM Swap for Faster Memory on Ubuntu Linux
This deep-dive covers zram swap ubuntu performance with actionable analysis, practical implications, and expert insights.
Why zRAM Outperforms Disk Swap on Modern Linux Systems
When a 4 GB VPS or 16 GB developer laptop runs low on free memory while compiling Rust, running Docker containers, or holding 60 browser tabs, hitting a disk-backed swapfile freezes the system for seconds at a time. zRAM creates a compressed block device directly in RAM. Instead of writing cold memory pages to an SSD, the kernel compresses them with zstd in microseconds, effectively expanding usable memory capacity while eliminating SSD wear.
Step 1: Install systemd-zram-generator on Ubuntu 24.04
Use the modern systemd-zram-generator package (the same tool Fedora ships by default) rather than legacy init scripts:
sudo apt update
sudo apt install -y systemd-zram-generator
If an older zram-config package is installed, remove it first so the two tools do not race over /dev/zram0:
sudo apt purge -y zram-config
Step 2: Configure /etc/systemd/zram-generator.conf
Create /etc/systemd/zram-generator.conf to allocate up to 50 percent of physical RAM (capped at 8 GB on workstations, or ram for 1-to-1 ratio on small 2 GB VPS hosts) using zstd compression and high swap priority:
[zram0]
zram-size = min(ram / 2, 8192)
compression-algorithm = zstd
swap-priority = 100
fs-type = swap
Activate the generated systemd unit immediately without rebooting:
sudo systemctl daemon-reload
sudo systemctl start systemd-zram-setup@zram0.service
Step 3: Verify zRAM Device and Compression Ratio
Inspect the active swap devices and live compression statistics:
zramctl
swapon --show
Under load, zramctl displays DATA (uncompressed bytes stored) alongside COMPR (actual physical RAM used), letting you verify your exact compression ratio in real time.
Step 4: Tune Linux VM Sysctl Parameters for zRAM
Ubuntu's default VM settings assume slow spinning disks or SSD swap. Applying zRAM-aware sysctl settings eliminates unnecessary read-ahead and prevents sluggishness under memory pressure:
cat <<'EOF' | sudo tee /etc/sysctl.d/99-zram-tuning.conf
vm.swappiness = 150
vm.page-cluster = 0
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
EOF
sudo sysctl --system
vm.page-cluster = 0: Disables multi-page swap read-ahead (reading 1 page at a time is optimal for RAM-backed random access).vm.swappiness = 150: Prefers compressing cold anonymous pages into zRAM over evicting active filesystem cache.vm.watermark_scale_factor = 125: Starts background reclaim slightly earlier so foreground apps never stall waiting for free pages.
Recommended zRAM Sizing by Machine Class
| System RAM | Recommended zram-size | Algorithm | Optional Disk Swap |
|---|---|---|---|
| 1 to 2 GB VPS | ram (100%) | zstd | 1 GB low-priority fallback |
| 4 to 8 GB Laptop/VPS | ram or ram / 2 | zstd | Optional (priority 10) |
| 16 to 32 GB Workstation | min(ram / 2, 8192) | zstd or lz4 | Only if using hibernate |
Summary
Setting up systemd-zram-generator with zstd compression and vm.page-cluster = 0 takes two minutes on Ubuntu 24.04 and immediately makes memory-constrained laptops and cloud VPS instances feel noticeably more responsive under heavy build or container loads.
Was this technical analysis helpful?
Your feedback informs our benchmark updates and ongoing article audits.
FAQ
Frequently Asked Questions
Structured for search engines and AI answer systems (AEO/GEO).
If you set zRAM to swap-priority = 100 and disk swap to priority 10, Linux always uses fast zRAM first and only spills to disk as a last-resort safety net before the OOM killer triggers. If you do not use hibernate, zRAM alone works great on most VPS servers.
Linux kernels 5.8+ allow swappiness up to 200. Because compressing anonymous pages into zRAM is far cheaper than evicting disk file cache and re-reading files from SSD, values between 100 and 180 keep hot file caches resident.
lz4 offers the lowest CPU latency (~2.1x compression), while zstd gives significantly better space savings (~3.0x+ compression) with very fast decompression on modern CPUs. zstd is the best default on Ubuntu 24.04.
No. zRAM allocates physical RAM dynamically only as pages are actually swapped in. An idle 8 GB zRAM device consumes practically zero physical RAM.
More answers in our FAQ hub.
Newsletter
Practical Linux & Applied AI Newsletter
Get new tested tutorials and benchmark comparisons delivered directly to your inbox. Zero spam.
Join Weekly guides readers getting our weekly digest.
- Practical Linux administration & terminal workflows
- Tested open-source AI setups & benchmarks
- Zero spam - unsubscribe anytime