Back to site

Swap and ZRAM

Configure a swap partition or ZRAM on a HolyCloud VPS to absorb memory spikes without exhausting RAM.

Swap and ZRAM

A HolyCloud Linux VPS with little RAM (1–2 GB) can be killed by the OOM killer during spikes (build, MySQL, PHP). Disk swap or ZRAM (compressed RAM) adds headroom. This guide compares both and details configuration.

Prerequisites

  • HolyCloud VPS Ubuntu 22.04/24.04 or Debian 12
  • sudo access
  • For disk swap: free disk space (often 1–2 GB is enough for a small VPS)

Note: Kubernetes/K3s recommends disabling classic swap. For a typical web server, swap or ZRAM remains relevant.

Understand swap vs ZRAM

| Criterion | Disk/file swap | ZRAM |

|---------|---------------------|------|

| Support | File or partition | Compressed block in RAM |

| Speed | Slow (disk I/O) | Faster |

| SSD wear | Yes (moderate) | No |

| Best for | VPS 2 GB+, simple margin | VPS 1 GB, short spikes |

Step 1: current state

free -h
swapon --show
cat /proc/swaps

Step 2: swap file (common method)

Recommended size: 50% to 100% of RAM for a web VPS (e.g. 2 GB RAM → 2 GB swap).

sudo fallocate -l 2G /swapfile
# Si fallocate échoue :
# sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Persistent in /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

swappiness (how often the kernel uses swap, 10–60 depending on load):

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl -p /etc/sysctl.d/99-swap.conf

Step 3: ZRAM with zram-generator (recent Ubuntu/Debian)

sudo apt update
sudo apt install -y zram-tools

Configuration /etc/default/zramswap or via generator — example /etc/systemd/zram-generator.conf:

[zram0]
zram-size = ram / 2
compression-algorithm = zstd

On some images:

sudo nano /etc/default/zramswap
# ALGO=zstd
# PERCENT=50
sudo systemctl restart zramswap
# ou
sudo systemctl enable --now zram-configure

Verify:

zramctl
free -h

Step 4: avoid excessive stacking

Avoid 2 GB disk swap + 50% ZRAM on a 2 GB VPS without reason — you hide real memory pressure. Choose one or the other for most HolyCloud cases.

Disable the swap file if you switch to ZRAM only:

sudo swapoff /swapfile
sudo sed -i '/\/swapfile/d' /etc/fstab
sudo rm /swapfile

Step 5: monitoring

watch -n 5 free -h
grep -i swap /var/log/syslog

If swap is constantly in use, upgrade RAM in your HolyCloud VPS plan rather than growing swap indefinitely.

Verification

free -h
swapon --show
zramctl 2>/dev/null || true
cat /proc/sys/vm/swappiness

Need help?

  • Slow services after enabling swap: normal under I/O load — consider more RAM
  • ZRAM missing after reboot: systemctl status zramswap, zram-tools package installed
  • HolyCloud support: free -h, swapon --show, VPS plan size and workload (MySQL, Docker, etc.)