VPS Memory Management & Swap Space Documentation

Essential guide for managing memory on low-RAM VPS instances

Overview

For VPS instances with limited RAM (especially < 2GB), swap space is essential for handling memory-intensive operations like application builds, compilations, and temporary workload spikes.

The Problem

Out of Memory (OOM) errors occur when:

  • Applications require more memory than physically available
  • Build processes (Node.js, Next.js, etc.) get terminated
  • System becomes unresponsive or crashes
  • Exit code 137 indicates process was killed due to OOM

Swap Space Solution

What is Swap?

  • Virtual memory extension using disk space
  • Acts as overflow when RAM is full
  • Prevents system crashes from memory exhaustion
  • Essential for low-RAM VPS environments

Current Memory Status Check

# Check current memory and swap
free -h
swapon --show

Creating Swap Space

Method 1: Using fallocate (Faster)

sudo swapoff -a
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Method 2: Using dd (More Compatible)

sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify Swap Installation

free -h
# Should show swap in the output