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.
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.