
How to Add Swap Space on Ubuntu (5GB Example)
Article from blog Description A step-by-step guide to creating a swap file on Ubuntu, making it persistent across reboots, and tuning swappiness for better performance. Why Swap Matters Swap is disk space that your OS uses as overflow when physical RAM fills up. Without it, the kernel may start killing processes (the dreaded OOM killer) when memory is exhausted. With it, your system degrades gracefully instead of crashing. My setup: 7.57 GB RAM, 0 KB swap. Adding 5 GB swap gives the system room to breathe under heavy loads. Step 1: Create the Swap File sudo fallocate -l 5G /swapfile fallocate instantly allocates the space on disk without writing zeroes — fast and efficient. If for some reason fallocate isn't available, use dd as a fallback: sudo dd if = /dev/zero of = /swapfile bs = 1M count = 5120 Step 2: Lock Down Permissions sudo chmod 600 /swapfile This ensures only root can read/write the swap file — important for security. A world-readable swap file is a potential data leak. Step
Continue reading on Dev.to
Opens in a new tab



