Linux VPS

Use zram compressed swap on a small Linux VPS

Set up zram on a low-memory VPS to get compressed swap in RAM: how it differs from a swap file, install with zram-tools or systemd zram-generator, tune size and swappiness, and monitor it.

IntermediateLinux VPS2 min read

On a 1–2 GB VPS, running out of memory can crash your database or web server. zram creates a compressed block device in RAM and uses it as swap, so rarely used pages are squeezed rather than written to slow disk. It often gives noticeably more usable memory on small servers.

Before you start

  • A KVM VPS (OpenVZ/LXC containers usually cannot set up swap; see OpenVZ explained).
  • Check current memory and swap with free -h and swapon --show.

zram vs swap file

zramSwap file on disk
SpeedFast (RAM + compression)Much slower
CPU costSome, for compressionMinimal
Disk wear and I/ONoneYes
CapacityLimited by RAM and compression ratioLimited by disk

They can be combined: zram with higher priority, a small disk swap file as a last resort. See create a swap file.

Set up on Debian/Ubuntu with zram-tools

sudo apt install -y zram-tools

Edit /etc/default/zramswap:

ALGO=zstd
PERCENT=50
PRIORITY=100
sudo systemctl restart zramswap

Set up with systemd zram-generator

On Fedora-family systems and others that package zram-generator (on AlmaLinux/Rocky it may require EPEL or a manual build), create /etc/systemd/zram-generator.conf:

[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100
sudo systemctl daemon-reload && sudo systemctl start systemd-zram-setup@zram0

Tune swappiness

With fast zram swap, a higher swappiness is reasonable:

echo "vm.swappiness=100" | sudo tee /etc/sysctl.d/99-zram.conf && sudo sysctl --system

Start there and adjust if you see unwanted swapping of active processes.

Check that it works

swapon --show
zramctl

zramctl shows the original data size and compressed size, so you can see the compression ratio.

Limits

  • zram is not a substitute for enough RAM: if the working set does not fit, performance drops.
  • Compression uses CPU; on a VPS with heavy CPU steal it helps less.
  • Fix memory-hungry settings (PHP-FPM children, database buffers) first; see high RAM usage.

Frequently asked questions

Should zram be half my RAM?

50% is a common default; it is a maximum, and only compressed pages actually use memory.

Which algorithm is best?

zstd gives a good ratio; lz4 is faster with less compression.

Will zram help MySQL?

It helps the system survive memory peaks, but database buffers should be sized to fit RAM.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.