How to Add Swap Space on a Linux VPS (512MB and 1GB Plans)

12 min read 2 views 0
On this page

On a 512 MB or 1 GB cloud server, a database, a PHP-FPM pool and a package update can easily exceed the available RAM at the same moment. When that happens, the kernel's out-of-memory killer stops a process, very often MySQL or MariaDB. Adding swap space on a Linux VPS gives the system an overflow area on disk so short memory peaks no longer crash services. This guide creates a swap file on Debian, Ubuntu, CentOS, Rocky Linux and AlmaLinux, makes it permanent and tunes swappiness, with advice on when swap really helps on small plans.

Step 1: Check Current Memory and Swap

Start by looking at how much RAM and swap the server has, how much free disk space is left, and whether the kernel has already killed processes because memory ran out.

free -h
swapon --show          # no output = no swap yet
df -h /                # make sure there is enough free disk space
# Has the kernel already killed processes for lack of memory?
journalctl -k | grep -i -E 'out of memory|killed process' 

If swapon --show already lists a device or file, you may only need to tune it (Step 6) rather than add another one.

Step 2: Choose the Right Swap Size

On a VPS, swap is a safety buffer, not a replacement for RAM. These sizes work well for typical small plans:

Plan RAMSuggested swapNotes
512 MB1 GBStrongly recommended
1 GB1 GB - 2 GBRecommended for databases and control panels
2 GB1 GBSafety buffer
4 GB and more1 GB - 2 GB or noneOnly as an emergency buffer

Keep in mind that the swap file uses space on the server's SSD, so check the free space from Step 1 before choosing a size.

Step 3: Create the Swap File with fallocate or dd

fallocate reserves the space instantly. Some file systems do not support it for swap; in that case dd writes the file block by block, which takes longer but always works. The file must be readable by root only.

# Fast method (ext4, XFS)
fallocate -l 1G /swapfile

# If fallocate fails, or swapon later reports "file has holes", use dd instead
rm -f /swapfile
dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress

chmod 600 /swapfile
ls -lh /swapfile
On Btrfs, swap files need special handling (no copy-on-write); use a dedicated method for that file system. On container-based virtualization, swapon may fail with "Operation not permitted" because swap is controlled by the host.

Step 4: Format and Enable the Swap File (mkswap and swapon)

mkswap writes the swap signature and swapon activates it immediately, without a reboot.

mkswap /swapfile
swapon /swapfile

swapon --show
free -h

Step 5: Make Swap Permanent in /etc/fstab

Without an fstab entry the swap disappears at the next reboot. Back up the file first, add one line, then test it by turning swap off and back on with swapon -a, which reads fstab exactly as the boot process does.

cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' >> /etc/fstab

# Test the fstab entry without rebooting
swapoff /swapfile
swapon -a
swapon --show

Step 6: Tune swappiness for a VPS

vm.swappiness controls how eagerly the kernel moves memory pages to swap. The default of 60 is designed for desktops. On a server, a value of 10 keeps active data in RAM and only uses swap under real pressure. Lowering vfs_cache_pressure keeps file system metadata cached a little longer.

cat /proc/sys/vm/swappiness        # often 60 by default

# Apply now
sysctl vm.swappiness=10
sysctl vm.vfs_cache_pressure=50

# Keep after reboot
cat > /etc/sysctl.d/99-swap.conf <<'EOF'
vm.swappiness=10
vm.vfs_cache_pressure=50
EOF
sysctl --system

Step 7: When Swap Helps on 512 MB - 1 GB Plans (and When It Does Not)

Swap is the right tool for short peaks: running apt upgrade or dnf update, compiling software, Composer or npm installs, nightly backups, or a database that briefly needs more buffers. It is not a fix for a workload that permanently needs more memory than the plan provides. Check whether the system is actively swapping:

# Watch the si (swap in) and so (swap out) columns for 10 seconds
vmstat 1 10

Occasional non-zero values in si and so are fine. Constant high values mean the server is thrashing, and everything becomes slow. In that case reduce memory use (fewer PHP-FPM workers, smaller database buffers) or add RAM to the VPS; JUSTG cloud servers can be upgraded with extra RAM from the client area or through a ticket.

Step 8: Resize or Remove the Swap File

# Resize to 2 GB or remove swap
swapoff /swapfile
rm -f /swapfile
# To remove completely, also delete the fstab line:
sed -i '\|^/swapfile |d' /etc/fstab
# To resize, repeat Step 3 and Step 4 with the new size (fstab line stays)

FAQ

Will swap wear out the SSD?

With swappiness set to 10 and swap used only for peaks, the amount written is small. Heavy, continuous swapping is a sign you need more RAM, not more swap.

Is a swap file slower than a swap partition?

On modern kernels the difference is negligible. A swap file is easier to create, resize and remove on a VPS, which is why this guide uses one.

MySQL still gets killed after I added swap. What now?

Check journalctl -k | grep -i oom and free -h. If swap is full, lower innodb_buffer_pool_size and the number of web server workers, or upgrade the plan's RAM.

If you need help with swap or memory on your server, please submit a ticket at https://www.justg.com/submitticket.php and JUSTG technical support will assist you.

Was this answer helpful?