NOTE
Virtual Memory
Programs may require more space than physical memory provides. These notes examine virtual memory through address mapping and swapping.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Why Virtual Memory Is Needed
To solve the problem of insufficient physical memory. For example, if we have a 16GB game but only 4GB of physical memory, how can it run?
Traditional memory management has two problems:
- A job must be loaded into memory all at once.
- A job must remain resident in memory while it runs.
Put simply, memory utilization is low, and a lot of unused data occupies memory for a long time.
2. What Is Virtual Memory?
Segmentation and paging + principle of locality + page-fault interrupt + page replacement.
- Assign addresses to the combined memory and disk space to form the process’s logical address space, and assign addresses to memory to form the physical address space. The process logical address is divided into segments, each segment is divided into pages, and the physical address is divided into pages. The process’s pages are loaded into physical-memory pages in page units.
- According to the principle of locality, the operating system loads only part of a program’s instructions and data (the currently executing instructions and data and the surrounding instructions and data) into memory; the rest remains in secondary storage.
- When the accessed instructions and data are not in memory, the operating system handles a page-fault interrupt and loads this part of the instructions and data into memory.
- When memory is insufficient, the operating system performs page replacement and moves unused instructions and data in memory back to secondary storage.
2.1. Underlying Principles of Virtual Memory
2.1.1. Segmentation and Paging Memory Management
2.1.2. Principle of Locality
- Temporal locality: a referenced memory location may be referenced again.
- Spatial locality: data near a referenced memory location is very likely to be referenced.
According to this principle, a program does not need to be fully loaded into memory when it runs; loading only part of it (the currently executing instruction and surrounding instructions) is sufficient.
2.1.3. Page-Fault Interrupt
When the accessed instructions and data are not in memory, the operating system handles a page-fault interrupt and loads this part of the instructions and data into memory.
2.1.4. Page Replacement
3. Linux swap

4. Windows

5. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub