Swap memory and how it affects your latencies.

When you work with Linux and memory you must have heard the term swap memory. In this article, we are going to see how swap memory affects the latencies of your system, and should you keep them or not?

What is a swap memory?

It is a memory space that is made available separately to the operating system to make use of. What exactly it does is make more memory available to the system in case of memory crunch. How it does is that it moves out some space from memory and puts that on swap memory which is on disk. And next time is it required it can read from swap memory and then put it as it is in memory.

This process happens when a page fault is triggered, the page fault system decides to move some data from memory to swap. Read more about page faults in the below article.

What effect does it have on latencies?

First of all, lets us understand what happens when there is no space on RAM. If you are exhausting RAM you can get OOM [out of memory] error and your system will crash. Swap memory saves you from that on the cost of latencies.

You have to note that swap space is on a disk which is a lot slower than RAM. And since you are swapping data in and out of the swap space, it will take some processing time to do this operation.

When you have high throughput systems it is very likely that the data may span across a wide range of pages in memory. So whenever a piece of data is required and the data is not present, a page from memory will be swapped out to swap memory.

Now this page swap will take some data which may be required in the next operation. So this way the number of swaps will increase dramatically with your high throughput.

So do not use swap in low latency systems.


Turn off the swap memory. But this will cause OOM?

Yes, but in current scenarios, a server crashing should not have more consequences than high latencies. Most of the infra nowadays is highly available. It again depends on your use case what you wanna use. But if you don’t want high latency to turn off the swap.

You should increase the RAM on the system rather than switching on the swap.

If you like the article please share and subscribe.

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.