What is swap and why resize it?
Swap is a designated portion of your hard drive that acts as an extension of your system's RAM. When your physical memory (RAM) fills up, the operating system moves less-used memory pages to swap space, freeing up physical RAM for active processes. Essentially, swap provides a safety net to prevent out-of-memory errors, albeit at the cost of slower performance compared to RAM.
Common reasons to resize swap:
Insufficient swap space: If your system is running out of memory during peak usage
Over-allocation: If you've allocated too much disk space to swap that could be better used elsewhere
Performance tuning: Adjusting swap to match your system's workload patterns
Preventing OOM (Out Of Memory) kills: Particularly important for database servers or memory-intensive applications
Recommended swap sizes:
System RAM | Recommended Swap Size | Swap Size (with Hibernation) |
---|---|---|
2GB or less | 2x RAM | 3x RAM |
2GB to 8GB | Equal to RAM | 2x RAM |
8GB to 64GB | At least 4GB | 1.5x RAM |
64GB or more | At least 4GB | Hibernation not typically used |
Prerequisites
Before modifying your swap configuration, ensure you have:
Root or sudo privileges: All commands in this guide require elevated permissions
Sufficient free disk space: Verify available space with
df -h
Backup of critical data: System-level changes always carry some risk
Understanding of current swap usage: Check with these commands:
# Show current swap files/partitions and their sizes
sudo swapon --show
# View memory and swap usage
free -h
# Check if swap is being actively used
vmstat 1 5
Step-by-Step process to resize swap
1. Prepare your system
Before disabling swap, check if your system has enough free RAM to handle the current swap usage:
# Check current memory usage
free -h
If swap is heavily used, consider closing memory-intensive applications first or performing this operation during a maintenance window.
2. Turn off all running swap processes
sudo swapoff -a
This command disables all swap spaces defined in /etc/fstab
and currently active. It may take some time to complete as the system moves data from swap back to RAM.
3. Resize the swap file
For this example, we'll create a 4GB swap file (adjust the size to your needs):
# Remove the old swap file if it exists
sudo rm /swapfile
# Create a new swap file with desired size (4G in this example)
sudo fallocate -l 4G /swapfile
# Alternative method if fallocate fails:
# sudo dd if=/dev/zero of=/swapfile bs=1G count=4
Note: Replace
4G
with your desired swap size (e.g.,2G
for 2GB,8G
for 8GB).
4. Set proper permissions and ownership
Swap files should be readable and writable only by root for security:
# Set correct permissions (only root can read/write)
sudo chmod 600 /swapfile
# Verify permissions
ls -lh /swapfile
5. Set up the swap area
# Format the file as swap
sudo mkswap /swapfile
You should see output confirming the swap file creation.
6. Activate the swap file
# Enable the swap file
sudo swapon /swapfile
# Verify the swap is active
sudo swapon --show
free -h
7. Make the changes persistent across reboots
To ensure your swap file is automatically activated after system restart, you need to update the /etc/fstab
file:
# First, backup the fstab file
sudo cp /etc/fstab /etc/fstab.bak
# Check if swap entry already exists
grep -q "swapfile" /etc/fstab || echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Optimizing swap performance
Adjusting swappiness
Swappiness is a kernel parameter that influences how aggressively the system uses swap space. Values range from 0 to 100:
Lower values (e.g., 10): The system will try to avoid swapping as much as possible
Higher values (e.g., 60): The system will swap out memory pages more aggressively
# Check current swappiness value
cat /proc/sys/vm/swappiness
# Temporarily change swappiness (until reboot)
sudo sysctl vm.swappiness=10
# Make the change permanent (add to /etc/sysctl.conf)
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Adjusting cache pressure
Cache pressure determines how aggressively the kernel reclaims memory used for caching filesystem data:
# Check current cache pressure
cat /proc/sys/vm/vfs_cache_pressure
# Set a more balanced value (100 is default)
sudo sysctl vm.vfs_cache_pressure=50
# Make permanent
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
Troubleshooting common issues
Swap file not activating after reboot
If your swap doesn't activate after reboot:
Check
/etc/fstab
for correct syntax:cat /etc/fstab
Manually try to enable the swap:
sudo swapon /swapfile
Look for errors in system logs:
sudo dmesg | grep -i swap sudo journalctl -xe
"Operation not permitted" error
If you see this error when trying to create or enable swap:
Check if you're using a filesystem that supports swap files (most do, but some like NFS don't)
Verify you have proper permissions and are using sudo
Performance issues
If your system becomes sluggish after configuring swap:
Consider reducing swappiness to a lower value (10-30)
Check if your swap is on an SSD for better performance
Monitor swap usage with
htop
orfree -h
to see if your system is swap-thrashing
Performance considerations
SSD vs HDD: Swap on an SSD will be significantly faster than on a traditional hard drive
Separate partition: For heavy swap usage, consider using a dedicated partition instead of a file
Multiple swap files: For very large systems, multiple swap files on different physical disks can improve performance
Monitoring swap usage
To effectively monitor swap usage over time:
# Install sysstat package for monitoring tools
sudo apt install sysstat
# Monitor swap usage every 5 seconds
vmstat 5
# Get detailed memory and swap statistics
free -h
# For graphical monitoring, consider installing and using htop
sudo apt install htop
htop
Conclusion
Properly configured swap space is an important part of system memory management in Ubuntu. While it's not a replacement for physical RAM, it provides a safety buffer against memory exhaustion and helps prevent application crashes during periods of high memory demand.
For production environments on Ploi, regularly monitor your swap usage patterns to determine if you need to adjust the swap size or consider upgrading your server's RAM for better performance.