When configuring a Linux web server with abundant RAM (12GB in your case), the swap partition debate becomes particularly relevant. Modern Linux kernels handle memory efficiently through:
# Check current memory usage (excluding buffers/cache)
free -h --gibi | awk '/Mem/{print "Used:", $3, "Free:", $4}'
# View swappiness value (default 60)
cat /proc/sys/vm/swappiness
Your friend's argument about Out-of-Memory (OOM) situations holds technical merit. Here's what happens during memory exhaustion:
# Simulate memory pressure (DANGER - test environment only)
stress-ng --vm 1 --vm-bytes 11G --vm-keep
With swap enabled, the system might:
- Page out SSH daemon to swap
- Trigger disk thrashing
- Create unresponsive conditions
For web servers handling HTTP traffic, consider this optimized approach:
# Completely disable swap
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
# OR alternatively set ultra-low swappiness
echo 'vm.swappiness = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Emergency memory limits for critical services
sudo systemctl set-property sshd.service MemoryLimit=512M
Before/after metrics to collect:
# Measure response times under load
ab -n 10000 -c 100 http://localhost/
# Monitor system responsiveness
dstat -tcmnd --disk-util --output monitor.csv
Swap remains beneficial for:
- Servers running memory-intensive batch jobs
- Systems with hibernation requirements
- Containers with memory limits
For these cases, consider dedicated swap files instead of partitions:
# Create 1GB swap file (more flexible than partition)
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
In Linux system administration, the swap partition serves as emergency memory when physical RAM is exhausted. For web servers with sufficient memory (like your 12GB configuration), the tradeoffs become particularly interesting:
# Check current swap usage
free -h
# Output example:
total used free shared buff/cache available
Mem: 12Gi 4.1Gi 6.8Gi 0.1Gi 1.1Gi 7.5Gi
Swap: 2.0Gi 0.0Gi 2.0Gi
Your friend makes valid technical points about OOM scenarios:
- With swap enabled, the system may degrade performance (thrashing) before finally crashing
- Critical admin processes (sshd) might get swapped out during emergencies
- Modern web applications rarely need swap during normal operation
# Temporary disable swap (without persistence)
sudo swapoff -a
# Verify swap status
cat /proc/swaps
There are legitimate use cases where swap remains valuable:
- Servers running memory-intensive background jobs (database maintenance, backups)
- Systems with unpredictable memory spikes
- Environments where graceful degradation is preferable to immediate crashes
For a balanced approach that maintains emergency access:
# /etc/sysctl.conf
# Reduce swap tendency (default 60)
vm.swappiness = 10
# Prioritize keeping SSH in memory
# /etc/security/limits.conf
sshd hard memlock unlimited
sshd soft memlock unlimited
# OOM killer adjustments
# /etc/sysctl.conf
vm.oom_kill_allocating_task = 1
vm.overcommit_memory = 2
vm.overcommit_ratio = 80
Instead of completely disabling swap, consider these middle-ground approaches:
# Create a smaller, dedicated swap file (512MB)
sudo fallocate -l 512M /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make persistent in /etc/fstab
/swapfile none swap sw 0 0
Implement these safeguards regardless of your swap decision:
# Install and configure earlyoom (OOM prevention)
sudo apt install earlyoom
sudo systemctl enable --now earlyoom
# Memory monitoring with alerts
# /etc/cron.d/memory_check
*/5 * * * * root /usr/bin/free -m | awk '/Mem/ {if ($3/$2 > 0.9) system("echo \"High memory usage\" | mail -s \"Memory Alert\" admin@example.com")}'