ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Operating Systems
    COMP3142
    Progress0 / 34 topics
    Topics
    1. Operating Systems Basics2. System Calls3. Process Concept and Scheduling4. Interprocess Communication5. Multithreaded Programming6. Multithreading Models7. Threading Issues8. Process Scheduling Algorithms9. Thread Scheduling10. Multiple-Processor Scheduling11. Synchronization12. Critical Section13. Synchronization Hardware14. Synchronization Problems15. Deadlocks16. Detecting and Recovering from Deadlocks17. Memory Management18. Swapping19. Contiguous Memory Allocation20. Segmentation and Paging21. Virtual Memory Management22. Demand Paging23. Thrashing24. Memory-Mapped Files25. File Systems26. File Concept27. Directory and Disk Structure28. Directory Implementation29. Free Space Management30. Disk Structure and Scheduling31. Swap Space Management32. System Protection33. Virtual Machines34. Operating System Security
    COMP3142›Virtual Memory Management
    Operating SystemsTopic 21 of 34

    Virtual Memory Management

    8 minread
    1,303words
    Intermediatelevel

    Virtual Memory Management in Operating Systems

    Virtual Memory is a memory management technique that creates an "idealized" abstraction of the storage resources that is actually larger than physical memory. It allows programs to access more memory than is physically available by using both physical memory (RAM) and secondary storage (such as hard disk or SSD). Virtual memory gives the illusion to the user and applications that there is a very large and continuous memory space, while physically, the memory may be fragmented and limited.

    Virtual memory is typically implemented through a combination of paging or segmentation, and demand paging techniques.


    Key Concepts of Virtual Memory

    1. Virtual Address Space:

      • Each process has its own virtual address space, which is the set of memory addresses it can use. This address space is divided into chunks like code, data, stack, and heap, which are all mapped to physical memory or secondary storage.
      • The virtual address space is typically much larger than the available physical memory (RAM).
    2. Physical Address Space:

      • This is the actual memory available in the system, made up of RAM and any other physical memory devices.
      • The operating system (OS) uses a page table to map virtual addresses to physical addresses.
    3. Page Table:

      • A page table is used to keep track of the mapping between the virtual memory addresses and the physical memory addresses.
      • The page table is maintained by the OS, and it translates virtual addresses generated by a process into physical addresses in memory.
    4. Memory Management Unit (MMU):

      • The MMU is a hardware component that helps in translating virtual addresses to physical addresses. It uses the page table to do this translation efficiently.
      • The MMU performs address translation on every memory access.
    5. Swap Space:

      • Swap space refers to a portion of secondary storage (typically a hard disk or SSD) that the OS uses to extend the available virtual memory when the physical memory is full.
      • When the system runs low on RAM, pages or segments of memory are moved to the swap space, making room for new data in physical memory.

    How Virtual Memory Works

    1. Address Translation: When a program accesses a memory location, the virtual memory address generated by the CPU must be translated into a physical memory address using the page table.

      • Virtual Address → Page Table → Physical Address.
      • The MMU performs this translation using the page table.
    2. Paging in Virtual Memory:

      • In paging, both the virtual memory and physical memory are divided into fixed-size pages (or frames in physical memory).
      • When a process accesses memory, its pages are mapped to available frames in physical memory. If a page is not currently in RAM (a page fault), the operating system swaps it from disk to memory.
    3. Demand Paging:

      • Demand paging is a technique where the OS does not load the entire process into memory at once. Instead, it loads only the pages that are currently needed by the program.
      • When a process tries to access a page that is not in memory, a page fault occurs, and the required page is loaded from the swap space on disk into RAM.
    4. Page Fault:

      • A page fault happens when a process tries to access a page that is not currently in physical memory. The OS will handle the page fault by loading the required page into memory from the swap space, and the process will be suspended until the page is loaded.
    5. Swap In and Swap Out:

      • Swap in: Moving a page or segment from secondary storage (disk or SSD) into physical memory (RAM).
      • Swap out: Moving a page or segment from physical memory to the swap space (secondary storage) to free up space in memory for other processes.

    Benefits of Virtual Memory

    1. Larger Address Space:

      • Virtual memory allows processes to access more memory than what is physically available in the system. This is especially useful in systems with limited physical RAM.
    2. Isolation of Processes:

      • Each process is given its own virtual address space, preventing it from directly accessing the memory of other processes. This ensures process isolation and security.
    3. Efficient Memory Usage:

      • Virtual memory allows the OS to allocate memory on demand (e.g., using demand paging), and pages that are not used can be swapped out to disk. This ensures that memory is used efficiently, even if it is oversubscribed.
    4. Process and System Efficiency:

      • Virtual memory increases the efficiency of system operation by allowing processes to run even if their entire memory requirements cannot fit into physical RAM at once.
    5. Protection:

      • Virtual memory can protect memory segments of different processes. The OS can prevent one process from reading or modifying the memory of another process, improving system stability and security.

    Challenges and Considerations with Virtual Memory

    1. Page Fault Overhead:

      • While virtual memory allows efficient memory usage, frequent page faults (where pages have to be loaded from disk to RAM) can significantly slow down performance. This is known as thrashing, where the system spends more time swapping data in and out of memory than executing processes.
    2. Disk I/O Overhead:

      • Swapping data between physical memory and disk storage incurs significant I/O overhead. Disk access is much slower than accessing memory, and excessive swapping can degrade system performance.
    3. Memory Fragmentation:

      • Even though virtual memory reduces external fragmentation by allowing non-contiguous allocation of memory, it can still suffer from internal fragmentation within pages if not all the space in a page is used.
    4. Hardware Requirements:

      • The implementation of virtual memory requires hardware support, such as the MMU and page tables, which adds complexity to the hardware and may increase the cost.

    Example of Virtual Memory Management with Paging

    Consider the following example:

    • Process P is executing and needs 6 pages of memory.
    • The physical memory has 4 available frames.
    Page Page Number Frame
    P1 1 Frame 0
    P2 2 Frame 1
    P3 3 Frame 2
    P4 4 Frame 3

    In this case:

    • Pages 1, 2, 3, and 4 are loaded into memory. The system is using paging to map each page to a frame in physical memory.
    • Pages 5 and 6 are not in memory. If a process needs to access pages 5 or 6, a page fault occurs, and the OS will swap out a page in memory and bring in the required page from disk.

    Page Replacement Algorithms

    When the physical memory is full, the OS must decide which page to swap out to make space for new pages. Several page replacement algorithms help in making this decision:

    1. First-In-First-Out (FIFO):

      • Pages are replaced in the order they were brought into memory. The first page to be loaded is the first to be replaced.
    2. Least Recently Used (LRU):

      • The page that has not been used for the longest time is swapped out.
    3. Optimal Page Replacement:

      • The page that will not be used for the longest period in the future is swapped out (ideal, but not practical in real systems as it requires future knowledge).
    4. Clock Algorithm:

      • A more efficient approximation of LRU, using a circular list of pages and a "clock hand" to determine which page to swap out.

    Conclusion

    Virtual Memory Management is a crucial component of modern operating systems that enables efficient memory utilization, isolation of processes, and the illusion of a larger address space than physically available. By leveraging paging and demand paging along with swap space, virtual memory enables systems to run large and complex applications even with limited physical memory. However, while virtual memory provides many advantages, it also introduces challenges such as page faults, I/O overhead, and the potential for thrashing, which need to be managed carefully by the OS.

    Previous topic 20
    Segmentation and Paging
    Next topic 22
    Demand Paging

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,303
      Code examples0
      DifficultyIntermediate