Skip to main content

Virtual Memory

Virtual Memory
Virtual Memory

Virtual memory creates the illusion for users that they have more RAM than is physically available. This technique allows a computer to compensate for shortages of physical memory by temporarily transferring data from RAM to disk storage.

How Virtual Memory Works​

  1. Assume the RAM is already executing pages from processes P1 (blue) and P2 (red).

  2. A new process, P3, requests execution and needs its first two pages loaded.

  3. Page 1 of P3 is mapped to Frame 2 in RAM.

  4. The OS detects that there is no free frame for Page 2 of P3. It then selects a page (using a replacement algorithm) from P1 or P2 to store on the disk (swap space).

  5. Suppose the OS moves a page from P1 (previously mapped to Frame 1) to the disk.

  6. Now, Page 2 of P3 can be mapped to Frame 1.

  7. Any remaining pages of P3 are stored on the disk until they are needed.

Even though RAM was initially full, the system managed to execute a new process (P3) by moving some older or less frequently used pages to disk storage. The area of the disk used for storing these pages is known as the swap space.

Advantages of Virtual Memory​

  • Execute Larger Programs: Programs larger than the available RAM, such as high-demand games or applications like GTA5, can still be executed efficiently.

  • Increased Multiprogramming: Virtual memory allows more processes to be loaded into memory simultaneously, increasing the system's degree of multiprogramming.

Disadvantages of Virtual Memory​

  • Performance Overhead: Frequent swapping of pages between the disk and RAM can slow down the system.

  • Risk of Thrashing: Excessive swapping can lead to thrashing, where the system spends more time swapping pages than executing actual processes.

Demand Paging​

Demand Paging
Demand Paging

Demand paging loads pages into memory only when they are required by a running process, minimizing the physical memory footprint.

How Demand Paging Works​

Demand paging waits to load a page into RAM until a page fault occurs (i.e., when the process tries to access a page that is not currently in RAM). Additionally, demand paging may load some essential pages preemptively, anticipating that they will be needed soon. For example, when a new process starts, demand paging might load the page containing the program's entry point (like the main function in C/C++).

Valid/Invalid Bit​

To manage which pages are in RAM and which are not, the page table uses a valid/invalid bit:

  • Valid (1): The page is in RAM.

  • Invalid (0): The page is not in RAM and is stored in the swap space on the disk.

Page Fault​

A page fault occurs when a process tries to access a page that is not currently in the main memory (RAM). This triggers the operating system to load the required page from the disk's swap space into a free frame in RAM.

How Page Faults Are Handled​

  1. The OS identifies that the requested page is not in RAM by checking the valid/invalid bit.

  2. The OS selects a free frame (or uses a replacement algorithm to free one) and loads the required page from the swap space.

  3. The page table is updated to reflect the new location of the page in RAM, and the valid/invalid bit is set to 1.

A page fault acts as a signal or exception that prompts the OS to bring the missing page into memory.

Pure Demand Paging​

Pure demand paging is a stricter version of demand paging where no pages are loaded into RAM until a page fault occurs.

Characteristics of Pure Demand Paging​

  • When a new process starts, no pages are preloaded. All pages are marked as "not in memory."

  • Pages are only loaded in response to a page fault, making pure demand paging more efficient in terms of memory usage but potentially slower in terms of initial execution time.

Advantages of Pure Demand Paging​

  • Memory Efficiency: Only the necessary pages are loaded, reducing memory usage.

  • Reduced Load Time: Initial process load times may be quicker since only essential pages are loaded as needed.

Disadvantages of Pure Demand Paging​

Potentially Higher Latency: Page faults cause delays as pages are loaded on-demand, which might affect performance if page faults occur frequently.