Swap usage in Linux is basically done in order to relocate the data from a page (physical RAM being divided into chucks) of memory to the free space on hard disk (swap space) in order to free that page. The reasons are obvious. There gets an urgent requirement to free the storage area because the bulk of data keeps on flowing and need to be stored somewhere. Moreover, a large part of pages likely being used only during initialization but never again. This unnecessarily wastes the memory.

What are swapping and thrashing?

The copies of pages are created between the physical memory and a swap device in order to make use of the swap devices. This is known as swapping process. Thrashing happens when the swapping process uses more resources than available even if the other processes on the system are there.

Types of swap space

There are two types of swap space in Linux – Swap partition and Swap file. The swap partition is that area where the data is temporarily saved so as to continue for the further procedure. It does not save anything for permanent but acts just as an intermediate in between. The swap file resides amongst the system and data files. The swap file can also be intentionally removed if it serves no purpose anymore.

swap usage in linux

Why do we need swap space?

It is required when there is a need of better processing speed and the RAM is not enough to handle that. Although, there comes a point when even swapping does not work properly. Here is when one would require to enhance the RAM and everything would be back to normal. The method is no longer used once the system returns back to its normal functioning.

Is it possible to run a system without swap?

As far as the system works fine the need of swapping is not felt. It is possible that the behavior of the server is just as normal despite the absence of swap space and its potential requirement. A bit of swapping does not hurt the system, but it should be calculative.

One should also keep in mind that the server may also deal with OOM (Out Of Memory) way too soon than expected if the requests of the memory requirement rises. There comes a point when even the minutest task, such as login into the database becomes troublesome. Once the server reaches that stage, it kills the processes to make room for the new applications/requests. One may manage this scenario by prioritizing which process could be killed first and so on.

If two or more swap spaces are given equal priority, despite being at different locations, Linux is going to interleave the swapping among them which in turn positively affects the overall performance. Moreover, if a swap file isn’t available then a little area (say 20%) from the existing RAM could be reserved for swapping process.

Freeing memory space has lots of advantages. If the memory gets free, it could be utilized for other application or even for caching. But it also has few disadvantages. The major disadvantage is the difference in the speeds. The memory speeds are measured in nanoseconds, while the disk speeds cannot go beyond milliseconds. The Random Access Memory runs at 100 ns, while the SSD runs at 10 ms. Clearly the disk speeds are tens of thousands of times slower compared to the physical memory.

Therefore, if the swapping process is frequent it would affect the overall speed of the server. This is how it goes – the page is swapped out then in, and then repeat several times consecutively. So if the new application(s) show(s) up on a continuous basis, there is no availability of the free memory. This results into a struggling system that cannot find free memory anymore along with the running applications. In these kinds of cases also only additional RAMs can rescue.

References:-

https://www.linux.com/news/all-about-linux-swap-space

https://www.redhat.com/en/blog/do-we-really-need-swap-modern-systems

(Visited 18 times, 1 visits today)