Welcome to the Hindi Tutor QA. Create an account or login for asking a question and writing an answer.
Nilam in Web Development
Swap space is indeed a portion of the hard disk (or other storage devices) reserved for the operating system to temporarily store data that is no longer actively used in RAM (Random Access Memory).

1 Answer

+1 vote
Alok

How to Add Swap Space on Ubuntu 22.04

Swap space is indeed a portion of the hard disk (or other storage devices) reserved for the operating system to temporarily store data that is no longer actively used in RAM (Random Access Memory).

Swap space is a valuable component of a system's memory management strategy, allowing it to handle situations where RAM is fully utilized. However, it should not be heavily relied upon for regular system operation, as excessive swapping can lead to performance issues. Properly sizing and configuring swap space, along with monitoring system resources, is essential for maintaining system stability and performance.

This blog will demonstrate the method to add swap space on Ubuntu 22.04.

Adding swap space on Ubuntu 22.04

For the purpose of adding swap space on Ubuntu 22.04, follow the given instructions.

Step 1: Check Swap information

First of all, run the provided command to check in there exists any pre-configured swap on the system:

$ sudo swapon --show

Alternatively, utilize the “free” command with the “-h” option to verify the existence of active swap space:

$ free -h

The output of the given commands indicates that a swap file is already present on our system. However, we will create a new swap file for demonstration and show you the method of adding swap space in Ubuntu 22.04.

Step 2: Check available hard drive space

In the next step, check the available space of the hard drive to ensure that it has enough space:

$ df -h

As you see, we have “9.5G” available space on our system:

Step 3: Create Swap file

For the purpose of creating a Swap file, write out the provided “fallocate” command in your Ubuntu 22.04 terminal with the “-l” option and specify the amount of space you want to allocate. In our case, we will create a swap file named “swapfile1” having “1G” space:

$ sudo fallocate -l 1G /swapfile1

Step 4: Verify Swap file allocated space

Verify the allocated space to the swap file by utilizing the provided command:

$ ls -lh /swapfile1

Step 5: Change Swap file permission

Utilize the provided “chmod” command to change the permissions of file to be only accessible by “root”:

$ sudo chmod 600 /swapfile1

Then, verify the assigned file permissions:

$ ls -lh /swapfile1

Step 6: Mark swapfile1 as swap space

Type out the following command for marking the created “swapfile1” as swap space:

$ sudo mkswap /swapfile1

Step 7: Enable Swap file

Enable the created swap file and permit the Ubuntu 22.04 system for its usage:

$ sudo swapon /swapfile1

Step 8: Check swap file availability

After enabling swap file, verify if it is available or not by using the given “swapon” command:

$ sudo swapon --show

Or, execute the “free” command with the “-h” option for the same purpose:

$ free -h

Step 9: Make Swap file permanent

The changes you have made till now are only applied to the current session. This states that as soon as the system reboot, the server will not automatically continue with the added settings. To make the swap file permanent, add the created file to the “/etc/fstab” file.

So, firstly, backup the “/etc/fstab” file as a precautionary measure:

$ sudo cp /etc/fstab /etc/fstab.bak

Then, append the following information related to “swapfile1”:

$ echo '/swapfile1 none swap sw 0 0' | sudo tee -a /etc/fstab

Now move to the last step.

Step 10: Tune Swap space settings

At this point, Swap space is permanently and ready to use. However, we will tune some additional swap space settings that will impact the performance of your Ubuntu 22.04 in a good way.

Firstly, check the value of the “swappiness” parameter, which indicates how often the system is going to swap data out of the RAM to the added swap space. The parameter’s value will lie between “0” and “100”. In order to check the current value of the “swappiness” parameter, run the following “cat” command:

$ cat /proc/sys/vm/swappiness

Then utilize the “sysctl” command to assign a new value to the “swappiness” parameter:

$ sudo sysctl vm.swappiness=20

To permanently save the added settings for the added swap space, add the same line in the “sysctl.confl” file:

$ sudo nano /etc/sysctl.conf

Another parameter’s value that we are going to tune is “vfs_cache_pressure”. This parameter controls the cache dentry and inode information. Check its current value with the help of the provided “cat” command:

$ cat /proc/sys/vm/vfs_cache_pressure

Similarly, set a new value for the “vfs_cache_pressure” parameter using the “sysctl” command:

$ sudo sysctl vm.vfs_cache_pressure=40

Add the same line in the “sysctl.conf” file to permanently set the value of “vfs_cache_pressure” parameter:

$ sudo nano /etc/sysctl.conf

Hit “CTRL+O” to save the added line and switch back to the terminal. The created Swap space is all ready to use with the configured settings:

We have compiled the easiest method for adding swap space on Ubuntu 22.04.

Conclusion

In conclusion, swap space is a crucial component of a computer's memory management system. It provides a safety net for when physical RAM is fully utilized, allowing the operating system to temporarily store less frequently used data and processes on disk. Here are the key takeaways:

  1. Purpose: Swap space is used to prevent system crashes and maintain stability when RAM is exhausted by temporarily moving data from RAM to disk.
  2. Extension of Memory: It effectively extends the amount of available memory, albeit at a slower speed compared to physical RAM.
  3. Size: The size of swap space can vary but is often recommended to be equal to or larger than the amount of physical RAM, although specific requirements may vary.
  4. Use Cases: Swap space is used in low-memory situations, typically when RAM is fully occupied. The operating system decides what data to move to swap based on various factors.
  5. Performance Impact: While swap space is valuable, excessive swapping can lead to performance degradation because reading from and writing to disk is slower than accessing data from RAM.
  6. Monitoring: Regularly monitor system memory usage and swap space to ensure proper system operation. Frequent or excessive swapping may indicate the need for more RAM or application optimization.

In summary, swap space is a vital resource for maintaining system stability and preventing crashes due to memory shortages. However, it should be used judiciously, and efforts should be made to optimize your system's memory usage and avoid excessive swapping for optimal performance.

Related questions

...