Virtual Memory
Virtual Memory
Conventional memory-management strategies load a whole process before execution. Virtual memory allows a process to run without being entirely resident, making the apparent memory space larger than physical memory. It removes the direct physical-capacity constraint and supports sharing files and parts of address spaces, but remains limited by address space and backing storage. Implementation is difficult, and inappropriate use can greatly reduce performance.
The whole program need not be loaded at once
A program does not necessarily use all its code and data at the same time. For example, someone using an image editor might only crop an image, without using filters or export features yet. Parts that are not currently needed can remain outside physical memory until they are needed.
Consider only this program: suppose it can use 4MB of physical memory, while its code and data total 8MB. If it currently needs only 2MB, those 2MB can be resident and the other parts can remain in backing storage. A program being larger than available physical memory does not necessarily prevent it from running; what matters is which parts it needs during execution.
| Program contents | Size | Current location |
|---|---|---|
| Currently used parts | 2MB | Physical memory |
| Parts not currently needed | 6MB | Backing storage, such as disk |
Programs use virtual addresses; memory uses physical addresses
The OS gives each process its own virtual address space, which ordinary user processes use to access memory. It establishes virtual-to-physical mappings; the CPU's memory-management unit, MMU, performs translation on access. Private mappings refer to distinct physical memory to avoid interference, while shared mappings can refer to the same physical memory.
A memory access can be understood in three steps:
- The program generates a virtual address, such as 100.
- The MMU uses the current process's mapping to translate it into a physical address.
- If the contents are resident and the access is permitted, memory is read or written at that physical location.
If the required contents are not in physical memory yet, an ordinary memory access cannot simply read them directly from disk. The OS first brings them into physical memory and updates the mapping, then lets the program continue. The later paging section explains this on-demand loading process.
Why can two processes use the same address?
In the simplified mapping below, P1 and P2 both use virtual address 100. P1's mapping points to physical address 1100, while P2's points to 2100. The same address number refers to different physical locations for the two processes.
This does not mean that every virtual address is translated by adding one fixed number; the actual mapping depends on the memory-management scheme. For now, remember that the same address number does not imply the same physical location. If the OS deliberately establishes shared mappings, two processes can also access the same physical memory.
Virtual-to-physical address mapping
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Virtual memory is not extra high-speed memory
Virtual memory does not create more physical memory or make disks as fast as RAM. It gives programs independent address spaces and brings contents into memory when needed. If a running program needs many contents but little memory is available, repeated transfers between memory and disk can slow it down.