First of all, I want to ask you a very interesting question. Has anyone ever played microcontroller? Has anyone ever used an Excel spreadsheet?
I believe that many people have not played microcontroller, but Excel is to play the thief slip, is not it?
Have not played microcontroller, which have heard of microcontroller operating system? Can you run multiple programs at the same time?
Have you never eaten pork? Have you never seen a pig run? Microcontroller is no operating system, also can not run multiple programs at the same time!
Because the microcontroller is to write a good program directly burned into the device, once burned, can not be changed, and at the same time can only run a program, can not run more. This is because the programs in the blade operate directly on physical memory.
Can you imagine what happens if the same physical memory address is used by multiple programs? The image — it’s hard to remember. Skull pain!!
And we now use the Windows series, unix-like series of operating systems, ‘intelligent’ for us to design a set of memory management mechanism MMU.
As soon as each process runs, it is allocated a virtual memory VM space and a physical memory PM space in the memory. Information in virtual memory tells the process what memory data it wants to use and where it is in physical memory.
Each process has its own virtual memory, which records the location of physical memory. Therefore, the memory of each process is naturally isolated and does not appear in the memory, robbing other processes of resources.
For a process to run properly, its physical memory must contain at least: Stack, heap data, code, data, information, these are in the physical memory block storage, which is starting from a certain position, a space in a row, are the same type of data, so the virtual memory, to all the start bit and offset the data storage, in this way, the process to use one type of data, You can quickly find the location of this kind of data in memory and get the data.
I said a lot of things, and maybe a lot of people don’t understand, but here’s the point.
When you need some work information, you can open the Excel file. There are many worksheets in the file. When you need some data, you can click the name of the worksheet, open the corresponding table, and view the corresponding data.
You can think of the entire Excel file as the information that the process needs to run, and each sheet name is kind of virtual memory, telling us, where is the specific data that we need? The information in each worksheet is equivalent to the data stored in physical memory. Click the name of the worksheet to enter the worksheet and obtain the location of the data.
This example is easy to understand. Then, compare the above professional content to see, is it easy to understand?
Now virtual memory and physical memory, is there a clearer understanding.
However, this is not the best way for our operating system to manage memory, because the method mentioned above is prone to memory fragmentation and memory swap efficiency is not high.
When multiple processes are started consecutively, contiguous chunks of memory space will be allocated on the memory bar. However, once a process stops and the memory space is released, memory blocks will be discontiguous and memory fragmentation will occur.
In this case, if a new process is started and the requested memory space is larger than the remaining memory fragment size, an error message is displayed indicating that the requested memory space cannot be obtained. However, the actual total remaining memory space may be larger than the requested memory space.
So, for example, if you have a worksheet, and you start to write something, you start on the first line and you go down, straight lines, and you write a lot. As a result, you realize that some of the information is invalid, so no, you delete the entire row, but you don’t delete the row, and you end up with a bunch of empty lines in this worksheet. Now, you want to use blank lines, and you want to write data inside, but you want to write multiple lines of data, more than the fragmented blank lines.
What do you do?
Have you figured out how to cut the rows with data to the sticky board, and then paste from the blank line position, so that you can sort the blank lines together?
In fact, our computer memory management, also have a similar approach.
Virtual space on the disk, called swap partition, and then, the entire space in memory, cut to swap partition, and then restore to memory, so that the original memory space location, the free memory space organized together. Well, that’s easy to understand.
Easy to understand, but is this really a good idea?
Swap partition, is by the virtual disk, it is much slower than the speed of the memory speed, and a process, it is usually hundreds of megabytes of memory space, if a copy one or several processes of memory resources to swap partition, and then restore to memory, this time is very long, the user can clearly feel caton, even no response operation.
How is this supposed to work?
If it were you, would you wonder if you could not copy so many copies at once, so large, but smaller copies at a time, multiple copies?
Computers think the same way, so there is a memory page management. The entire virtual and physical memory space is sliced into a fixed-size page, which in Linux is 4K by default. If a process needs 256MB of memory, divide that space by 4K to get its number of pages, which are divided into this number of pages. Virtual memory, establish a page table, record page information, when needed to obtain physical memory, physical memory does not need to store all, can also be used, memory to other devices to obtain, because each time only a few K, a small amount of speed will be very fast.
So how do we understand this?
Still use Excel worksheets, for example, have saved a lot of data in a table, we can according to the grouping column values, the same as a set, so, you are looking for a data, is to see its value, and then find it a set of data, directly from a set of data found in the data that you want, isn’t it much faster.
But is this grouping easy to manage?
Maybe, it’s simple in imagination, but it’s complicated in engineering. So, page management is complicated. If you are interested, you can go and study on your own.
Ok, have you mastered the knowledge of virtual memory, physical memory and swap partition?