Table of Contents
How to Prevent VPS Crashes on AlmaLinux 8 with Safe Sysctl Tuning and ZRAM
This guide shows safe sysctl values, the right way to add swap, and how to enable ZRAM so your server degrades gracefully instead of falling over.
Why Small VPSes Crash Under Load
- Apps and caches quickly consume 2 GB RAM.
- Too little or poorly tuned swap = no room to push idle pages.
- OOM-killer kicks in and kills MySQL, PHP-FPM, Node, etc.
Step 1: Add (or Increase) Swap
With 2 GB RAM, aim for 2–4 GB swap. If you only have 1 GB, add another 2 GB file:
sudo fallocate -l 2G /swapfile2
sudo chmod 600 /swapfile2
sudo mkswap /swapfile2
sudo swapon /swapfile2
echo '/swapfile2 none swap sw 0 0' | sudo tee -a /etc/fstab
This gives the kernel breathing room when memory pressure rises.
Step 2: Safe Sysctl Tuning for Low-Memory VPS
Use fixed byte limits for dirty writeback; ratios can overshoot on small RAM. Create /etc/sysctl.d/99-vps-memory.conf:
# Predictable writeback limits
vm.dirty_background_bytes = 67108864 # 64 MB
vm.dirty_bytes = 134217728 # 128 MB
# Swap a bit earlier to avoid sudden OOM
vm.swappiness = 60
# Reasonable inode/dentry reclaim (near default)
vm.vfs_cache_pressure = 100
sudo sysctl --system
Why this works: bounded dirty pages limit I/O stalls; moderate swappiness makes room before memory gets critical.
Step 3: Enable ZRAM (Fast, Compressed Swap in RAM)
ZRAM creates a compressed swap device in RAM—much faster than disk swap and perfect for tiny VPSes.
Install zram-generator
sudo dnf install -y epel-release
sudo dnf install -y zram-generator
Configure ZRAM
Create /etc/systemd/zram-generator.conf:
[zram0]
zram-size = ram * 0.75
compression-algorithm = zstd
swap-priority = 100
Activate
sudo systemctl daemon-reexec
sudo systemctl start /dev/zram0
swapon --show
You should see both /dev/zram0 (preferred) and your disk swap (fallback).
Step 4: Verify and Monitor
free -h
swapon --show
dmesg -T | egrep -i 'out of memory|oom-killer'
No more OOM lines? You’re stable.
Tuning Your Stack
- MariaDB/MySQL: keep
innodb_buffer_pool_sizein the 256–512 MB range on 2 GB RAM. - PHP-FPM: lower
pm.max_childrento prevent pileups. - Node/Java: set explicit memory caps (e.g.,
--max-old-space-size,-Xmx). - Apache: prefer
eventMPM or switch to Nginx for leaner memory.
- Setting
vm.swappinesstoo low (e.g., 1–10) on 2 GB RAM delays reclaim until it’s too late. - Over-aggressive
vm.vfs_cache_pressure(e.g., 300) can thrash caches and slow I/O without improving stability. - Relying only on disk swap—use ZRAM first for better responsiveness.
FAQ
What actually causes the “crash” on my 2 GB VPS?
In most cases it’s the Linux OOM-killer terminating heavy processes when memory and swap are exhausted, not the kernel itself panicking.
How much swap should I use on a 2 GB server?
Use 2–4 GB total. Pair fast ZRAM (≈75% of RAM) with a smaller disk swap file for overflow.
Is vm.swappiness=60 safe?
Yes for small RAM servers with ZRAM. It encourages earlier reclaim into fast compressed swap and reduces sudden OOM events.
What’s the difference between ZRAM and ZSWAP?
ZRAM is an in-RAM compressed swap device. ZSWAP is a compressed cache in front of your existing swap device. For tiny VPSes, ZRAM is simpler and very effective.
Will using swap hurt my SSD?
ZRAM doesn’t touch disk at all. Disk swap writes more, but on VPSes it’s acceptable for stability; keep it as overflow behind ZRAM.
How do I undo these changes?
Remove or comment the sysctl lines and run sudo sysctl --system. Stop ZRAM with sudo swapoff /dev/zram0 and sudo systemctl stop /dev/zram0, then delete the zram-generator config.
Do I still need to optimize my apps?
Yes. Kernel tweaks prevent catastrophic failures, but setting sane memory limits for databases, PHP-FPM, Node, and Java is still essential.