Are you wondering what a swap file or swap partition is? Do you wonder why your PC crashes when your RAM is full?
What is a Swap File?
A swap file is also known as a swap space, page file, or paging file. It is a space allocated to your operating system to take over processes of lesser priority or LRU applications from the RAM, leaving ample space for high-priority processes.
Why do I need a Swap File?
Have you ever wondered why your PC lags and slows down when processes are being run sometimes? Whilst there might be a lot of other factors that might be causing this, the most probable cause would be that your RAM is full. This is where swapping or swap files comes in handy. When we allocate some space on our HDD or SSD for swapping, it cushions the effect of RAM overloading and swaps out some processes from the RAM allowing higher-priority processes to run. Whilst there are a lot of technicalities largely decided by the operating system or kernel properties as to what processes should be swapped and at what time, it is most common to swap low-priority processes. Swap files or partitions are not compulsory, especially if you have a large RAM, but they are a desirable feature to have on your device.
How do I create a Swap file?
Whilst swap files can be used on both Windows and Linux OS, I will be focusing on the Linux operating system in this article. To create a swap file in the Linux OS we need to follow these steps:
Step 1: Check if you already have space or partition allocated on your HDD or SSD for swapping. How do you do this?
From the image above, on the Swap line, we can see that I have 0B (Zero bytes) allocated which indicates that I do not have a swap file or partition on my virtual box.
Step 2: Create the swap file. When creating a swap file we need superuser (root) privileges because we would be modifying low-level system resources. The command to create a swap is:
sudo fallocate -l 1G /swapfile
fallocate is used to deallocate, reallocate, or preallocate disk space for a file. In our case, we are preallocating disk space of 1Gigabyte to the file swapfile. The -l flag depicts the length or amount of disk space we want to allocate. How do we know how much disk space to allocate?
We can use the following guide:
Step 3: Set user permissions. To set user permissions we use the chmod command:
sudo chmod 600 /swapfile
chmod 600 gives the user read and write access.
Step 4: Format the file we created to swap using the mkswap command.
sudo mkswap /swapfile
Step 5: Enable or activate the swap file using the swapon command.
sudo swapon /swapfile
Step 6: Make disk space allocation permanent i.e. for the changes to persist despite system reboots. To effect this we must go into the /etc/fstab directory, using the command:
vi /etc/fstab
Next, we would do the following:
Step 7: Finally, to check the disk space allocated for swapping, we run the command “swapon” or “free -h”.
Cool right :), Explore the world of swapping.
How Do I delete my Swap Space?
We can do this with 2 simple steps:
Step 1: Deactivate the Swap Space.
sudo swapoff -v /swapfile
Step 2: Delete the Swap file.
sudo rm /swapfile
NOTE:
- Swap files will not be displayed by running the ls command. The swap file is not a regular file.
- It is desirable to have a swap file rather than a swap partition hardcoded into the HDD or SSD during the installation of the OS. This is because changes to previously allocated disk space can be easily done on swap files rather than a swap partition
- You could also read about Swappiness.