A small desk can still be used for a very large project if the papers not in use are kept in a drawer and swapped in when needed. Virtual memory lets a program use more memory than is physically installed in the same way.
Virtual Memory
Virtual memory gives programmers the illusion of a very large memory, although the main memory is small. The addresses used by a program are called virtual addresses, and the set of them is the address space. The real memory locations are physical addresses, and the set of them is the memory space. A mapping converts the virtual addresses to physical addresses while the program runs.
Definition: Virtual memory. A technique that lets a program use an address space larger than the main memory, by keeping the parts not in use in auxiliary memory and bringing them in when needed.
Paging
In paging, the address space is divided into equal pages, and the memory space into blocks of the same size. A page table records in which block each page is held. A virtual address has a page number and a line number within the page. If the page is not in main memory, a page fault occurs and the page is brought from the disk, replacing another page chosen by a replacement policy. FIFO replaces the oldest page, and LRU replaces the one unused for the longest time.
Memory Management Hardware
The memory management unit (MMU) is the hardware that handles the mapping. It translates virtual addresses to physical addresses, protects the pages of one program from another, and shares memory between programs. A small, fast cache of recent page table entries, the translation lookaside buffer (TLB), avoids a memory access for the page table on most references.
Table 4.12: Cache and virtual memory compared
| Feature | Cache memory | Virtual memory |
|---|---|---|
| Purpose | Speed up access | Give a larger memory |
| Between | CPU and main memory | Main memory and disk |
| Unit of transfer | Line or block | Page |
| Controlled by | Hardware | Hardware and operating system |
Example 4.11: Counting page faults
Problem: A program makes the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with three frames in main memory. Count the page faults with the FIFO and the LRU policy.
Solution:
- With FIFO the faults occur for 7, 0, 1, 2, 3, 0, 4, 2, 3 and 0.
- With LRU the faults occur for 7, 0, 1, 2, 3, 4, 2, 3 and 0.
- The count for FIFO is 10 and for LRU is 9.
Result: FIFO gives 10 page faults and LRU gives 9 for this reference string.
def faults(refs, frames, lru):
mem, n = [], 0
for p in refs:
if p in mem:
if lru:
mem.remove(p); mem.append(p)
continue
n += 1
if len(mem) == frames:
mem.pop(0)
mem.append(p)
return n
r = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
print(faults(r, 3, False), faults(r, 3, True))
Output
10 9
Common mistakes
- Believing that virtual memory is a separate physical memory. It is a mapping technique.
- Mixing up pages, which belong to the address space, and blocks, which belong to the memory space.
- Forgetting that a page fault needs the page to be read from the disk.
Key takeaways
- Virtual addresses are converted to physical addresses by the page table.
- A page fault occurs when the page is not in main memory.
- FIFO replaces the oldest page; LRU replaces the least recently used page.
- The MMU and TLB do the address translation.
Practice questions
- What is a page fault?
- What is the function of the page table?
- What does LRU replace?
Answers to the practice questions
- An event in which the required page is not in main memory and must be brought from the disk.
- It records in which memory block each page of the address space is held.
- The page that has not been used for the longest time.
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit IV
