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
    CSI-505
    Progress0 / 20 topics
    Topics
    1. History and Goals2. Evolution of Multi-User Systems3. Process and CPU Management4. Multithreading5. Kernel and User Modes6. Protection7. Problems of Cooperative Processes8. Synchronization9. Deadlocks10. Memory Management and Virtual Memory11. Relocation12. External Fragmentation13. Paging and Demand Paging14. Secondary Storage15. Security and Protection16. File Systems17. I/O Systems18. Introduction to Distributed Operating Systems19. Scheduling and Dispatch20. Introduction to Concurrency
    CSI-505›Memory Management and Virtual Memory
    Operating SystemsTopic 10 of 20

    Memory Management and Virtual Memory

    8 minread
    1,433words
    Intermediatelevel

    Memory Management and Virtual Memory in Operating Systems

    Memory management is a fundamental part of an operating system that involves the allocation, tracking, and management of the computer's memory resources. Since multiple processes and programs may be running simultaneously, the operating system must ensure that each process gets the necessary memory without interfering with others.

    Virtual memory is a technique that allows the execution of processes to be independent of the available physical memory, enabling more efficient use of memory and allowing programs to use more memory than is physically available.

    1. Memory Management Overview

    Memory management in an operating system involves a series of processes and policies designed to optimize the use of the physical memory (RAM) and virtual memory, ensuring the smooth operation of multiple programs and processes.

    The primary tasks of memory management include:

    • Allocation: Deciding which part of the physical memory will be assigned to which process.
    • Tracking: Keeping track of which memory locations are in use and which are free.
    • Deallocation: Releasing memory when it is no longer needed.
    • Protection: Ensuring that processes do not interfere with each other’s memory space.
    • Swapping: Moving data between physical memory and disk storage.

    2. Components of Memory Management

    • Physical Memory (RAM): The actual hardware memory where programs are loaded for execution. It is limited and must be allocated efficiently.
    • Logical (Virtual) Address Space: Each process has its own virtual address space. The operating system maps virtual addresses to physical addresses in RAM.
    • Memory Allocators: Software components (e.g., heap, stack, free list) used to allocate memory to processes dynamically during execution.

    3. Memory Allocation Techniques

    Memory allocation refers to the process of assigning portions of memory to programs and processes. There are several techniques used to allocate and deallocate memory:

    a) Contiguous Memory Allocation

    In this technique, each process is allocated a single contiguous block of memory in the physical memory. This is the simplest and most straightforward memory allocation method, but it has drawbacks:

    • Fragmentation: Over time, the memory becomes fragmented, leaving small unusable gaps between allocated areas.
    • Inefficient use of memory: The system may not be able to use all available memory due to fragmentation, even though there may be free memory.

    b) Paging

    Paging divides physical memory into fixed-size blocks called pages and divides the logical memory of a process into blocks of the same size, called page frames. Pages are loaded into available frames, and the operating system uses a page table to keep track of which virtual page is loaded into which physical frame.

    • Page Table: A data structure used to store the mapping between virtual addresses and physical addresses.
    • Advantages: Paging helps eliminate external fragmentation and allows the operating system to allocate memory in non-contiguous blocks.

    c) Segmentation

    Segmentation divides memory into segments of varying sizes, such as code, data, and stack segments. Unlike paging, the segments are logical units of the program rather than fixed-size blocks. Each segment is assigned a segment table to map virtual addresses to physical addresses.

    • Advantages: Segmentation allows for more flexible and efficient allocation compared to paging, as it respects the logical structure of a program.

    d) Paged Segmentation

    This is a hybrid approach combining paging and segmentation. The program is divided into segments, and each segment is further divided into pages. This helps combine the advantages of both paging (eliminating external fragmentation) and segmentation (providing logical separation of program components).

    4. Virtual Memory

    Virtual memory is a technique that allows the operating system to present the illusion of a larger memory space than is actually available in physical RAM. Virtual memory enables a system to run programs that require more memory than what is physically installed in the machine.

    • Virtual Memory Addressing: Each process is given the illusion of having its own continuous address space, even though the memory may be fragmented and stored across various locations in physical memory and secondary storage (disk).

    • Page Fault: When a process accesses a page that is not currently in physical memory (RAM), a page fault occurs. The operating system then fetches the required page from disk and loads it into memory. If the memory is full, the operating system must swap out a page to free space.

    • Swap Space: This is a dedicated area on the hard disk or SSD used to store pages that are not currently in memory. When the physical memory is full, pages are moved between physical memory and swap space.

    5. How Virtual Memory Works

    1. Address Translation: Virtual addresses generated by the CPU are mapped to physical addresses using a hardware mechanism called the Memory Management Unit (MMU). The MMU uses the page table (in paging) or segment table (in segmentation) to perform this translation.

    2. Page Table: A page table holds the mapping between a process’s virtual pages and the physical memory frames. If a page is not currently in memory, a page fault occurs, triggering the operating system to bring the page into memory from secondary storage.

    3. Demand Paging: With demand paging, pages are only loaded into memory when they are needed, reducing the amount of physical memory required at any time.

    4. Thrashing: If the system spends too much time swapping pages in and out of memory, a condition called thrashing occurs. This leads to poor performance because the system is constantly swapping pages between memory and disk, rather than executing processes.

    5. TLB (Translation Lookaside Buffer): To optimize memory access speed, modern systems use a small, fast cache called the TLB to store recent translations of virtual addresses to physical addresses. The TLB reduces the time it takes to look up the mapping in the page table.

    6. Page Replacement Algorithms

    When a page fault occurs and there is no available space in physical memory, the operating system must decide which page to remove from memory. Several page replacement algorithms are used to select the victim page:

    a) FIFO (First-In, First-Out)

    • This algorithm removes the page that has been in memory the longest.
    • Problem: FIFO may lead to poor performance because it does not take into account how often a page is used (i.e., a frequently used page could be removed just because it’s the oldest).

    b) LRU (Least Recently Used)

    • This algorithm removes the page that has not been used for the longest period of time.
    • Advantages: LRU is more efficient than FIFO because it keeps the most frequently used pages in memory.

    c) Optimal Page Replacement (OPT)

    • This algorithm removes the page that will not be used for the longest period of time in the future. It minimizes page faults, but it requires knowledge of future memory accesses, which is not practical in real systems.

    d) Clock Algorithm

    • The clock algorithm is a practical approximation of the LRU algorithm. Pages are organized in a circular list, and the algorithm uses a "hand" to check and replace pages in a circular manner.

    7. Advantages of Virtual Memory

    • Large Address Space: Virtual memory allows processes to use more memory than physically available, enabling larger applications to run on systems with limited RAM.
    • Isolation: Each process runs in its own virtual address space, which provides isolation and protection from other processes.
    • Efficient Resource Use: Virtual memory allows for more efficient utilization of physical memory by keeping only the necessary pages in RAM at any time.
    • No Need for Continuous Allocation: Processes can allocate memory without worrying about fragmentation or the physical layout of the memory.

    8. Challenges and Drawbacks of Virtual Memory

    • Performance Overhead: The process of swapping pages between physical memory and disk introduces overhead. Frequent page faults (due to insufficient physical memory) can severely degrade system performance.
    • Thrashing: If the system spends too much time swapping pages, the effective CPU utilization drops, causing a situation called thrashing.
    • Disk I/O: Since disk access is much slower than RAM access, the use of virtual memory can lead to significant delays if the system frequently accesses the disk for swapping.

    Conclusion

    Memory management and virtual memory are crucial aspects of modern operating systems, enabling efficient utilization of both physical and virtual memory. Memory management includes techniques such as contiguous memory allocation, paging, and segmentation, while virtual memory allows programs to run using more memory than physically available. Despite its benefits, virtual memory can introduce challenges like thrashing and performance degradation if not managed properly. Effective memory management is essential for maintaining system performance and reliability, particularly in multi-tasking environments.

    Previous topic 9
    Deadlocks
    Next topic 11
    Relocation

    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,433
      Code examples0
      DifficultyIntermediate