📖 Virtual Memory
Virtual Memory: Demand Paging, Page Replacement, and Thrashing
Virtual Memory explains how an operating system provides processes with large, protected address spaces while keeping only necessary portions in physical memory. Building on paging, page tables, address translation, and memory protection, this chapter develops demand paging, copy-on-write, page replacement, and frame allocation. It then connects working-set management and thrashing with memory-mapped files, kernel memory allocation, and hardware translation considerations such as page size and TLB reach. Windows and Solaris examples demonstrate how these mechanisms operate in practical computing environments.
Prerequisites for Understanding Virtual-Memory Management
Students should understand paging, logical and physical addresses, page tables, page-table entries, and memory protection from previous memory-management studies. Familiarity with address translation, TLB operation, physical memory frames, and page faults is particularly important. Knowledge of secondary storage, file systems, process address spaces, and basic I/O operations will help explain demand paging, memory-mapped files, page replacement, and kernel memory allocation.
Learning Objectives: Page Faults, Frame Allocation, and Memory Mapping
After studying this chapter, students should be able to explain demand paging, calculate its effective access time, and describe copy-on-write and prepaging. They should compare FIFO, optimal, LRU, approximate LRU, and counting-based page replacement algorithms, including buffering techniques. They should analyze frame allocation, NUMA considerations, and thrashing using working-set and page-fault-frequency models. They should also explain memory-mapped files, shared memory, kernel memory allocators, page locking, page-table structures, TLB reach, and Windows and Solaris virtual-memory implementations.
Demand Paging, Page Replacement, and Virtual-Memory Techniques
- Demand Paging and Initial Page Loading - explains page faults, performance, copy-on-write, and prepaging.
- Page Replacement Policies - compares FIFO, optimal, LRU, approximations, counting, buffering, and practical applications.
- Frame Allocation and Thrashing - examines minimum frames, allocation strategies, NUMA, working sets, and page-fault frequency.
- Memory-Mapped Files and I/O - covers file mapping, Windows shared memory, and memory-mapped device I/O.
- Kernel Memory Allocation - explains buddy and slab allocators and pages locked during I/O.
- Page Translation and Virtual-Memory Design - covers page size, TLB reach, inverted tables, and program locality.
- Operating-System Implementations - examines virtual-memory mechanisms and practical examples in Windows and Solaris.
Learning Path: From Demand Paging to Virtual-Memory Implementation
Begin with demand paging to understand how pages enter physical memory only when required. Study page-fault handling, performance calculations, copy-on-write, and prepaging before examining replacement algorithms. Next, learn how minimum frame requirements and allocation policies affect process execution. Study thrashing through working-set and page-fault-frequency models. Then explore memory-mapped files, shared memory, and memory-mapped I/O. Continue with kernel memory allocation and page locking. Finish with page size, TLB reach, inverted page tables, program locality, and operating-system implementation examples.
How Page Faults, Replacement Policies, and Frame Allocation Interact
Demand paging introduces on-demand loading, while copy-on-write avoids unnecessary page duplication. Page faults trigger replacement decisions when free frames are unavailable. Frame allocation determines how many frames processes receive, and NUMA introduces memory-locality considerations. Insufficient frames can cause thrashing, which working-set and page-fault-frequency models help identify. Memory mapping and kernel allocation extend memory-management applications, while page size, TLB reach, and page-table structures determine address-translation efficiency.
How Virtual Memory Loads, Replaces, and Maps Pages During Execution
Assume a process maps a file and first reads virtual address 0x00402000 on a system using 4 KiB pages. Demand paging raises a page fault because the valid page is not resident. The operating system loads the corresponding file data into a physical frame, updates the page table, and makes the translation available. If the process later writes to a copy-on-write page, the operating system creates a private copy. Under memory pressure, page replacement selects a victim. Repeated faults can reveal thrashing, assessed through working-set or page-fault-frequency measures.
Why Virtual Memory Supports Large and Efficient Applications
Without virtual memory, applications would face stricter physical-memory limitations, inefficient loading, and reduced isolation. Poor replacement and allocation decisions could cause excessive page faults and thrashing. Memory mapping and kernel allocation support efficient resource use, while page protection and locking maintain correctness. These mechanisms underpin modern application execution, shared libraries, multitasking, and efficient memory utilization.
Common Misconceptions About Page Faults and Page Replacement
Students sometimes assume 'every page fault indicates an invalid memory access.' A valid page can generate a page fault simply because it is not currently resident. Another misconception is 'page replacement and frame allocation perform the same task.' Frame allocation determines how many frames a process receives, whereas replacement selects a victim page. Finally, 'copy-on-write immediately duplicates shared pages' is incorrect because copying occurs only when a process attempts a modifying write.
Virtual-Memory Algorithms and Performance: Key Exam Areas
Examinations commonly test effective access time calculations, page-fault handling, and tracing FIFO, optimal, and LRU replacement sequences. Students should calculate page faults, compare replacement policies, and analyze frame allocation. Other important questions involve working-set calculations, thrashing, copy-on-write, and page buffering. Students should also explain TLB reach, compare page-table structures, distinguish memory-mapped files from memory-mapped I/O, and identify kernel memory allocation techniques.
Summary of Demand Paging and Virtual-Memory Management
This chapter explains how virtual memory separates process address spaces from limited physical memory. Demand paging and copy-on-write reduce unnecessary loading, while replacement and allocation policies manage available frames. Working-set analysis helps control excessive paging. Memory mapping extends access to files and shared regions, while kernel allocators manage memory internally. Hardware translation structures and operating-system implementations complete the picture of efficient virtual-memory management.
Essential Virtual-Memory and Page-Replacement Terminology
- Demand Paging - a memory-management technique that loads a page into physical memory when it is first needed.
- Page Fault - an exception generated when an accessed virtual page requires operating-system handling.
- Copy-on-Write - a technique that delays copying shared pages until a process attempts to modify them.
- Page Replacement - the process of selecting a resident page to remove when another page needs a frame.
- Working Set - the collection of pages a process has actively referenced during a defined recent interval.
- Thrashing - a condition in which excessive paging leaves processes spending disproportionate time handling page faults.
- Memory-Mapped File - a file whose contents are accessed through a process address-space mapping.
- TLB Reach - the amount of virtual memory addressable through the page translations currently held in the TLB.
Further Reading on Virtual-Memory Design
- Linux Kernel Documentation - Memory Management - explains page allocation, reclaim, page tables, memory zones, and kernel memory-management mechanisms.
- Linux man-pages - mmap(2) - provides practical details about file mapping, shared memory mappings, and memory protection flags.
- Microsoft Learn - File Mapping - explains Windows file-mapping objects, shared memory, mapped views, and memory-mapped file operations.
- Microsoft Learn - Virtual Address Spaces - examines virtual-memory allocation, address-space organization, page protection, and memory management in Windows.
- Linux Kernel Documentation - Slab Allocation - explores kernel object allocation, slab caches, and efficient management of frequently allocated data structures.
- Oracle Solaris Internals - Virtual Memory - examines Solaris virtual-memory architecture, page management, memory allocation, and paging behavior.