Lesson 10 of 19 ↓
- 1.Fedora + Distrobox + Arch + AUR Setup Guide
- 2.Enabling Hibernate on Linux
- 3.A Practical Git Workflow
- 4.Useful Linux Commands & Tips
- 5.Mirroring Your Android Screen with Scrcpy
- 6.Downloading Media with Yt-dlp
- 7.Consistent C/c++ Style with Clang-format
- 8.Auto-mounting Drives at Boot
- 9.Customizing Grub
- 10.Linux Swap Files Explained
- 11.Zram: Compressed Swap in Ram
- 12.Custom App Launch Behavior Via .desktop Files
- 13.Custom Mime Icons in Gnome
- 14.Fixing Missing Dock Icons (startupwmclass)
- 15.Installing Questasim for Hdl Simulation
- 16.Building Python 2 From Source
- 17.Auto-Starting WhatsApp and Telegram (Minimized or Tray)
- 18.Custom Neovim Configuration Guide
- 19.Running Windows Applications on Linux Seamlessly with WinBoat (Docker)
1 min read
Linux Swap Files Explained
Swap space is what the kernel falls back to when RAM fills up: inactive memory pages get moved out to disk instead of the system triggering an out-of-memory kill. It's slower than RAM, but it's the difference between a heavy workload merely slowing down versus crashing outright. This walks through creating, securing, and enabling a 16GB swap file.
Disable Any Existing Swap
Start from a clean slate, especially if replacing or resizing an existing swap file:
sudo swapoff -a-a disables every swap device listed in /proc/swaps.
Allocate the Swap File
sudo fallocate -l 16G /swapfilefallocate pre-allocates the space directly — much faster than the older dd if=/dev/zero approach.
Secure It
Swap can contain data moved straight out of RAM, including things like passwords held in memory — so the file must only be readable by root:
sudo chmod 600 /swapfile600 means read/write for the owner (root) only; no permissions for group or others.
Format it as Swap
sudo mkswap /swapfileThis sets up the file as a Linux swap area so the kernel recognizes it as valid swap storage.
Enable it for the Current Session
sudo swapon /swapfileMake it Persistent Across Reboots
The steps above only take effect until the next reboot. To have it load automatically at boot, add it to /etc/fstab:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabVerify
sudo swapon --show # should list /swapfile with SIZE 16G
free -h # check the Swap: row for total/usedRemoving it Later
sudo swapoff -v /swapfile
sudo nano /etc/fstab # delete the /swapfile line
sudo rm /swapfile
Comments
Loading the discussion…