What Is Virtual Memory?
Virtual memory is a technology for computer system memory management. It makes the application think that it has contiguous available memory (a contiguous and complete address space). In fact, it is usually divided into multiple physical memory fragments, and some are temporarily stored on external disk storage. Perform data exchange at any time. At present, most operating systems use virtual memory, such as the "virtual memory" of the Windows family; "swap space" of Linux, and so on. [1]
- Chinese name
- Virtual Memory
- Foreign name
- Virtual Memory
- nickname
- Virtual memory
- Use
- Computer system memory management
- Virtual memory is a technology for computer system memory management. It makes the application think that it has contiguous available memory (a contiguous and complete address space). In fact, it is usually divided into multiple physical memory fragments, and some are temporarily stored on external disk storage. Perform data exchange at any time. At present, most operating systems use virtual memory, such as the "virtual memory" of the Windows family; "swap space" of Linux, and so on. [1]
Introduction to virtual memory
- Virtual memory is also called virtual memory. Computer running
- Virtual memory is part of the hard disk space that Windows uses as memory. Virtual memory is actually a huge file on the hard disk. The file name is PageFile.Sys, which is usually invisible. You must turn off Explorer's protection of system files to see this file. Virtual memory is sometimes referred to as a "page file" and is derived from the file name of this file. [2]
- Memory plays a large role in the computer. All programs running in the computer need to be executed by the memory. If the programs executed are large or many, the memory will be exhausted. In order to solve this problem, WINDOWS uses virtual memory technology, that is, taking out a part of hard disk space to serve as memory usage. This part of the space is called virtual memory. The virtual memory exists on the hard disk in the form of PAGEFILE.SYS. [2]
How virtual memory works
- Virtual memory is automatically scheduled and managed by hardware and operating system. Its working process includes 6 steps: [3]
- The logical address of the central processing unit accessing the main memory is decomposed into a group number a and an address b in the group, and the address conversion is performed on the group number a. That is, the logical group number a is used as an index. In main memory. [3]
- If the group number is already in the main memory, go to execute ; if the group number is not in the main memory, check whether there is a free area in the main memory, and if not, call out a temporarily unused group. Go to secondary storage to transfer this group of information into main storage. [3]
- Read the desired group from the auxiliary memory, and send it to the free area of the main memory, and then register the free physical group number a and logical group number a in the address conversion table. [3]
- The physical group number a corresponding to the logical group number a is read from the address conversion table. [3]
- Obtain the physical address from the physical group number a and the byte address b in the group. [3]
- Access the necessary information from the main memory based on the physical address. [3]
Virtual memory scheduling
- Scheduling methods are paged, segmented, and paged. Paged scheduling is the division of logical and physical address space into fixed-size pages. The main memory is numbered in page order, and each independently addressed program space has its own page number order. By scheduling each page of the program in the auxiliary memory, it can be discretely loaded into different page positions in the main memory, and can be one by one according to the table. Correspondence search. The advantage of page-based scheduling is that the pages within the page are small, the page table is transparent to the programmer, the address conversion is fast, and the loading operation is simple. The disadvantage is that each page is not an independent module of the program, which is not convenient for the protection of programs and data. Segment scheduling divides the address space according to the logical structure of the program. The length of the segment is arbitrary and allows extension. Its advantages are that it eliminates the memory fragment, is easy to implement storage protection, and facilitates the dynamic assembly of the program; the disadvantage is that the loading operation is complicated . Combining these two methods constitutes segmented page scheduling. In segment page scheduling, the physical space is divided into pages. The program is divided into modules, and each segment is divided into pages that are as small as the physical space pages. Segmented page scheduling combines the advantages of segmented and paged. The disadvantages are increased hardware costs and complicated software. Most large-scale general-purpose computer systems use segmented page scheduling. [3]
Virtual memory page scheduling
- In a paged virtual storage system, virtual space is divided into pages of equal size, called logical pages or virtual pages. The main memory space is also divided into pages of the same size, called physical pages or real pages. Correspondingly, the virtual address is divided into two fields: the high-order field is a virtual page number, and the low-order field is an in-page address. The actual address is also divided into two fields: the high-order field is the real page number, and the low-order field is the page address. At the same time, the size of the page is taken as an integer power of two. [4]
- Page tables can be used to convert virtual addresses into physical addresses. Each program sets a page table. In the page table, there is an entry for each virtual page number. The content of the entry contains at least the main memory page address (real page number) where the virtual page is located. High-order field; the real page number is concatenated with the in-page address of the virtual address to generate a complete physical address, and access the main memory accordingly. [4]
Virtual memory segmented scheduling
- Pages are fixed-length fixed-area areas that are divided into the physical memory space. The paging method has the advantages of fixed page length, so it is easy to construct page tables, easy to manage, and there is no external fragmentation. But the disadvantage of the paging method is that the page length is not related to the logical size of the program. For example, at some point a subroutine may have part of it in main memory and another part in auxiliary memory. This is not conducive to the independence during programming, and causes troubles to operations such as swap-in / swap-out processing, storage protection, and storage sharing. [4]
- Another method of dividing the addressable storage space is called segmentation. Segments are regions that are divided according to the natural boundaries of the program and whose length can be changed dynamically. Generally, programmers divide different types of data such as subroutines, operands, and constants into different sections, and each program can have multiple sections of the same type. [4]
- In a segmented virtual storage system, a virtual address is composed of a segment number and an intra-segment address, and the conversion from a virtual address to a real storage address is implemented by a segment table. Each program sets a segment table. Each entry in the segment table corresponds to a segment. Each entry includes at least three fields: a valid bit (indicating whether the segment has been transferred to main memory), a segment start address (the segment The first address in real memory) and the segment length (record the actual length of the segment). [4]
Virtual memory segment page scheduling
- Segment paged virtual memory is a combination of segmented virtual memory and paged virtual memory. [4]
- First, the real memory is divided into pages. In the segment-page virtual memory, after the program is segmented according to the logical structure, each segment is paged according to the size of the actual page. The program is paged in and out, but it can be shared and segmented by segment. protection. Therefore, it can have the advantages of both page and segment systems. Its disadvantage is that it needs to look up the table multiple times in the process of address mapping. The conversion of a virtual address into a physical address is located by a segment table and a set of page tables. Each entry in the segment table corresponds to a segment, and each entry has a starting address (page number) that points to the page table of the segment and the control protection letter page table of the segment indicates that each page of the segment is in main memory And the flags such as whether they are loaded, modified, and so on. [4]
Virtual memory virtual storage address translation
- Virtual memory address transformation basically has three types of virtual memory working process: full associative transformation, direct transformation and group associative transformation. The way in which any logical space page can be transformed to any page position in physical space is called a full associative transformation. The way that each logical space page can only transform to a specific page in physical space is called direct transformation. Group associative transformation refers to the direct transformation between groups, and the total associative transformation between pages in the group. Replacement rules are used to determine which part of the main memory is replaced in order to vacate part of the main memory and store the content to be transferred from the auxiliary memory. There are four common replacement algorithms. [5]
- Random algorithm: use software or hardware random number generator to determine the replacement page. [5]
- First-in, first-out: the page transferred to the main memory is replaced first. [5]
- Least Recently Used (LRU): Replace pages that have not been used for a long time. [5]
- Optimal algorithm: Replace pages that are used after the longest time. This is an idealized algorithm and can only be used as a measure of the pros and cons of various other algorithms. [5]
- The efficiency of virtual memory is an important part of system performance evaluation, and it is related to factors such as main memory capacity, page size, hit rate, program locality, and replacement algorithms. [5]
The key issues of virtual memory
- (1) Scheduling problem: Decide which programs and data should be transferred to main memory. [6]
- (2) Address mapping problem: when accessing the main memory, the virtual address is changed to the physical address of the main memory (this process is called internal address translation); when accessing the auxiliary memory, the virtual address is changed to the physical address of the auxiliary memory (this The process is called Outer Address Translation) in order to change pages. In addition, issues such as main memory allocation, storage protection, and program relocation must be addressed. [6]
- (3) Replacement problem: Decide which programs and data should be recalled from main memory. [6]
- (4) Update problem: ensure the consistency of main storage and auxiliary storage. [6]
- Under the control of the operating system, hardware and system software solve the above problems for the user, thereby greatly simplifying the programming of the application program. [6]
Virtual memory virtual memory settings
- Now take Windows system as an example to see how the virtual memory is set. Windows system uses memory virtualization technology to satisfactorily solve the problems caused by insufficient physical memory. In Windows, virtual memory is actually a large file called pagefile.sys, which is called a page file. The paging file has system and hidden attributes. Normally, it is invisible. Only the "Hide protected operating system files" and "Do not show hidden files and folders" are displayed in the "Folder Options" dialog box. You can see it only when the hook is removed. [7]
- Right-click "My Computer" on the Windows desktop, click "Properties" in the menu, click Settings in the Performance tab of the Advanced tab, and then select the "Advanced" option in "Performance Options". The virtual memory column appears below. Click the "Change" button with the mouse, and the "Virtual Memory" settings dialog box opens. [7]
- The Windows virtual memory settings page file can be placed on any hard drive that is online in the system. The size of this virtual memory can be customized, and it changes dynamically. You need to set the initial size and maximum value. You can also choose not to manually allocate it, and it will be managed automatically by the operating system; There is no virtual memory, and the operation of the system may be affected as a result, so it is usually not set this way. [7]
Virtual memory related concepts
Virtual memory real address and virtual address
- The address used when the user compiles the program is called a virtual address or logical address, and its corresponding storage space is called a virtual memory space or logical address space; and the access address of the computer's physical memory is called a real address or physical address, and its corresponding storage Space is called physical storage space or main storage space. The process of program's virtual address to real address conversion is called program relocation. [6]
Virtual memory access process
- The user program in the virtual memory space is programmed in accordance with the virtual address and stored in the auxiliary memory. When the program is running, the address conversion mechanism transfers a part of the program into real memory according to the real address space allocated to the program at that time. Each time memory is accessed, first determine whether the part corresponding to the virtual address is in real memory: if it is, perform address translation and access the main memory with the real address; otherwise, schedule some programs in the auxiliary memory into according to some algorithm Memory, and then access main memory in the same way. [6]
- It can be seen that the virtual address space of each program can be much larger than the real address space, and it can also be much smaller than the real address space. The latter situation usually occurs in multi-user or multi-tasking systems: the real memory space is large, while a single task does not require a large address space, and the small virtual memory space can shorten the length of the address field in the instruction. [6]
- With the virtual memory mechanism, applications can transparently use the entire virtual memory space. For applications, if the hit rate of the main memory is high, the access time of the virtual memory is close to the main memory access time, and the size of the virtual memory depends only on the size of the auxiliary memory. [6]
Virtual memory heterogeneous system
- From the concept of virtual memory, it can be seen that the access mechanism of main storage and auxiliary storage is similar to that of cache main storage. This is two levels in a three-level storage system consisting of cache memory, main memory and auxiliary memory. Between cache and main storage and between main storage and auxiliary storage, there are auxiliary hardware and auxiliary software and hardware respectively responsible for address translation and management, so that all levels of memory can form an organic three-tier storage system. The cache and main storage constitute the system's memory, and the main storage and auxiliary storage form the virtual storage with the support of auxiliary software and hardware. [6]
- In the three-tier storage system, the cache-main storage and main storage-auxiliary storage levels have many similarities: [6]
- (1) The starting point is the same: both are tiered storage systems constructed to improve the performance-price ratio of the storage system, and both strive to make the performance of the storage system close to high-speed storage, and the price and capacity close to low-speed storage. [6]
- (2) The principle is the same: the locality principle of the program is used to transfer recently used information blocks from a relatively slow and large-capacity memory to a relatively high-speed and small-capacity memory. [6]
- However, there are many differences between the two storage levels of cache-main storage and main storage-auxiliary storage: [6]
- (1) Different emphasis: cache mainly solves the problem of the speed difference between main memory and CPU; and in terms of improving the performance price ratio, virtual memory mainly solves the problem of storage capacity, and also includes storage management, main memory allocation and storage protection etc. [6]
- (2) Different data paths: There are direct access paths between the CPU, cache, and main memory, and direct access to main memory when the cache misses; and there is no direct data path between the auxiliary memory that the virtual memory depends on and the CPU. When the main memory is missed, it can only be solved by paging, and the CPU will eventually access the main memory. [6]
- (3) Transparency: The management of the cache is completely completed by hardware, which is transparent to system programmers and application programmers. The virtual memory management is completed by software (operating system) and hardware. Due to the intervention of software, virtual memory is implemented by System programmers for storage management are opaque and only transparent to application programmers (segmented and segmented page management are "translucent" to application programmers). [6]
- (4) The loss when missed is different: because the access time of the main memory is 5 to 10 times the access time of the cache, and the access speed of the main memory is usually thousands of times faster than the access speed of the auxiliary memory, so The performance loss when the main memory misses is much greater than the loss when the cache misses. [6]