Hello, I’m Liang Xu.
Familiar with the computer partners know that we often say that the three main components of the host are the motherboard, CPU, memory, visible in the role of the computer host is extraordinary, like DIY host partners on the understanding of the three major components is better.
The execution process of the program is familiar to those who have written the program. It is loaded from the hard disk to the memory, and then the CPU and memory exchange and process the data to complete the corresponding operation (the real execution details are quite complex), so as to achieve the corresponding function.
But the capacity of memory is very limited, due to the high cost of memory (currently the commonly used memory is double speed SDRAM), computer users to upgrade the memory capacity is also a small expense.
So let’s discuss the memory of the computer if it is not enough, the application of virtual memory technology, the emergence of virtual memory, let the machine memory is not enough to be partially solved, we need to pay attention to is that some information will also be called virtual memory swap memory.
1. Warm up before discussing virtual memory
What we need to know is that computers divide memory into physical memory and virtual memory (note the difference between virtual memory and virtual address space). Physical memory is the actual memory size of a computer, made up of RAM chips. Virtual memory is virtualized, using disk instead of memory.
How virtual memory works
As described earlier, when the system runs out of physical memory, a portion of the physical memory needs to be freed up for use by currently running programs.
The freed space may come from programs that have not done anything for a long time. The freed space is temporarily stored in swap space, and when the programs are ready to run, the saved data is recovered from the swap space into memory.
3. Implementation of Linux virtual memory
There are two ways to implement virtual memory in Linux: swap partition and swap file.
##### Switch Partition
Swap partition is to create a partition and mount the partition as swap space. The procedure is the same as that of traditional partition creation. However, mkswap and swapon commands are used to format and mount partitions respectively.
Swap file
There is not enough space on the hard drive to create a swap partition, so we manually create a swap file to swap the inactive contents of RAM into it.
4. Ideal exchange weights
Linux allows us to set the weight of the swap, which means we can set how often the swap occurs. Friends can set the weight of the exchange between 0 and 100 as needed. The smaller the value, the less the kernel uses swap, and the larger the value, the more the kernel uses swap space.
The default value and recommended value of swap weight are 60. If you are interested, you can learn more about swappiness, which is a Linux kernel parameter that is not described too much here.
5. Benefits of using virtual memory
- It can easily store occupied chunks of RAM that are barely used. The freed RAM can then be used to accommodate more higher-priority programs.
- Prevent situations where RAM space runs out and other programs cannot be executed.
- Virtual memory acts as a backup of RAM, increasing the actual RAM space.
- During hibernation, all contents of RAM are written to swap space. Therefore, virtual memory is required for successful hibernation.
- It improves the overall performance of the system. (If the kernel parameter swappiness is set properly)
Conclusion 6.
In this article, we learned about swap memory and its many benefits, which are used as an extension of RAM when RAM space is low, preventing our application from crashing due to lack of memory.
In fact, Swap adjustments are critical to the performance of Linux servers, especially Web servers. By adjusting Swap, you can sometimes overcome system performance bottlenecks and save system upgrade costs.