If you are a Linux user, then you have definitely heard about Linux Swap. But the reason you may be here is that either you may not know what exactly it is or how to use it. In this blog, I’ll be covering the concept of swap space and some of its practical implementations. So, let’s start.
What is Swap Space?
You probably know that Linux swap space is somehow related to RAM (Random Access Memory). Swap Space can also affect your System Performance too. How? We’ll answer both of the above-mentioned points.
If we talk about Storage Hierarchy, RAM lies at 3rd position from the top, after the Registers and Cache memory. In the hierarchy, the one at the topmost level will always provide the best IOPS (Input Output per Second), but its data storage capacity will be the lowest. In other words, IOPS and Storage Capacity is inversely proportional.
The Linux Kernel usually uses your RAM memory to store temporary information. And since RAM is on the 3rd level of the hierarchy, it has high IOPS but has low storage capacity compared to other storage mechanisms down the line in the storage hierarchy, which can sometimes leave us with very less or no RAM space availability, which further might lead in crashing the application or system. So what’s the solution?
In this scenario, when there is no enough RAM space is available, the Linux Kernel takes some of the information from RAM and writes it to the swap space on the hard drive. This process is known as Swapping Process. This way, your Linux system can release some RAM space and doesn’t crash due to lack of memory.
In brief, the swap memory extends the RAM as it provides some additional memory when the RAM space is exhausted.
Do I need Linux Swap?
As a beginner, you might ask this question to yourself whether there is a need for swap space or not. The answer to this question depends on your usage and the RAM your system has.
Nevertheless, Swap space is desirable even if you have a lot of RAM because otherwise there are high chances that whenever your system’s RAM gets full, it will crash the system. On the other hand, if you have the swap space, the kernel will write some information from RAM to your swap memory and your system will keep working, though, a little slower.
And as we know, nothing comes for free, so obviously, if you are relying more on swap space, it will make your system much slower than RAM.
There are two options available for having a swap space in Linux
- A Swap Partition
- A Swap File
- It is a part of your Hard Drive which is reserved for a swap space.
- Once configured, it is not easy to change.
- Created at the time of installing your Linux distribution.
- Alternate to swap partition
- Can be created after the installation.
- Swapping done to a file instead of a partition.
- You can modify the size of the swap file anytime, easily.
Swap Size Recommended to have
The swap size you should have on your system depends on the amount of RAM you have on your system.
|RAM Size||Swap Size|
|< 1 Gb||2 Gb|
|2-4 Gb||2-4 Gb|
|8 Gb||4 Gb|
|> 8 Gb||2-4 Gb|
You can refer to the above table, but again, everything depends on your use case.
How can I check my Swap Space Size
## Check swap size free -h ## Check swap partition size, swap type with mount point swapon
How to create a Swap file
NOTE: To understand what a command does in more details, try any of the below options: whatis <cmd> <cmd> --help ## Pre Allocate a space to a file sudo fallocate -l 1G /swapfile ## Change permissions of the file to user read & write. sudo chmod 600 /swapfile ## Setup a Linux swap area sudo mkswap /swapfile ## Enable the swap sudo swapon /swapfile NOTE: This won't persist the swap you have created. To make these changes permanent, run the following ## Edit "/etc/fstab" by adding the following at last /swapfile swap swap defaults 0 0 ## Verify the swap free -h
How to remove or edit a Swap file
### REMOVE the swap file ## Deactivate the swap file. "-v" is for verbose mode sudo swapoff -v /swapfile ## Delete the swap file sudo rm /swapfile ## Remove the entry in "/etc/fstab" ## Edit "/etc/fstab" by removing the following /swapfile swap swap defaults 0 0 ### EDIT SIZE of the swap file ## Run the REMOVE steps first ## Afterwards, run the CREATE steps with new size
After reading this blog, you will now be able to understand what Swap is in Linux and how to customize your swap size by modifying the swap file size. At last, I would like to add that it is safer to have some swap space on your computer. You can use either a swap partition or a swap file. Still, if you have any doubts/suggestions, you can contact me directly at email@example.com.
Also, I would like to thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs-ups and share this blog and provide suggestions on how can I improve my future posts to suit your needs. Follow me to get updates on different technologies.