Linux Commands

What is Swap Memory in Linux?

For every operating system, there is a dedicated amount of RAM available that makes the processing of a program possible. However, the amount of this RAM is limited which is why RAM cannot hold a bulk of data in it. Therefore, there should be a backup option available which can support RAM whenever it runs out of memory.

This concept holds for the Windows operating system as well as for Linux. In Windows OS, whenever RAM has an insufficient amount of memory to hold a process, it borrows some amount of memory from the secondary storage. This borrowed memory is known as Virtual Memory. Similarly, whenever RAM runs out of memory in Linux, it borrows some memory from the secondary storage to store its inactive content.

In this way, the RAM finds sufficient space to hold a new process within it. Here, the borrowed space from the hard disk is called Swap Memory. In this article, we will try to learn the concept of swap memory in detail.

Working of Swap Memory:

As explained above, swap memory is the dedicated amount of hard drive that is used whenever RAM runs out of memory. There is a memory management program in Linux that takes care of this process. Whenever RAM is short of memory, the memory management program looks for all those inactive blocks of data present in RAM that have not been used for a long time.

When it successfully finds those blocks, it shifts them into the swap memory. In this way, the space of RAM is freed up and hence it can be utilized for some other programs that need processing on an urgent basis. The concept of swapping is very much similar to the concept of paging used in the Windows operating system.

Types of Swap Memory:

Typically there are two different types of swap memory which are mentioned below:

  • Swap Partition- This is the default type of swap memory which is in fact, a hard drive partition that is dedicated to swapping.
  • Swap File- This is a self-created type of swap memory. Whenever there is no sufficient amount of space left in the hard drive to create a swap partition, a swap file is manually created for swapping the inactive contents of RAM into it.

What should be the Ideal Frequency of Swapping?

Linux allows us to set the frequency of swapping on our own i.e. how frequently the process of swapping should take place. You can set the value of swapping between 0 and 100 depending upon your requirements. A low-frequency value of swapping means that the process of swapping will take place very rarely only when it is needed whereas a high-frequency value of swapping means that the swapping process will occur quite often. However, the default and recommended value of swapping frequency is 60.

Benefits of using Swap Memory:

By learning the working of swap memory, we can easily perceive the benefits of using it. However, some of the main benefits of using swap memory are listed below:

  • It can easily hold those inactive blocks of RAM that are hardly used once or twice and then they are never used. The freed up RAM can then be used to hold more programs that have a higher priority.
  • It prevents RAM from running out of space.
  • It acts as a backup for enhancing the actual space of RAM.
  • It allows you to run heavy applications more conveniently that require a large amount of RAM.
  • During the process of hibernation, all the contents of RAM are written on the swap memory. Therefore, it is essentially required for the hibernation process to take place successfully.
  • It enhances the overall performance of your system.

Conclusion:

In this article, we have learned the usage and working swap memory along with its numerous benefits. Swap memory acts as a backup option for RAM when it runs short of space. We all know that we cannot have an infinite amount of RAM however; we do realize that today’s high-end applications require a large amount of RAM to operate smoothly. Therefore, we must have a sufficient amount of RAM to avoid our applications from crashing.

Also, there is a cost associated with adding in more RAM whereas there is no cost of using swap memory. Moreover, additional RAM can also be plugged in up to a certain limit depending upon your hardware. Hence, the only option we are left with is to use swap memory which can make our system work very efficiently without any cost.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.