Virtual Memory Management Methods
Virtual memory is a technology that allows a computer system to provide a storage space larger than the physical memory (main memory). It simulates a large storage space by storing part of the data in a slower secondary storage (such as a hard disk) and calling this data into the main memory when needed. The emergence of virtual memory makes it unnecessary for programmers to care about the actual size of the memory because it creates a seemingly almost unlimited storage space.
Definition of virtual memory
- Virtual memory does not actually exist. It is an abstract concept that aims to expand the capacity of the main memory and allow programs to access storage space beyond the actual physical memory.
- It is implemented through discretely allocated storage management methods (such as paging, segmentation, etc.), allowing jobs to be called into the main memory at different times, even if the entire job is not completely loaded into the main memory.
- The capacity of virtual memory depends on the computer's address structure and the capacity of the secondary storage (such as disk), and has nothing to do with the actual main memory capacity. The system makes virtual memory look as fast as main memory through the cooperation of hardware and operating system, but its cost is lower, close to the cost of secondary memory.
Main features of virtual memory
Virtual memory has four major features, namely:
-
Discreteness: Virtual memory breaks the limitation of traditional continuous storage. The data in the memory is not stored continuously, but scattered. This means that the system no longer requires that all the data of the program must be stored continuously in the physical memory.
-
Multiplicity: Virtual memory supports partial loading, that is, the job can only load a part of the page or segment into the main memory. The job can be called into the main memory multiple times during execution. In this way, large-scale programs can still be run when the main memory space is limited.
-
Swapability: Virtual memory allows swapping of data between main memory and auxiliary storage (such as hard disk). When a data page is no longer used, it can be swapped out of the main memory, and the freed space can be used to load other data pages. The swap mechanism ensures that the program can run even though its data is not completely stored in the main memory.
-
Virtuality: Through virtual memory, the program thinks that it has a huge address space, but in fact its physical storage space is much smaller. This allows programmers to write programs without considering the limitations of physical memory.
Demand Paging Memory Management
Demand Paging Memory Management is an implementation of virtual memory that combines paging memory management and page replacement technology. It manages virtual memory in the following ways:
Basic working principle
In demand paging memory management, the pages of a job are not loaded into the main memory at once, but are loaded dynamically as needed:
-
Partial loading: When a job starts, only a part of its pages are loaded into the main memory. Other pages can be loaded lazily.
-
Page fault interrupt: During program execution, if the accessed page has not been loaded into the main memory, a page fault interrupt will be triggered. The system will detect the page fault and start the disk I/O operation to transfer the missing page from the auxiliary storage (such as disk) to the main memory.
-
Page replacement: If the main memory space is full, the system needs to select a currently inactive page to replace it to make room for the newly loaded page. This process involves page replacement algorithms (such as LRU (least recently used) algorithm).
Workflow
-
Starting a job: When a job starts, only some necessary pages are loaded into the main memory. These pages may be the initial code of the program or the parts that must be accessed immediately when the program is executed.
-
Page fault interrupt: When the program is executed, if a page is not in the main memory, the operating system will generate a page fault interrupt. After the page fault interrupt is triggered, the operating system will look for the auxiliary storage where the page is located.
-
Page replacement: If the main memory space is full, the operating system will use the page replacement algorithm (such as FIFO, LRU, etc.) to decide which page to call out and call the required page into the main memory. The page that is replaced may be written back to the disk so that it can be reloaded when it is needed again in the future.
-
Address translation: Through the address translation mechanism, the logical address (virtual address) accessed by the program is mapped to the physical address. The operating system finds the physical location corresponding to the page by looking up the page table.