Have you ever heard of the term SWAP? Today's computers have higher performance and more memory, so you may not be aware of the term when you use your computer in a general way. However, in our job as MSP, SWAP is something we need to be aware of its role, function and why it needs to be monitored.
In this article, I will briefly explain the logic behind SWAP, why it is necessary to monitor, and what would happen if there were no SWAP.
Structure Of Computers In The Nutshell
To give you a very rough idea, there are three main parts that configure a computer.
Data Storage (SSD, HDD etc.)
The CPU, as you know, is the brain of the computer, the part that controls the calculations.
Memory is a place to temporarily store the data that the CPU inputs to perform calculations, while storage is a place to permanently store data.
It is important to note that the CPU does not have direct access to storage. What is going on when your program reads files on the storage is that the files are extracted to memory once and then processed by the CPU.
One more thing that needs to be mentioned is that the main difference between memory and storage is speed.
Some of you may have had the experience of being amazed at how much faster your PC runs by switching from HDD to SSD for storage (although, to be fair, nowadays there are only a few computers with HDDs...). That's because SSDs are much faster than HDDs.
Then how about memory (DRAM) compares to SSDs?
The answer is DRAM. It is much faster than SSDs. There are several standards for speed, such as access time and transfer rate, but DRAM is faster in all of them.
About SWAP Memory
SWAP memory (SWAP area) is, simply put, a "virtual memory area" created on the storage. Even though it is called a virtual memory area, the speed of accessing the data is much slower than a memory because it is still created on top of the storage.
Also, since this SWAP area is reserved, files cannot be written to this area by other operations. For example, if you reserve 1GB of SWAP, the free space of the target storage will be reduced by 1GB.
And this SWAP memory is used when memory is insufficient. For example, when multiple programs are executed at once, or when a particular program consumes a large amount of memory, the memory cannot store enough data to run the program. The SWAP memory starts being used in such cases.
As mentioned before, the CPU cannot access storage directly, so all the data needed to run the program must be held in memory. If the memory is overflowed, the program will not run properly and an error will occur due to insufficient memory. However, it will work if SWAP is enabled and large enough to store the data that would otherwise overflow the memory.
This is accomplished by the following process.
1. The CPU writes infrequently used data in memory to the SWAP area (storage) in order to obtain the necessary data in memory.
2. The data is read from the storage into the memory area left vacant by the above process.
So, as the name implied, data is swapped between memory and storage. This allows the computer to perform operations as if it had more memory.
However, there is a tradeoff.
As mentioned earlier, storage has a slow processing speed, so if data is frequently read or written whenever memory is in short supply, processing will naturally become slower and disk I/O will rise.
By the way, the above explanation is based on Linux, but the equivalent of SWAP also exists in Windows under the name of "virtual memory". Initially, Windows calculates the recommended size of the virtual memory based on the size of the installed memory and allocates it on its own.
To summarize, the facts about memory are below.
If you use a lot of SWAP memory, the system becomes very slow.
However, if SWAP is not set, there are cases where programs cannot be started due to insufficient memory.
If there is not enough memory for Linux to run, OOM Killer may occur.
And based on the above facts, you should consider below.
The size of the swap memory should be set appropriately for memory consumption.
A system that is constantly consuming swap memory should consider expanding the memory size.
If the swap consumption expands inexhaustibly, it may be due to a program failure (e.g. memory leak or other inadequate settings) and needs to be investigated.
So setting up SWAP memory is the key to properly building a stable system!
In recent years, as hardware has become more powerful and software has introduced mechanisms to handle the memory more efficiently, we may be less aware of SWAP.
However, even today, SWAP is still an important feature in every OS.
This article only gives a rough explanation, but if you are curious about SWAP, I encourage you to dig deeper and look into it.
This blog post is translated from a blog post written by Yuuki Nomura on our Japanese website Beyond Co..