[IT Engineer Tips] Do you really understand memory swapping?

table of contents
Hello.
I'm Nomushin, the biggest hardware enthusiast (self-proclaimed) in the company.
Are you familiar with the term "memory swap"? Today's computers are becoming more powerful and have ample memory, so you may not even think about it when using a regular computer
One of our company's server monitoring services involves SWAP monitoring, and an alert goes off when SWAP consumption increases
"SWAP is evil! SWAP must die!!"
I'm sure there are some members who think the same way. ...Huh? Aren't there any?
However, surprisingly few people understand how SWAP works and why this mechanism is necessary.
Therefore, this article will briefly explain the logic behind SWAP generation, why monitoring is necessary, and what would happen without it. Please note
that this article will not cover specific SWAP configuration details.
Structure of a computer (PC, server) * Very rough
To put it very simply, the three main components that make up a computer are:
• CPU
• Memory (DRAM)[Primary memory]
• Storage (SSD, HDD, etc.)[Secondary storage]
As you know, the CPU is the brain of the computer, the part that handles calculations.
Memory is where the data that the CPU inputs for calculations is temporarily stored, and storage is where data is permanently saved (DRAM data is lost when the power is turned off).
It's important to note here that"the CPU cannot directly access storage (secondary storage).
" You might be thinking, "But my program reads files from the disk!" However, in this case, the process involves first loading the file from storage into memory, and then the CPU processes it.
One more point.
The biggest difference between memory and storageis "speed.
Many people have probably experienced a surprising increase in performance after switching from an HDD to an SSD. (Although nowadays, computers with HDDs are in the minority…)
And that's no surprise. SSDs are overwhelmingly faster than HDDs.
But what about comparing memory (DRAM) to SSDs?
In this case, DRAM is overwhelmingly faster. The difference is orders of magnitude.
There are multiple criteria for speed, such as access time and transfer speed, but in all cases, DRAM is faster.
If we go into more detail, the CPU is equipped with multiple layers of cache memory (SRAM) that is orders of magnitude faster than DRAM, but this is outside the scope of this topic so we will leave it aside for now
Regarding the main topic, SWAP
Have you grasped the basic structure of a computer?
Now let's get to the main topic: SWAP.
Do you know the meaning of the word SWAP?
I asked Google and"exchange" or "replacement."it said
So, if a SWAP occurs, does that mean something is being exchanged for something else...?
First, what is a SWAP area?
Simply put,a "virtual memory area" created on the storage.
Although it's a "virtual memory area," because it's created on the storage, the speed of accessing data is equivalent to that of the storage itself, and "it's very slow compared to memory."
Also, because this SWAP area is reserved for use when a swap occurs, files cannot be written to this area by other operations. Therefore, for example, if you allocate 1 GB of SWAP area, the free space on the target storage will be reduced by 1 GB
So when is this SWAP area used?
It'swhen there's a shortage of memory.
For example, when multiple programs are running at once, or when a particular program consumes a large amount of memory,"the data necessary to run the program cannot be stored in memory...! It's overflowing!!"it's when
As mentioned earlier, the CPU cannot directly access storage (secondary storage), soall the data necessary to run a program must be temporarily stored in memory. If the memory overflows, the program will not function correctly; it will result in a memory shortage error.
However, it will work if SWAP is enabled and there is enough SWAP space (large enough to store data that overflows from memory)
This mechanism works as follows:
1) The CPU writes infrequently used data from memory to the SWAP area (storage) in order to obtain the data it needs
2) The CPU then reads data from storage into the memory area freed up as a result of step 1)
.
between memory and storage SWAP!!In essence, data is being
This allows the computer to operate as if it had more memory
However, there are trade-offs, of course.
As I've said many times, storage is slow. When data is frequently read and written every time memory runs out, processing naturally slows down. Disk I/O also increases.
As a result,"it runs, but it's not exactly comfortable..."you end up with a situation where
But it's better than it stopping due to an error.
The reason why performance slows down when a SWAP event occurs is due to the principle described above.
Therefore, you should not ignore SWAP events, but in the first place, SWAP is a convenient function that makes things that wouldn't work otherwise start working.
That's right,a mother's wisdom for cleverly managing a limited household budgetwas...!
*By the way, the above explanation uses Linux, but Windows also"virtual memory."has something equivalent to SWAP called
↓This is it.

In its initial state, Windows should automatically allocate a recommended amount based on the size of the installed memory.
conclusion
The conclusions are as follows:
- If SWAP occurs frequently, the system will become very slow
- However, if you do not set up SWAP, there are cases where the program cannot be started due to insufficient memory
- If there is not enough memory for Linux to run, the OOM Killer may occur
Taking the above into consideration,
- You need to set an appropriate size for the SWAP area based on the memory consumption of your system
- Systems that are constantly consuming swap space should consider expanding the memory size in the first place
- If SWAP consumption continues to grow indefinitely, it may be due to a program malfunction (such as a memory leak) or improper settings, so an investigation is required
These are some of the points we can make.
To build a stable system, let's configure SWAP appropriately!
lastly
This is SEKARAKU Lab, the system development service site I work for
SEKARAKU Lab:https://sekarakulab.beyondjapan.com
Beyond can handle everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us
Thank you for reading to the end.
In recent years, with the increased performance of hardware and the introduction of efficient memory management mechanisms in software, you may not need to be as conscious of SWAP.
However, it remains an important function included in all operating systems.
This article only provides a brief explanation, but if you're interested, please feel free to delve deeper and research it further.
for other articles about SWAP.Click here
[Persian Cat Memo] I tried using the AWS free tier! Adding swap space
58
