NOTE
Virtual Memory
[toc] 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
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.
- Address the space of memory + disk to form the process’s logical address space, and address 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

Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub