How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
What is Linux swap space, how to create a swap file, configure swappiness, monitor swap usage, and optimize memory performance on your server.
Swap space is virtual memory on disk that the operating system uses when physical RAM is full. It prevents system crashes under memory pressure and keeps applications running. This guide covers creating a swap file, configuring it, and monitoring usage.
Swap does not replace RAM; it only serves as an emergency buffer. Consistently high swap usage is a sign of insufficient RAM.
Swap space can be created in two ways:
| Method | Advantage | Disadvantage |
|---|---|---|
| Swap partition | Slightly faster | Fixed size |
| Swap file | Flexible, resizable | Slightly slower |
Swap files are preferred on modern systems because their size can be changed dynamically.
When is swap needed?
# Show swap usage
swapon --show
# Show memory and swap summary
free -h
# Detailed memory info
cat /proc/meminfo | grep -i swap
# List swap areas
cat /proc/swaps
# Create a 2 GB swap file (dd method)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
# Or with fallocate (faster)
sudo fallocate -l 2G /swapfile
# Verify file size
ls -lh /swapfile
# Only root can read/write
sudo chmod 600 /swapfile
# Verify permissions
ls -la /swapfile
# Output: -rw------- 1 root root 2.0G ...
# Mark as swap space
sudo mkswap /swapfile
# Activate swap
sudo swapon /swapfile
# Verify
swapon --show
free -h
Add to /etc/fstab so swap activates automatically on reboot:
# Add to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Verify
cat /etc/fstab | grep swap
To increase the existing swap file:
# Disable swap
sudo swapoff /swapfile
# Create new file with larger size (4 GB)
sudo fallocate -l 4G /swapfile
# Reformat
sudo mkswap /swapfile
# Re-enable
sudo swapon /swapfile
# Verify
free -h
Swappiness is a value between 0-100 that determines how aggressively the kernel uses swap:
| Value | Behavior |
|---|---|
| 0 | Use swap as little as possible |
| 10 | Recommended for servers |
| 60 | Linux default |
| 100 | Aggressive swap usage |
# Show current swappiness
cat /proc/sys/vm/swappiness
# Change temporarily (resets on reboot)
sudo sysctl vm.swappiness=10
# Change permanently
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
# Apply changes
sudo sysctl -p
swappiness=10 is recommended for servers. This minimizes swap usage before RAM is exhausted and reduces disk I/O.
vfs_cache_pressure can also be tuned alongside swappiness:
# Show current value
cat /proc/sys/vm/vfs_cache_pressure
# Less aggressive cache reclaim (50 recommended)
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Instant memory and swap status
free -h
# Continuous monitoring (every 2 seconds)
watch -n 2 free -h
# Which processes are using swap?
for file in /proc/*/status; do
awk '/VmSwap|Name/{printf $2 " " $3 "\n"}' $file
done | sort -k 2 -n -r | head -10
# Monitor with vmstat
vmstat 2 5
vmstat 1 5
# si: blocks swapped in per second
# so: blocks swapped out per second
# Consistently high si/so means insufficient RAM
# Temporarily disable
sudo swapoff /swapfile
# Permanently remove
sudo swapoff /swapfile
sudo rm /swapfile
# Also remove the swap line from /etc/fstab
Before disabling swap, make sure there is enough free RAM. Otherwise the system may encounter an OOM (Out of Memory) error.
Swap space is important for system stability, especially on servers with limited RAM. Creating a swap file, optimizing the swappiness value, and regularly monitoring swap usage improves your server's memory management. However, swap is not a permanent solution for insufficient RAM — if you see consistently high swap usage, consider upgrading RAM.
General rule: if RAM is 2 GB or less, create 2x RAM as swap; for 2-8 GB RAM, create swap equal to RAM; for more than 8 GB, 4-8 GB of swap is sufficient. If you use hibernate, you need at least as much swap as RAM.
High swap usage is a sign of insufficient RAM. First identify which processes use the most memory (with 'top' or 'htop'). Stop unnecessary services or consider upgrading RAM. Lowering swappiness to 10 can also help.
SSDs are much more durable than HDDs for write operations, but heavy swap usage can shorten SSD lifespan. Minimize swap usage with swappiness=10. On modern SSDs this is generally not a concern.
Swap files are preferred on modern systems: their size can be changed dynamically, they are easy to create, and the performance difference is negligible. Swap partitions are only preferred on older systems or for special requirements.
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.