Swap File for Old Laptop
Swap space is a reserved area on disk used when physical RAM is full. When a Linux system runs out of RAM, inactive pages are moved from RAM to the swap space — keeping the system running instead of crashing.
This is important for my old laptop, which only has 4 GB of RAM and starts to crawl under load. Adding a larger swap file gives it some breathing room.
Quick Steps
- Turn off swap processes
- Resize the swap file
- Make the file usable as swap
- Activate the swap file
- Verify the swap size
Step-by-Step
Step 1 — Turn off swap
sudo swapoff -a
Step 2 — Create a new swap file (8 GB = double the RAM)
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
bs=1M count=8192creates an 8 GiB file. Adjustcountfor a different size (e.g.count=4096for 4 GiB).
Step 3 — Set correct permissions
sudo chmod 0600 /swapfile
Step 4 — Format it as swap
sudo mkswap /swapfile
Step 5 — Activate it
sudo swapon /swapfile
Step 6 — Verify
grep Swap /proc/meminfo
Or check the file size directly:
ls -lh /swapfile
Make It Permanent (survive reboots)
Add the swap file to /etc/fstab so it activates automatically on boot:
# /etc/fstab — add this line at the end
/swapfile swap swap sw 0 0
Alternative: Swap on a Separate Partition or Path
If you want to store the swap file on a different drive (e.g. a fast HDD), change the path accordingly:
# Create the file on a specific drive
sudo dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M
# Format it
sudo mkswap /media/fasthdd/swapfile.img
# Activate it
sudo swapon /media/fasthdd/swapfile.img
And in /etc/fstab:
/media/fasthdd/swapfile.img swap swap sw 0 0
Confirm Everything is Working
cat /proc/swaps
Expected output:
Filename Type Size Used Priority
/swapfile file 8388604 2724 -1
grep 'Swap' /proc/meminfo
Expected output:
SwapCached: 4772 kB
SwapTotal: 8388604 kB
SwapFree: 8355812 kB
If SwapTotal shows your new size, you’re done. ✓