Talking about virtual memory

Time:2021-3-1

Hello, I’m Liang Xu.

Familiar with the computer’s small partners know, we often say the host three pieces are the motherboard, CPU, memory, visible memory in the role of the computer host is not the same, like DIY host of small partners to understand these three pieces is better.

Small partners who have written programs are familiar with the execution process of the program. They first load the program from the hard disk to the memory, then exchange and process the data between the CPU and the memory, and complete the corresponding operations (the real execution details are quite complex), so as to realize the corresponding functions.

But the memory capacity is very limited, because of the high cost of memory (the commonly used memory is double speed SDRAM), it is also a big expense for computer users to upgrade the memory capacity.

Then Liang Xu will discuss with you that if the computer’s memory is not enough, the application of virtual memory technology and the emergence of virtual memory will partially solve the problem of insufficient machine memory. What you need to pay attention to is that some materials also call virtual memory swap memory.

1. Discuss the warm-up before virtual memory

What we need to know is that computer memory is divided into physical memory and virtual memory (pay attention to the difference between virtual memory and virtual address space). Physical memory is the actual memory size of the computer, which is composed of RAM chip. Virtual memory is virtual, using disk instead of memory.

2. Working principle of virtual memory

As described earlier, when the physical memory of the system is not enough, a part of the physical memory needs to be released for the current running program.

The released space may come from some programs that have no operation for a long time. The released space is temporarily saved in the swap space. When the programs are to run, the saved data will be restored from the swap space to the memory.

3. Implementation of Linux virtual memory

There are two ways to realize virtual memory in Linux system, one is to exchange partitionswapPartition) and exchange files.

Switch partition

Swap partition is actually to create a new partition, and then mount the partition as swap space. The method and steps are the same as the traditional new partition. Only format partition and mount partition are used separatelymkswapandswaponOrders.

Exchange of documents

If there is not enough space in the hard drive to create a swap partition, we will manually create a swap file to swap the inactive contents of ram into it.

4. Ideal exchange weight

Linux allows us to set the weight of the exchange, that is, we can set the frequency of the exchange process. Partners can set the weight of exchange between 0 and 100 according to their needs. The smaller the value is, the less exchange space will be used by the kernel. The larger the value is, the more exchange space will be used by the kernel.

The default value and recommended value of exchange weight is 60. Interested partners can learn from itswappinessThis Linux kernel parameter is not described here.

5. Benefits of using virtual memory

  • It can easily save ram blocks that are almost unused and occupied. The released RAM can then be used to hold more programs with higher priority.
  • Prevent the situation that RAM space is used up and other programs cannot be executed.
  • Virtual memory acts as the backup of ram, which increases the real space of ram.
  • During sleep, all the contents of RAM are written into the swap space. Therefore, virtual memory is necessary for successful hibernation.
  • It improves the overall performance of the system. (in the kernel parameterswappiness(if the setting is reasonable)

6. Conclusion

In this article, we learned about swap memory and its many advantages. When the RAM space is insufficient, it will be used as an expansion of ram to avoid our application crashing due to insufficient memory.

In fact, the adjustment of * * swap * * is crucial to the performance of Linux servers, especially web servers. By adjustingSwapSometimes, it can overcome the bottleneck of system performance and save the cost of system upgrade.


Official account: good Linux

Talking about virtual memory

What’s the gain? I hope the old fellow will take three strikes to show this article to more people.