Monday, January 19, 2026

What Happens When You Run swapoff -a on a Running Server?

 

What Happens When You Run swapoff -a on a Running Server?

✅ 1. All swap pages are moved into RAM

swapoff disables swap.
But before the kernel can turn it off, every page currently in swap must be loaded back into physical RAM.

If enough free RAM is available

→ System continues normally.
→ You’ll probably notice some slowdown as the system swaps pages back into memory.

If RAM is insufficient

→ Linux starts struggling to free enough memory
→ Can trigger the OOM-Killer (Out Of Memory Killer)


❌ 2. OOM-Killer might kill important services

The kernel tries to free memory by killing processes based on OOM score.

This can result in:

  • Database services dying (MySQL, PostgreSQL, Oracle, MongoDB)
  • JVM applications crashing
  • Docker containers terminating
  • SSH sessions closing
  • Even the kernel panicking in rare cases

As a Database Architect, this is high-risk because DB instances usually use large buffers (Oracle SGA, MySQL InnoDB buffer pool).


⚠️ 3. System may freeze or become unresponsive

If memory pressure becomes too high:

  • System may hang
  • Commands stop responding
  • You might lose remote access
  • Only a forced reboot recovers the machine

🧠 4. Why swap was being used matters

If swap is actively used heavily

Turning it off is dangerous because the system needed that memory.

If swap is barely used

Impact is minimal. You can check with:

free -h

or:

swapon --show


🛡️ Best Practices Before Running swapoff

✔️ 1. Check swap usage

If swap is >20–30% used, consider scaling RAM or stopping heavy services first.

✔️ 2. Add temporary RAM using zRAM (safe method)s

systemctl enable --now zramswap.service

✔️ 3. Reduce swap use instead of disabling it

Usually the goal is to stop active swapping, not to disable swap entirely.
Use:

sysctl -w vm.swappiness=10

✔️ 4. Only disable swap during maintenance window

Especially on DB or production servers.


🧩 Summary

SituationWhat happens after swapoff
Enough free RAMSystem works normally, small slowdown.
Moderate swap in useSystem slows, may trigger OOM-Killer.
Heavy swap usage + low RAMHigh chance of service crash or freeze.
DB servers under loadVery high risk of outages.

If you want, I can also help you evaluate your server’s swap usage and memory safety before you disable swap — just share the output of:

free -h
swapon --show
top -o %MEM | head -20

No comments:

Post a Comment

What Happens When You Run swapoff -a on a Running Server?

  What Happens When You Run swapoff -a on a Running Server? ✅ 1. All swap pages are moved into RAM swapoff disables swap. But before the k...