Back to All Lectures

Lecture 18: Virtual Memory

Lectures on Computer Architecture

Click the thumbnail above to watch the video lecture on YouTube

By Dr. Isuru Nawinne

18.1 Introduction

Virtual memory represents one of the most elegant abstractions in computer architecture, creating a layer between physical memory hardware and the memory view presented to programs. This lecture explores how virtual memory enables programs to use more memory than physically available by treating main memory as a cache for disk storage, supports safe execution of multiple concurrent programs through address space isolation, and provides memory protection mechanisms preventing programs from corrupting each other's data. We examine page tables, translation lookaside buffers (TLBs), page faults, and the critical design decisions that make virtual memory both practical and performant despite the enormous speed gap between RAM and disk storage.

18.2 Introduction to Virtual Memory

Virtual memory allows programs to use more memory than physically available by using main memory as a cache for secondary storage.

18.2.1 Key Purposes of Virtual Memory

  1. Allow programs to use more memory than actually available
  2. Support multiple programs running simultaneously on a CPU
  3. Enable safe and efficient memory sharing between programs
  4. Ensure programs only access their allocated memory

18.3 CPU Word Size and Address Space

The relationship between CPU word size and addressable memory determines the maximum amount of memory that can be addressed.

18.3.1 Address Space by CPU Word Size

8-bit CPU

16-bit CPU

32-bit CPU

64-bit CPU

18.3.2 Historical Pattern

18.4 Virtual vs Physical Addresses

18.4.1 Virtual Address

18.4.2 Physical Address

18.4.3 Address Translation

18.5 Memory Hierarchy with Virtual Memory

Complete hierarchy from top to bottom:

  1. CPU (generates virtual addresses, thinks memory is large and fast)
  2. Cache (virtually or physically addressed)
  3. Main Memory (acts as cache for secondary storage)
  4. Secondary Storage/Disk (contains all pages)

CPU accesses cache directly. Main memory acts as cache for disk, not just a second level cache - requires additional mechanisms.

18.6 Terminology

18.6.1 CPU Level

18.6.2 Cache Level

18.6.3 Memory Level

18.7 Access Latencies

Understanding the latency differences is crucial for virtual memory design:

Access Latencies

18.8 Virtual and Physical Address Structure

18.8.1 Example with 32-bit Addresses

Virtual Address (32 bits)

Physical Address (28 bits)

18.8.2 Key Points

18.9 Supporting Multiple Programs

Multiple programs can run simultaneously by sharing physical memory:

18.9.1 Each Program

18.9.2 Memory Sharing

18.9.3 Example

18.10 Page Table

The page table is a data structure stored in memory that contains address translations.

18.10.1 Purpose

18.10.2 Page Table Entry Contents

  1. Physical Page Number (main component)
  2. Valid Bit: Is the page currently in memory?
    • 1 = Page is in memory (translation valid)
    • 0 = Page not in memory (page fault)
  3. Dirty Bit: Has page been modified?
    • 1 = Page modified, inconsistent with disk
    • 0 = Page not modified, consistent with disk
  4. Additional bits: Access permissions, memory protection status

18.10.3 Finding Page Table

18.11 Address Translation Process

Address Translation Process

Steps to access memory:

  1. CPU generates virtual address (virtual page number + page offset)
  2. Access page table using PTBR + virtual page number as index
  3. Read page table entry:
    • If valid bit = 0: Page fault (handled by OS)
    • If valid bit = 1: Read physical page number
  4. Construct physical address: Physical page number + page offset
  5. Access physical memory with physical address
  6. Return data to CPU

18.11.1 Memory Accesses Required

18.12 Page Table Size Calculation

18.12.1 Example: 4 GB Virtual, 1 GB Physical, 1 KB Pages

Number of Entries

Entry Size

Total Page Table Size

18.13 Write Policy for Virtual Memory

18.13.1 Write-Through: NOT USED

18.13.2 Write-Back: USED (Standard Policy)

18.14 Placement Policy

18.14.1 Fully Associative Placement

18.14.2 Why Fully Associative?

18.15 Page Fault Handling

18.15.1 What Operating System Must Do

1. Fetch Missing Page

2. Find Unused Frame

3. If Memory Full (No Unused Frames)

4. Check Dirty Bit of Page to be Replaced

5. Update Data Structures

18.15.2 Optimization

18.15.3 Why Software Handling?

18.16 Translation Lookaside Buffer (TLB)

TLB

18.16.1 Purpose

18.16.2 What is TLB?

18.16.3 TLB Entry Structure

18.16.4 TLB Parameters

Size

Block Size

Placement Policy

Hit Latency

Miss Penalty

18.16.5 TLB Operation

Hit

Miss

18.16.6 Why Low Miss Rate Essential?

18.17 Complete Memory Access with TLB

Two different approaches for handling memory access with TLB:

18.18 Approach 1: Virtually Addressed Cache

18.18.1 Process

  1. CPU generates virtual address
  2. Access cache with virtual address (parallel with TLB)
  3. Cache Hit: Return data to CPU immediately
  4. Cache Miss:
    • Check TLB for address translation
    • TLB Hit:
      • Get physical address
      • Access memory with physical address
      • Fetch missing block
      • Update cache
      • Send word to CPU
    • TLB Miss:
      • Access page table in memory
      • Page Hit:
        • Get translation
        • Access memory for data
        • Update TLB
        • Update cache
        • Send word to CPU
      • Page Fault:
        • OS accesses disk
        • Fetch missing page
        • Find unused frame or replace page
        • If replaced page dirty: write back
        • Update page table
        • Update TLB
        • Update cache
        • Send word to CPU

18.18.2 Advantage

18.19 Approach 2: Physically Addressed Cache

18.19.1 Process

  1. CPU generates virtual address
  2. Access TLB for translation first
  3. TLB Hit:
    • Get physical address
    • Access cache with physical address
    • Cache Hit: Return data to CPU
    • Cache Miss:
      • Access memory with physical address
      • Fetch missing block
      • Update cache
      • Send word to CPU
  4. TLB Miss:
    • Access page table in memory
    • Page Hit:
      • Get translation
      • Update TLB
      • Access cache with physical address
      • If cache hit: return data
      • If cache miss: fetch from memory, update cache, return data
    • Page Fault:
      • OS handles as described above
      • Update page table, TLB, cache
      • Return data to CPU

18.19.2 Advantage

18.19.3 Key Difference

Both approaches are valid, and the choice depends on cache indexing method (virtual vs physical).

Key Takeaways

  1. Virtual memory provides memory abstraction and protection
  2. Address translation is fundamental to virtual memory operation
  3. Page tables map virtual addresses to physical addresses
  4. TLB caches translations to avoid double memory access
  5. Page faults are extremely expensive (~1 million cycles)
  6. Write-back policy is essential for virtual memory
  7. Fully associative placement minimizes page faults
  8. Multiple programs can safely share physical memory
  9. OS handles page faults in software
  10. Virtual memory enables modern multitasking operating systems

Summary

Virtual memory represents a crucial abstraction in modern computing, enabling efficient and safe memory management across multiple concurrent programs while providing each program with the illusion of abundant, dedicated memory resources.