Virtual Memory: Paging and Segmentation

Virtual memory creates an illusion for the programmer that the system has a very large, contiguous main memory, even if the physical RAM is small. It handles the mapping between 'Virtual Addresses' (used by software) and 'Physical Addresses' (actual RAM locations).

1. Paging

Paging divides virtual memory into fixed-size blocks called **pages** and physical memory into blocks called **frames**. This eliminates the need for contiguous allocation and reduces external fragmentation.

  • **Page Table:** A data structure used by the OS to store the mapping between pages and frames.
  • **Page Fault:** Occurs when the CPU tries to access a page not currently in RAM, triggering a fetch from the disk.

2. Segmentation

Unlike paging, segmentation divides memory into logical units of varying sizes (e.g., Code, Data, Stack). It reflects the programmer's view of the program.

  • **Advantages:** Better protection (e.g., read-only segments) and easier sharing of data between processes.
  • **Disadvantages:** Leads to external fragmentation because segments vary in length.

3. TLB (Translation Lookaside Buffer)

Address translation via a page table in RAM is slow because it requires two memory accesses. The TLB is a small, high-speed cache inside the CPU that stores recent virtual-to-physical address mappings.

TEXT
Process: CPU -> TLB Hit? -> (Yes) Get Physical Address -> (No) Check Page Table in RAM

Comparison Table

FeaturePagingSegmentation
Block SizeFixed (e.g., 4KB)Variable (Logical size)
FragmentationInternal FragmentationExternal Fragmentation
User VisibilityInvisible to programmerVisible to programmer
AccountabilityManaged by Hardware/OSDefined by Compiler/User

Common Mistakes to Avoid

  • Confusing 'Page Table' (stored in RAM) with 'TLB' (stored in CPU).
  • Thinking virtual memory makes a computer faster (it actually adds overhead; it only increases capacity).
  • Assuming 'Thrashing' is just a slow computer (it's specifically when the system spends more time swapping pages than executing).
  • Ignoring that modern systems often use 'Paged Segmentation' to get the best of both worlds.

Advanced Concepts

  • Multi-level Page Tables
  • Inverted Page Tables
  • Page Replacement Algorithms (LRU, Optimal, FIFO)
  • Demand Paging
  • Memory Management Unit (MMU)

Practice Exercises

  • Calculate the physical address given a virtual address and a page table.
  • Explain why the TLB is critical for system performance.
  • Compare internal vs. external fragmentation with a diagram.
  • Research why most modern 64-bit systems use 4-level paging.

Conclusion

Virtual memory is one of the most powerful concepts in computer architecture. Through Paging and TLB optimization, it allows multiple large programs to run securely and efficiently on limited physical hardware.

Note: Note: A high TLB Hit Rate (usually >99%) is essential to prevent the CPU from slowing down due to memory management overhead.