Skip to content

Virtual Memory

Introduction

Virtual memory is a memory management technique that allows a process to execute even if it is partially loaded into the main memory (RAM). It creates an illusion of a large address space by using both RAM and disk storage.

πŸ“Œ Key Benefits of Virtual Memory:
βœ” Allows execution of large programs.
βœ” Efficient use of RAM.
βœ” Enables multiprogramming.
βœ” Reduces the problem of memory fragmentation.


Basic Concepts of Demand Paging

Demand paging is a technique where pages of a process are loaded into memory only when needed. Instead of loading the entire process at once, the OS loads pages on demand, reducing memory usage.

How Demand Paging Works?

  1. The process starts execution, but only the required pages are loaded into RAM.
  2. If a page is not available in memory, a page fault occurs.
  3. The OS fetches the missing page from disk (swap space) and loads it into RAM.
  4. Execution resumes after the page is available.

πŸ“Œ Example:

  • A large software like Photoshop only loads the basic UI at startup.
  • Additional features (e.g., filters, effects) are loaded on demand when used.

βœ… Advantage: Reduces RAM usage by loading only necessary pages.
❌ Disadvantage: If too many page faults occur, performance degrades (thrashing).


Page Replacement Algorithms

When RAM is full, the OS must replace an existing page to make room for a new one. This decision is made using page replacement algorithms.

1. FIFO (First In, First Out) Page Replacement

βœ” The oldest page in memory is replaced first.
βœ” Simple and easy to implement.

πŸ“Œ Example:

  • If 4 pages fit in memory, and a 5th page needs to be loaded, the first page will be removed.

βœ… Advantage: Simple to implement.
❌ Disadvantage: May replace important pages (Belady’s anomaly).


2. LRU (Least Recently Used) Page Replacement

βœ” The least recently used page is replaced first.
βœ” More efficient than FIFO.

πŸ“Œ Example:

  • If page A has not been used for a long time, while page B was recently accessed, then page A is replaced.

βœ… Advantage: Reduces unnecessary page faults.
❌ Disadvantage: Requires extra hardware support (tracking last access time).


3. Optimal Page Replacement

βœ” Replaces the page that will not be used for the longest time in the future.
βœ” Theoretically the best algorithm but not practical (requires future knowledge).

πŸ“Œ Example:

  • If the OS knows future page requests, it removes the page that won’t be used for the longest time.

βœ… Advantage: Lowest number of page faults.
❌ Disadvantage: Not feasible in real-world scenarios.


4. LFU (Least Frequently Used) Page Replacement

βœ” Replaces the least frequently accessed page.
βœ” Tracks page usage count to make decisions.

πŸ“Œ Example:

  • If a page was loaded but never used, it will be replaced first.

βœ… Advantage: Useful in applications where some data is rarely accessed.
❌ Disadvantage: High overhead in tracking page frequency.


Comparison of Page Replacement Algorithms

AlgorithmConceptBest Used WhenDrawback
FIFOOldest page replaced firstSimple implementationsCan remove frequently used pages (Belady’s anomaly)
LRULeast recently used page replacedWhen recent usage predicts future useRequires extra hardware for tracking usage
OptimalFuture-use-based replacementTheoretical best caseRequires knowing future memory requests
LFULeast frequently used page replacedWhen some pages are rarely usedHigh overhead in counting usage

Conclusion

Virtual memory improves system performance by managing memory efficiently. Demand paging ensures that only necessary pages are loaded, and page replacement algorithms help decide which pages to remove when memory is full. LRU and Optimal algorithms are preferred in real-world scenarios, but FIFO is simplest to implement.