[IT Engineer Trivia] Do you really understand memory SWAP?
table of contents
Hello.
I'm Nomushin, the biggest hardware geek in the company (self-proclaimed).
Memory SWAP? Do you know the word? Modern computers are becoming more sophisticated and have plenty of memory, so you may not be aware of this when using a computer in general.
One of our services, server monitoring, includes SWAP monitoring, and if the amount of SWAP consumed increases, an alert will sound.
"SWAP is evil! SWAP should die!!"
I think there are some members who think so. …picture? Are you there? ?
However, surprisingly few people understand how SWAP works and why this mechanism is necessary.
So this time, he will discuss the logic behind SWAP and why it is necessary to monitor it. I will briefly explain what would happen if there was no SWAP.
Please note that this article does not discuss the specific settings of SWAP.
Structure of a computer (PC, server) * Super rough explanation
To give you a very rough explanation, there are three main parts that make up a computer:
・CPU
・Memory (DRAM)
[Main storage]・Storage (SSD, HDD, etc.) [Auxiliary storage]
As you know, the CPU is the brain of the computer, the part that controls calculations.
Memory is a place where the CPU temporarily stores data input to perform calculations, and storage is a place where data is stored permanently (DRAM loses data when the power is turned off. )
The important thing to note here is that
the CPU cannot directly access storage (auxiliary storage) Some of you may be thinking, "No, the program I wrote reads the file on the disk!!", but in this case, the operation is to first expand the file on the storage to memory, and then read the file on the disk. , the CPU processes it.
One more thing.
The major difference between memory and storage is speed.
When I replaced the HDD with an SSD for storage, I was surprised at how fast it worked! I think there are some people who have had this experience. (Nowadays, there are fewer computers equipped with HDDs...)
That should be it. SSDs are significantly faster than HDDs.
So how do you compare memory (DRAM) and his SSD?
DRAM is by far the fastest. The digits are different.
There are multiple standards for speed, such as access time and transfer speed, but DRAM is the fastest in all of them.
To be more specific, 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 let's leave that aside for now. Let's go.
About SWAP
Did you understand the general structure of a computer?
Now on to the main topic: SWAP.
Do you know what the word SWAP means?
When I asked Google, it said
"exchange" and "replacement" So, when SWAP occurs, it means that something is being replaced with something...? ? ?
First, what is the SWAP area?
Simply put, a ``virtual memory area'' created on storage .
Although it is a "virtual memory area," it is created on storage, so the speed of accessing data is equivalent to storage and "very slow compared to memory."
Also, this SWAP area is reserved for use when SWAP occurs, so files cannot be written to this area by other operations. Therefore, for example, if you reserve 1GB of SWAP space, the free space of the target storage will decrease by 1GB.
So when is this SWAP area used?
That's when you run out of memory .
If you are running multiple programs at once, or if a particular program consumes a large amount of memory, you may notice that the data required to run the program cannot be stored in memory. Overflowing! ! ' It's time.
As mentioned above , the CPU cannot directly access storage (auxiliary storage) , so all the data necessary to run a program must be stored in memory . If the memory overflows, the program will not work properly. An error will occur due to insufficient memory.
However, it will work if SWAP is enabled and there is sufficient SWAP space (large enough to store data that would overflow from memory).
This trick is
as follows: 1) In order for the CPU to obtain the necessary data, data that is infrequently used in the memory is written to the SWAP area (storage).
2) Data is written from the storage to the area that becomes free on the memory as a result of 1).
This is achieved by the process of
loading SWAP!! between memory and storage .
This allows the computer to perform processing as if the amount of memory had increased.
However, of course there are trade-offs.
As I've said many times before, storage is slow. If data is read and written frequently every time there is a shortage of memory, processing will naturally become slower. Disk I/O will also increase.
As a result , you end up in a situation
where you can move, but it's far from comfortable. But it's better than being stuck due to an error.
This is the reason why operations become slow when SWAP occurs.
Therefore, you should not ignore the occurrence of SWAP, but in the first place, he is a useful function that allows you to make something that would not work without SWAP into a working state.
Yes, was my mother's wisdom to make ends meet within a limited family budget ...!
*By the way, the above explanation is for Linux, but Windows also has something equivalent to SWAP called
"virtual memory." ↓This is it.
In the initial state, Windows should calculate the recommended value based on the installed memory size and allocate it automatically.
conclusion
The conclusion is as follows.
- When SWAP occurs frequently, system operation becomes very slow.
- However, if SWAP is not set, there are cases where the program cannot start due to insufficient memory.
- OOM Killer may occur if there is not enough memory for Linux to operate.
Based on the above,
- It is necessary to set an appropriate SWAP area size for the system memory consumption.
- Systems that constantly consume SWAP space should consider expanding the memory size in the first place.
- If SWAP consumption continues to expand without limit, there may be a problem with the program (memory leak, etc.) or incorrect settings, so an investigation is required.
I think you can say something like that.
Configure SWAP appropriately to build a stable system!
lastly
This is SEKARAKU Lab, a system development service site to which I belong.
SEKARAKU Lab: https://sekarakulab.beyondjapan.com
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
Thank you for reading to the end.
In recent years, hardware has become more sophisticated and software has introduced mechanisms to efficiently handle memory, so we may not be aware of SWAP anymore.
However, it remains an important function that is included in each OS even today.
This article only provides a rough explanation, but if you are interested, please dig deeper and find out more.
Click here for other SWAP articles
[Persian cat memorandum] I tried it with AWS free frame! Add SWAP area