Managing Swap Space: Creating, Enabling, and Resizing Swap Files
On a Hovixa VPS, Swap Space acts as a safety net for your system's RAM. When the physical memory is fully utilized, the Linux kernel moves inactive pages from RAM to the swap space on your NVMe storage. While disk-based memory is slower than RAM, having an adequately sized swap file prevents the OOM (Out-Of-Memory) Killer from terminating critical processes like MySQL or Nginx during traffic spikes.
1. Understanding Swap Architecture
Swap can be implemented as a dedicated partition or a swap file. For VPS environments, swap files are preferred because they can be resized or moved without modifying the partition table.
2. Creating and Enabling a Swap File
Follow these steps to create a 2GB swap file. Adjust the 2G parameter based on your specific requirements (usually 1x to 2x your RAM size for smaller instances).
# 1. Allocate space (fallocate is faster than dd)
sudo fallocate -l 2G /swapfile
# 2. Secure the file (CRITICAL: Swap must not be world-readable)
sudo chmod 600 /swapfile
# 3. Set up the swap area
sudo mkswap /swapfile
# 4. Enable the swap
sudo swapon /swapfile
3. Making Swap Persistent
By default, the swapon command is temporary. To ensure your Hovixa VPS enables swap automatically after a reboot, you must add an entry to your filesystem table.
# Backup fstab first
sudo cp /etc/fstab /etc/fstab.bak
# Append the swap entry
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
4. Tuning Swappiness and Cache Pressure
The Swappiness parameter (0-100) defines how aggressively the kernel uses swap. A lower value (e.g., 10) tells the kernel to avoid swapping unless absolutely necessary, which is ideal for performance on a VPS.
| Parameter | Suggested Value | Technical Impact |
|---|---|---|
| `vm.swappiness` | `10` | Prioritizes RAM; reduces disk I/O overhead. |
| `vm.vfs_cache_pressure` | `50` | Determines how long the kernel keeps directory/inode caches in RAM. |
# Apply settings permanently in sysctl.conf
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50
5. Resizing an Existing Swap File
To increase or decrease swap, you must disable the current file, resize it (or recreate it), and re-enable it.
- Disable:
sudo swapoff /swapfile - Resize:
sudo fallocate -l 4G /swapfile(to change to 4GB) - Format:
sudo mkswap /swapfile - Enable:
sudo swapon /swapfile
6. Technical Implementation Details
- NVMe Lifespan: Because Hovixa uses NVMe, swap performance is excellent. However, excessive swapping (thrashing) can lead to higher write wear. Always tune your applications to fit in RAM where possible.
- fallocate vs dd:
fallocateis nearly instantaneous because it marks the blocks as allocated without writing zeros. If your filesystem doesn't support it, use:sudo dd if=/dev/zero of=/swapfile bs=1M count=2048. - Verification: Use
free -horswapon --showto confirm the total swap available and its current utilization.
Sysadmin Advice: If you see your swap usage consistently climbing while RAM is free, your **swappiness** is too high. If you see your VPS hanging during high traffic, your swap might be too small or non-existent, causing the OOM Killer to panic and reboot the system.
