To understand what Swap memory in Linux is, we will have a look at the concept of RAM and how the Linux system reacts when RAM reaches its maximum capacity.
With the topic of memory, it is fairly common to come across mentions of RAM and swap space. In this tutorial, we discuss RAM and swap memory to understand why swap memory in Linux is so essential.
Table of Contents
Setting the Context…
Sometimes, you can experience your system getting sluggish while you are working. This usually happens due to the physical memory, or RAM being used at its maximum capacity.
This is something we want to avoid, as unavailability of RAM space can cause high I/O wait times and reduced responsiveness for the system. To counter it, the system utilizes swap space. It helps ensure that the system does not exhaust the physical memory of your system.
How is the RAM used?
RAM or the Random Access Memory is the most important part of any digital device. This piece of hardware is where all the data being currently used a processor is stored.
This memory location can be read much faster by the processor compared to other locations such as an HDD or SSD.
Due to its volatile nature, the RAM only stores the data which is currently being used. As soon as a system is shut down, it loses all the memory which was stored on it. Hence, we use storage devices such as an HDD, SSD or optical drive to permanently store any required data.
But these storage devices also help us in other instances. One such instance is when the RAM is nearing full capacity and cannot hold all the required data at the same time.
This brings us to the concept of swap memory in Linux.
Understanding swap memory in Linux
Swap memory in Linux is used to prevent data overflow in RAM in event of the system facing high load. This helps prevent the system to become unresponsive due to a lack of memory. It also ensures that applications or processes don’t encounter ‘out-of-memory’ errors.
How does swap memory work?
In terms of memory, a page is a fixed block of memory space. The data blocks occupying the RAM do so in the form of these pages.
In the event of the RAM storage nearing its limit, the OS needs to prioritize processes that get to utilize this memory. Hence, the OS selects the old data or processes which may be dormant to free up the RAM.
This data is then written in a pre-defined section of the ROM storage device on the system. This section is known as the ‘swap space’.
This function of the swap memory in Linux allows the OS to maintain sufficient free RAM space at all times. Later, these pages are ‘swapped’ with the ones stored in the RAM as and when needed.
Why is swap memory important?
RAM space is a precious resource. It is required for all processes. Swap memory in Linux allows the OS to balance available resources. This, in turn, results in the efficient working of your system.
The data stored in the swap memory in Linux takes longer to access than that stored in RAM. However, this does not cause any issues.
All the data written in the swap memory is loaded back into the RAM as soon as it is free, or the data is required for a current process. This makes swap memory in Linux a reliable fail-safe for preventing any out-of-memory exception for the running processes on our system.
Note: It is an essential detail to keep in mind that swap memory is only recommended for systems utilizing mechanical hard disk drives.
In the case of systems using Solid State Drives, creating swap memory partitions has been observed to cause hardware degradation in the long run. This lowers the life of your SSD and may risk memory failure.
How to check your system’s swap memory allocation?
For most systems, it is common to come across a term known as ‘virtual memory’ in the memory properties. Virtual memory is the space on the system which is equal to the sum of the RAM and swap memory in Linux.
Almost all modern Linux systems declare a swap partition by default to prevent memory overflow situations.
Hence, it may be essential for a Linux user to check the virtual and swap memory allocation on their system. This can help in deciding whether you wish to increase or reduce the swap partition as per your requirements.
To check the total virtual memory allocation and utilisation on your system, type the following command in your terminal.
As shown in the screenshot above, this shows you the total available RAM and swap memory.
It also shows you the amount of memory being utilized at the moment and the amount of free memory available to the system.
Side note: I have explicitly declared a 0% swap for my Linux server since it’s a test environment and has no need for the swap space.
If you simply wish to find the size of the swap partition on your system, you can use the command line to display the contents of the swap configuration file stored at /proc/swaps using the cat command:
As mentioned before, I do not have swap space configured on my system but you can run this command to view the swap space configured for your system.
In any system, few resources are even close to the importance of RAM space. With the help of swap memory, systems can make the best out of available resources without compromising a lot on performance.
This mechanism silently works in the background to ensure a smooth experience for the users even in the event of high load sessions.
We hope that, through this tutorial, you were able to develop a good understanding of RAM, along with the concept of swap memory in Linux.