Virtual memory

Virtual memory

Virtual memory is a memory management technique that allows a computer to use more memory than what is physically available in RAM. It creates an illusion for the user that there's more RAM than there actually is by using a combination of the computer's RAM and a portion of the hard drive. 

Imagine you have a computer with 4 GB of RAM, but you're running several programs that together require 6 GB of memory. The operating system will use 4 GB from the physical RAM and then take 2 GB from the hard drive, which acts as an extension of the RAM. This portion of the hard drive is called the swap space or page file. Here's how it works:

  • Running Programs: Suppose you're using a browser, a word processor, and a game simultaneously.
  • Memory Requirement: These applications together need more memory than your 4 GB of RAM can provide.
  • Swapping: The OS decides which parts of the data or applications are not actively in use and temporarily moves them from RAM to the swap space on the hard drive.
  • Accessing Data: When the data in the swap space is needed again, the OS will swap it back into RAM, possibly swapping out some other data to the hard drive. 
Advantages of Virtual Memory:
  • Efficient Use of Memory: Virtual memory allows the operating system to use disk space as an extension of RAM, enabling more processes to run simultaneously, even if they collectively require more memory than is physically available.
  • Larger Address Space: It provides processes with a larger address space than the physical memory, enabling the execution of large applications that wouldn’t fit entirely in RAM.
Disadvantages of Virtual Memory:
  • Slower Performance: Accessing data from the hard drive (which is used in virtual memory) is much slower than accessing data from RAM, which can lead to significant performance drops, especially if the system heavily relies on swapping.
  • Increased Complexity: Managing virtual memory adds complexity to the operating system, including the need for sophisticated algorithms for page replacement and memory allocation.
  • Potential for Thrashing:  If the system relies too heavily on swapping, it can lead to thrashing, where the OS spends more time swapping pages in and out of memory than executing processes, resulting in severe performance degradation.
Demand paging 
It is a technique in virtual memory management where pages of data (sections of a program) are loaded into RAM only when they are needed (or "demanded") by a process. Instead of loading the entire program into memory at once, the operating system loads only the pages that are currently required.
How Demand Paging Works:
  • Process Starts: When you start a program, instead of loading the entire program into RAM, the OS only loads the essential parts needed to begin execution.
  • Page Fault: If the program needs to access a page that isn't currently in RAM (because it hasn't been loaded yet), a "page fault" occurs. This is an event that tells the OS to fetch the needed page from the hard drive (or swap space) and load it into RAM.
  • Loading the Page: The OS then retrieves the required page from the disk and loads it into RAM. The program can then continue running as if the page had been there all along.
  • The valid-invalid scheme in demand paging is a method used by the operating system to manage and track which pages of a process's memory are currently in RAM and which are not. 
  • Valid Bit: The page table entry has a valid bit that indicates whether the page is currently in RAM.  
  • Valid (1): The page is in RAM, so the CPU can directly access it. 
  • Invalid (0): The page is not in RAM; it might be on the disk (e.g., in a swap file) or not allocated yet.

What is a Page fault?

A page fault happens when a program tries to access a part of its memory (a page) that is not currently in the computer's RAM. This usually occurs because the needed page is stored on the hard drive or hasn't been loaded into RAM yet.

How is a Page Fault Generated:
  • Program Execution: A program (process) runs and tries to access some data.
  • This data is in a specific page of the program's memory.
  • Memory Access: The CPU checks if the page is in RAM by looking at the page table, which keeps track of where each page is stored.
  • Page Table Check: If the page is not in RAM (the valid bit in the page table is set to 0), a page fault occurs because the CPU can't find the page in the memory.
How is a Page Fault Handled:
  • Pause the Program: When a page fault occurs, the operating system pauses the program because it needs to bring the missing page into RAM.
  • Locate the Page: The operating system finds where the needed page is stored on the hard drive.
  • Load the Page into RAM: The OS loads the page from the hard drive into an available space in RAM.
  • Update the Page Table: The page table is updated to reflect that the page is now in RAM (the valid bit is set to 1).
  • Resume the Program: The program is resumed from where it left off, and it can now access the data it needs from RAM. 

Comments

Popular posts from this blog

Indexed allocation

Thrashing

Basics of paging