I have consulted other people's documents and sorted out basic knowledge such as virtual memory, physical memory, swap partition, and page replacement mechanism.
- Relationship between virtual address space and physical address space
Virtual addresses are maintained by the operating system and can be converted by MMU, which expands the paging Management of memory space. Most systems that use virtual memory use a paging mechanism. Virtual Address Space is divided
Page(Page), and the corresponding physical address space is also divided, the unit is page frame, one on disk, one in memory, the size of the page and page must be the same. On a 32-bit machine, its virtual address ranges from 0 ~ 0 xffffffff (4G), and this machine only has a physical address of M, so it can run 4G programs, but this program cannot be transferred to the memory at a time to run. This machine must have an external memory (such as a disk or flash) that can store 4G programs to ensure that program fragments can be called as needed. In this example, the page size is 4 K (4096 B), and the page size is the same as the page size-this must be ensured, because the transmission between memory and peripheral memory is always in the unit of pages. Corresponding to 4G virtual addresses and M physical storage, they contain 1 m pages and 64 K page frames respectively. (1 MB Page: 1 MB * 4 k = 2 ^ 20*2 ^ 2*2 ^ 10 = 2 ^ 32 = 4G) (64 K page frames: 64 K = 2 ^ 8*2 ^ 10 = 2 ^ 18 = 256 m)
A page table is like a function. The input is the page number and the output is the page number, which maps the page number to the physical address.
The operating system maintains a page table for each process. Therefore, the virtual addresses of different processes may be the same. The page table shows the position of the page frame corresponding to each page in the process.
PS: the "virtual memory" technology not only breaks through the limitation of physical memory in terms of functions, so that the program can manipulate the space larger than the actual physical memory, at the same time, isolate the security protection network of each process so that each process is not disturbed by other programs.
2. Page Replacement
Concept differentiation: Virtual Memory, physical memory, swap partition (hard disk storage area)
The Virtual Memory maps the entire program storage space and maps the currently running data to the corresponding physical memory for running through the virtual space. When the physical memory is insufficient, swap part of the data to the hard disk swap zone-swap. When necessary, replace the content of the SWAp partition in the physical memory.
Page Replacement: swap a page from memory to the swap area of the disk (SWAP partition, that is, part of the hard disk storage area)
Swap PartitionWhen the physical memory of the system is insufficient, release some space in the physical memory for the currently running program. The released space may come from some programs that haven't been operated for a long time. The released space is temporarily saved to the swap partition and will be executed by those programs, then, the stored data is restored from the swap partition to the memory.
Jitter: in a computer with virtual storage, the system efficiency is reduced due to frequent page adjustment activities that allow too many disk accesses.
For example, the operating system runs multiple programs at the same time. When you switch to a program that has been ignored for a long time, you will hear the hard disk crash. This is because the memory of this program is "stolen" by frequently running programs and put in the SWAp area. Therefore, once the program is placed on the front end, it will retrieve its data from the swap area, put it into the memory, and then run.
Not all data exchanged from the physical memory is stored in the SWAp. A considerable amount of data is directly exchanged to the file system. For example, to read and write files, you can directly put the memory space of these programs in the file when you need to swap them out. If it is a file read operation, the memory data is directly released and does not need to be exchanged, because it can be directly restored from the file system when needed next time; if it is a file write operation, you only need to save the changed data to the file for recovery. However, the data of objects generated using the malloc and new functions is different. They need swap space because they do not have the corresponding "reserve" file in the file system, therefore, it is called "anonymous" (anonymous) memory data. This type of data also includes some status and variable data in the stack. Therefore, the swap space is the swap space for "anonymous" data.
3. Swap Partition
Swap space is paging (physical memory is divided by PAGE frames). The size of each page is the same as that of the Memory Page, facilitating data exchange between swap space and memory.
In the earlier version of Linux, when implementing the swap space, the first page of the swap space is used as a bit map for all the swap space pages ). This means that each bit on the first page corresponds to a page of swap space. If this parameter is set to 1, swap is available on this page. If it is set to 0, this page is a bad block and cannot be used. In this case, the first swap ing bit should be 0, because the first page of swap is a ing page. In addition, the last 10 (byte) ing bits are also occupied to represent the version of swap. If the size of one page is S, the swap implementation method can manage "8 * (S-10)-1" swap pages. For i386 systems, if S = 4096, the total space size is 133890048. If 1 MB = 2 ^ 20 byte, the size is exactly 128 M.
Linux removes the bitmap method and removes the m limit. Direct access with address, limited to 2 GB (not understood)
4. Swap size allocation (impact on performance)
If the system's physical memory is used up, the system will run slowly but still run. If the swap space is used up, the system will encounter an error.
Generally, swap space should be greater than or equal to the size of physical memory, and the minimum should not be less than 64 M. Generally, the size of swap space should be 2-times that of physical memory. However, different applications should have different configurations: For a small desktop system, only a small swap space is required, A large server system requires different sizes of swap space depending on the situation. Especially for database servers and web servers, as the access traffic increases, the requirements for swap space will also increase. For detailed configuration, see the description of each server product.
Because swap operations are disk Io operations, if there are multiple swap areas, the swap space allocation will operate on all swap in turn, this will greatly balance the IO load and speed up swap switching.
During address ing, if you find that the page to be accessed is no longer in memory, a page disconnection occurs. When a page disconnection occurs, the operating system must select a page in the memory to remove it from the memory, so as to make room for the page to be transferred. The rule used to select which page to discard is called the page replacement algorithm.
Refer to here