[IT Engineer Trivia] Do you really understand memory SWAP?

What's SWAP?

Hello.
I'm Nomusin, the self-proclaimed biggest hardware geek 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.
So, this time I will roughly explain what logic causes SWAP, why monitoring is necessary, and what would happen if SWAP did not exist. Please note
that this article does not cover specific SWAP settings.

Structure of a computer (PC, server) * Very rough

To put it very simply, there are three main parts that make up a computer:
CPU
, memory (DRAM)
[main memory device] , storage (SSD, HDD, etc.) [auxiliary memory device].

As you know, the CPU is the brain of the computer, the part that controls calculations.
Memory is where the data that the CPU inputs for calculations is temporarily stored, and storage is where data is permanently saved (DRAM loses its stored data when the power is turned off).

It's important to note here that the CPU cannot directly access storage (auxiliary storage devices) .
Some of you may be thinking, "Well, the program I wrote reads files from disk!", but in this case, the file on storage is first loaded into memory, and then the CPU processes it.

Another
big difference between memory and storage is "speed.
I'm sure there are people who have experienced being surprised at how much faster their computer became after switching from HDD to SSD. (These days, computers with HDDs are becoming rarer...)

That's no surprise. SSDs are overwhelmingly faster than HDDs.
But what about memory (DRAM) compared to SSDs?
DRAM is overwhelmingly faster. It's an order of magnitude faster.
There are multiple standards for speed, such as access time and transfer speed, but DRAM is faster in all cases.

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 understood the general structure of a computer?
Now, let's get to the main topic: SWAP.

Do you know the meaning of the word SWAP?
When I asked Google, it came up with
"exchange" or "replacement." So, when 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 storage .
Although it is a "virtual memory area," because it is created on storage, the speed at which data is accessed is the same as storage, but "very slower than 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 when there is a memory shortage .
occurs when multiple programs are running at once, or when a particular program consumes a large amount of memory, and the memory cannot store all the data necessary to run the programs...it overflows!

As mentioned above , the CPU cannot directly access storage (auxiliary storage) , so all data required for a program to run must be stored in memory . If memory overflows, the program will not run properly. An error will occur due to insufficient memory.

However, it will work if SWAP is enabled and there is enough SWAP space (large enough to store data that overflows from memory)

The trick is that
1) in order for the CPU to obtain the data it needs, it first writes the infrequently used data in memory to the SWAP area (storage), and

is read
from storage into the freed up memory area as a result of 1) is exchanged SWAP!! between memory and storage

This allows the computer to operate as if it had more memory

However, there is of course a trade-off.
As I've said many times before, storage is slow. And if you have to read and write data frequently every time memory runs out, processing will naturally slow down. Disk I/O will also increase.
As a result, you'll end up in a situation where
"it works, but it's hard to say it's comfortable..." But it's better than having it stop due to an error.

This is the reason why operation slows down when a swap occurs.
Therefore, you should not ignore the occurrence of a swap, but it is a useful function that makes things that would not work without a swap possible to work.

Yes, was my mother's way of managing our limited household budget ...!

*By the way, the above explanation is for Linux, but Windows also has something equivalent to SWAP called
"virtual memory." ↓This is it.

By default, Windows calculates the recommended value based on the installed memory size and automatically allocates it.

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


To build a stable system, make sure to set up 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 hardware becoming more powerful and software introducing mechanisms for efficiently handling memory, SWAP may no longer be something we need to be aware of.
However, it remains an important function that is included in every operating system today.
This article only provides a rough explanation, but if you're interested, please dig deeper and look into it.

※Other articles about SWAP can be found here

[Persian cat memorandum] I tried it with AWS free frame! Add SWAP area

If you found this article helpful , please give it a like!
57
Loading...
57 votes, average: 1.00 / 157
100,984
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Nomura

After graduating from university, he worked on electronic medical record development at a local system development company.
After that, he moved to a mobile app development company and gained experience developing apps for flip phones and smartphones, before joining Beyond in 2013.
He works in the Osaka office. He is currently in charge of designing and developing web systems.
He continues to develop smartphone apps in his private life as a hobby.
He is a mechanic who is a hardware geek. Leave the assembly of computers and bicycles to us!