Understanding and Creating a Swapfile
Why Linux needs Swap memory to prevent freezing during multitasking, and how to safely create one on an NVMe SSD.
What is Swap Memory?
When you migrate from Windows to Linux, one system mechanic you need to understand is memory management. Windows automatically handles virtual memory (the pagefile), but Linux installs can end up without any swap — most commonly when you chose manual partitioning (“Something else”) and did not create a swap partition or file yourself.
Swap is a dedicated space on your drive (SSD/HDD) that acts as an overflow area for your physical RAM.
Why is Swap Important for Multitasking?
If you are heavily multitasking (e.g., leaving dozens of browser tabs open, running virtual machines, or editing heavy images), your physical RAM will eventually fill up.
- Without Swap: If your RAM hits 100%, the Linux Out-Of-Memory (OOM) killer kicks in. Your system will either freeze or force-close applications to save itself.
- With Swap: When your physical RAM approaches its limit, Linux moves idle data (like a background browser tab you haven’t clicked in an hour) from RAM onto the swapfile on your SSD. This frees up fast physical RAM for your active tasks, keeping the system responsive and preventing crashes.
Tip: On an NVMe SSD, swapping is fast. In the past, writing memory to a spinning hard drive was painfully slow. With a modern NVMe SSD, moving data in and out of a swapfile happens quickly enough that you will barely notice it.
How Much Swap Do You Need?
A common rule of thumb for modern systems:
- If you have 8GB RAM: A 4GB to 8GB swapfile is a sensible choice.
- If you have 16GB RAM: An 8GB swapfile works well for heavy multitasking.
- If you want Hibernation (Suspend-to-Disk): You need a swapfile slightly larger than your physical RAM (e.g., 20GB of swap for 16GB of RAM). However, hibernation is rarely used on modern SSDs since standard “Sleep” consumes almost no battery.
Step-by-Step: Creating an 8GB Swapfile
Before proceeding, ensure your Root (/) partition has enough free space.
Warning: Only execute these commands if you have confirmed you do not already have a swap partition or file set up. You can check this by typing
swapon --showin the terminal. If nothing outputs, you have no swap.
Open your terminal and execute the following commands one by one. You will be prompted for your system password.
1. Create the File
Allocate exactly 8 Gigabytes of space on your drive for the Swapfile:
sudo fallocate -l 8G /swapfile
2. Set the Correct Permissions
For security reasons, only the root system should be able to read this memory file:
sudo chmod 600 /swapfile
3. Format the File as Swap
Prepare the file to be recognized by Linux as memory:
sudo mkswap /swapfile
4. Activate the Swap
Turn the swap on immediately without needing to reboot:
sudo swapon /swapfile
5. Make it Permanent
By default, the swap turns off when you restart the computer. Add the swapfile to your system’s drive-mounting file (/etc/fstab) so it automatically activates on every boot:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
How to Verify It Worked
Run the following command to check your system’s memory allocation:
free -h
Look at the bottom row labeled Swap:. Instead of saying 0B, it should now display your newly allocated space (e.g., 8.0G). Your system now has an overflow buffer, so a full RAM no longer means an instant freeze or a killed application.
Discussion