Understand virtual memory and its role

Resources | Subject Notes | Computer Science

Virtual Memory

Virtual memory is a memory management technique that gives processes the illusion of having more memory than is physically available. It allows programs to access memory locations that are not currently in RAM, using the hard disk as an extension of RAM.

How Virtual Memory Works

  1. Address Translation: The operating system (OS) uses a Memory Management Unit (MMU) to translate virtual addresses (used by the program) into physical addresses (in RAM).
  2. Paging: Both virtual and physical memory are divided into fixed-size blocks called pages and frames, respectively.
  3. Page Table: The OS maintains a page table for each process. This table maps virtual pages to physical frames.
  4. Page Fault: If a virtual page is not currently in RAM (i.e., it's on the hard disk), a page fault occurs.
  5. Disk Swapping: The OS retrieves the required page from the hard disk and loads it into a free frame in RAM. If no frames are free, the OS must choose a page in RAM to swap out to the hard disk (using a page replacement algorithm).

Benefits of Virtual Memory

  • Larger Programs: Allows programs to run even if they require more memory than is physically available.
  • Multitasking: Enables multiple programs to run concurrently, each with its own virtual address space.
  • Memory Protection: Provides memory protection between processes, preventing one process from accessing the memory of another.
  • Simplified Programming: Programmers don't need to worry about the physical memory layout.

Page Replacement Algorithms

When a page fault occurs and no free frames are available, the OS needs to choose a page to swap out. Several algorithms are used for this purpose:

Algorithm Description Advantages Disadvantages
First-In, First-Out (FIFO) Swaps out the page that has been in memory the longest. Simple to implement. Can lead to poor performance if a frequently used page is swapped out.
Least Recently Used (LRU) Swaps out the page that has not been used for the longest time. Generally performs well. More complex to implement.
Optimal (OPT) Swaps out the page that will not be used for the longest time in the future. Provides the best possible performance (but is not practical to implement). Requires knowledge of future page access.

Diagram

Suggested diagram: A diagram showing the relationship between virtual memory (program's view), physical memory (RAM), and the hard disk. Illustrate pages, frames, page table, and the MMU.

Key Terms

  • Virtual Address: An address used by the program.
  • Physical Address: An address in physical memory (RAM).
  • Page: A fixed-size block of virtual memory.
  • Frame: A fixed-size block of physical memory.
  • Page Table: A data structure that maps virtual pages to physical frames.
  • MMU: Memory Management Unit – hardware component that translates virtual addresses to physical addresses.
  • Page Fault: An event that occurs when a virtual page is not in RAM.