Linux is an open-source Unix-like operating system based on the Linux kernel. The Linux operating system has recently evolved to run on a wide variety of hardware, from phones, tablets, and supercomputers. Every Linux-based OS is packaged with a Linux Kernel that manages hardware resources and software packages that make up the operating system.
How do I increase swap space in Linux? Have you been looking for a way to increase the swap space on your Linux PC? If so, don’t worry since this article will cover all the aspects you need to know about increasing swap space in Linux.
Generally, every processor in the machine requires a certain amount of data capacity to run and store loaded applications. Therefore, all PCs are designed to have both primary and secondary memories from where all the programs can be run and stored information is executed.
For IT or computer scientist guys, you really need to take the storage issue seriously because your machine runs several exhaustive software that may lead to your machine crushing if not well managed.
There could be many issues related to storage capacity in the Linux system when running applications, some of which include adequate RAM and space to prevent machine crushing.
The Swap space in the Linux system helps the machine run in situations where the RAM is used up. This mainly occurs when the system is working in multiple applications at once, filling up the RAM thus, leaving no space for any other functionality. This, therefore, forces the system to begin utilizing the secondary storage where the Swap space is located.
The swap space’s access time is usually less than the system’s physical memory. When applications running stop working because of insufficient RAM space, they force inactive pages to move to the Swap space. Therefore, this means that if you have insufficient swap space, your machine will end up hanging or crushing.
Users who, for instance, use Video editing tools or memory-consuming applications should consider increasing their swap space for their machines to support the workload.
Now, most people find themselves in a dilemma on how much swap space is required in specific RAM size. However, since the article has created a guided table to cover and save you from this dilemma, don’t beat yourself up.
Guided table:
System RAM | Recommended Swap Space |
Less than 2GB | 2 x RAM |
2GB-8GB | 1 x RAM |
8GB-64GB | 0.5xRAM |
More than 64GB | Depend on workload |
Below are the recommendations for CentOS and Red hat users:
System RAM | Recommended Swap Space |
Less than 2GB | 2xRAM |
2GB-8GB | 1xRAM |
8GB-64GB | Minimum of 4GB |
More than 64GB | Minimum of 4GB |
And for the Ubuntu Operating system, use the following recommendations that are little changed:
System RAM | Recommended Swap Space |
Less than 1GB | Swap>=RAM<2xRAM |
Greater | Swap>=square root of RAM but Swap<2xRAM |
How to increase swap space in Linux
Before increasing swap space in the Linux system, we first need to check if the swap is enabled. To attain this, copy and paste the following command in the terminal:
sudo swapon –show
Note: If you didn’t get any output, it means your system lacks the swap space at the moment.
Additionally, there is another way to check the memory and swap space information, using the “free -h” command.
free -h
In our case, the RAM is 7.2 GB, so the allocated swap space memory would be around 2GB, as mentioned in the recommendation table above.
Note: all the commands we are going to run require sudo privileges to make changes(increase swap space)
Step 1: copy and paste the following commands to increase the swap space:
sudo dd if=/dev/zero of=/swap_file bs=3GB count=1
Note: set the value of bs and count according to the requirement.
- bs- refers to the sets of blocksize
- count- refers to the number of blocks
After that, set the permission access for the users to 600 so users can’t be able to read essential data from the swap_file:
sudo chmod 600 /swap_file
To enable the swap area on the file “swap_file,” use the “mkswap” command utility:
sudo mkswap /swap_file
The next step is to enable the swap file “swap_file” using the following command:
sudo swapon /swap_file
After that, we will run the “free -h” command to check if the swap space has increased:
free –h
So, when checked before, the swap space was 2GB. And now it has been appended to 2.9GB.
Conclusion
Under normal circumstances, every processor needs an allocated memory to run packages and tools when using the system. The PC, at this point, uses the memory(RAM) allocated for the system. However, on other occasions, the RAM fails to be sufficient enough to support applications running simultaneously, and this is where the swap space comes in to supplement.
In a Linux system, swap space is located in the system as secondary memory so that when the RAM is exhausted, the swap space helps it run the applications.
In this article, we walked you through the general meaning of the swap space. After that, we went further to the recommendations on the size of the swap space with different RAM sizes. We then looked at the core part of the article, which was increasing the swap memory size. We believe that by now, you are comfortable with the whole process involving swap space increment; if so, please please share with us in the comment section below.