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›Memory Management
    Operating SystemsTopic 17 of 34

    Memory Management

    8 minread
    1,307words
    Intermediatelevel

    Memory Management in Operating Systems

    Memory management is one of the core functions of an operating system (OS). It involves managing the computer's memory resources, such as RAM (Random Access Memory) and cache, to ensure that each process in the system has sufficient memory space to execute and to optimize the performance of the system. Efficient memory management ensures the system runs smoothly, minimizes resource conflicts, and prevents memory-related issues like fragmentation.

    Objectives of Memory Management

    • Fair allocation of memory: Ensures that each process gets the memory it needs without unfair allocation or starvation.
    • Optimal use of memory: Maximize the use of the available physical memory.
    • Protection: Ensures that processes do not interfere with each other’s memory spaces.
    • Efficiency: Reduces memory fragmentation and overhead in managing memory allocation.
    • Security: Prevent unauthorized access to memory regions by different processes.

    Memory Management Concepts

    1. Memory Hierarchy:
      Memory management operates across multiple levels of memory, from high-speed registers to high-capacity storage (e.g., hard disks). The OS must manage the transition between different types of memory, ensuring the fastest and most efficient access to data.

    2. Memory Allocation:
      Memory allocation refers to the process of assigning memory to different processes in the system. It can occur in two ways:

      • Static Allocation: Memory is allocated at compile time, and it remains fixed throughout the process’s execution.
      • Dynamic Allocation: Memory is allocated at runtime, typically from a pool of free memory, and can be adjusted as processes grow or shrink.
    3. Memory Protection:
      To ensure that processes do not accidentally or maliciously access each other's memory, the OS isolates the memory regions of each process using hardware features like base and limit registers, and virtual memory.

    4. Virtual Memory:
      Virtual memory allows processes to operate in an address space that is larger than the physical memory. It gives the illusion that the system has more RAM than it physically does by using paging and segmentation techniques, in which part of the program is stored on secondary storage (disk) and brought into physical memory when needed.

    5. Memory Mapping:
      Memory mapping refers to the way the operating system manages access to memory, including mapping files, devices, or memory regions into the process’s address space. This helps make the process management more efficient.


    Types of Memory Management Systems

    1. Contiguous Memory Allocation

    In this approach, processes are allocated a single contiguous block of memory from the available physical memory. There are two main strategies in contiguous memory allocation:

    • Fixed Partitioning: The memory is divided into fixed-size partitions. Each partition can hold one process. However, fixed partitioning may lead to internal fragmentation (unused space within a partition) and underutilization of memory.

    • Dynamic Partitioning: The system divides memory dynamically at runtime based on process requirements. Each process is allocated memory as per its needs. This method reduces fragmentation but can suffer from external fragmentation.

    Problems:

    • Internal Fragmentation: Wasted memory within a partition.
    • External Fragmentation: Free memory is scattered in small blocks across the system, leading to inefficient memory utilization.

    2. Paging

    Paging is a memory management scheme that eliminates the problem of external fragmentation by breaking memory into fixed-size blocks, known as pages. The physical memory is divided into frames, and the virtual memory is divided into pages. The operating system maintains a page table to map pages to frames.

    • Page Table: A data structure that keeps track of where each page is stored in physical memory.
    • Page Fault: Occurs when a process tries to access a page that is not currently in physical memory, causing the OS to load the page from disk into RAM.

    Benefits of Paging:

    • Eliminates external fragmentation.
    • Provides efficient memory use, as any free frame can hold any page.
    • Allows processes to be loaded into any available space.

    Disadvantages:

    • Internal Fragmentation: Occurs when the last page is partially filled.
    • Overhead: Maintaining page tables requires extra memory and processing time.

    3. Segmentation

    Segmentation divides a process’s memory into variable-sized segments based on the logical divisions of the program, such as code, data, and stack. Each segment has a base address and a length.

    • Logical Division: Each segment is a distinct part of the program’s logical structure, rather than being divided into fixed-size pages.
    • Segment Table: A table that holds the base and limit information for each segment.

    Advantages of Segmentation:

    • More intuitive and closer to the way programs are logically structured (e.g., separating code and data).
    • Reduces internal fragmentation as segments are of variable size.

    Disadvantages:

    • External Fragmentation: Since segments vary in size, there may be unused spaces between segments.
    • Complexity: Segment management can be more complicated than paging.

    4. Virtual Memory

    Virtual memory combines paging and segmentation and provides the illusion that the computer has more RAM than it physically does. It allows processes to run without needing to fit entirely in physical memory, using disk space (secondary storage) to swap data in and out of memory as needed.

    Key Concepts in Virtual Memory:

    • Swap Space: A portion of the hard drive set aside to store pages or segments of processes that are not currently in use.
    • Page Replacement Algorithms: When a page is not in physical memory and a new page needs to be loaded, the system must choose which page to evict. Common algorithms include FIFO (First-In-First-Out), LRU (Least Recently Used), and Optimal Page Replacement.

    Benefits:

    • Enables large programs to run on systems with limited physical memory.
    • Provides isolation between processes.

    Challenges:

    • Page Faults: Every time a page is needed but not in memory, a page fault occurs, which can degrade performance.
    • Thrashing: Excessive paging activity caused by not having enough physical memory to meet process demands, leading to a performance degradation.

    Memory Allocation Strategies

    • First-Fit: Allocates the first available memory block large enough to satisfy the request.
    • Best-Fit: Allocates the smallest available memory block that is large enough to satisfy the request, minimizing wasted space.
    • Worst-Fit: Allocates the largest available memory block, leaving as much remaining free space as possible.

    Each of these strategies has trade-offs in terms of efficiency and fragmentation. Best-fit is ideal for minimizing wasted space, but it often results in smaller, unusable fragments. First-fit is faster, but it can leave larger chunks of unusable memory.


    Memory Management Challenges

    1. Fragmentation:

      • Internal Fragmentation: When allocated memory is larger than what is needed by a process, leading to wasted space within the allocated block.
      • External Fragmentation: When free memory is scattered in small chunks across the system, preventing the allocation of larger contiguous blocks even though enough memory might be available.
    2. Thrashing:

      • Thrashing occurs when the system spends too much time swapping data between physical memory and disk, rather than executing processes. This can happen when the system is overloaded and not enough memory is available to support the processes’ needs.
    3. Memory Leaks:

      • Memory leaks occur when a process fails to release memory it no longer needs, leading to gradual loss of memory resources over time.
    4. Protection and Isolation:

      • Ensuring that one process cannot access or modify the memory space of another process is critical for system stability and security. The OS must protect processes' memory spaces and isolate them from each other.

    Conclusion

    Memory management is fundamental for the operating system's performance and stability. It involves managing both the physical and virtual memory to ensure that processes have enough memory to run efficiently while maintaining protection and isolation. Techniques such as paging, segmentation, and virtual memory help in efficiently utilizing memory and supporting multitasking. However, challenges such as fragmentation, thrashing, and memory leaks must be addressed to ensure that the system runs smoothly and efficiently.

    Previous topic 16
    Detecting and Recovering from Deadlocks
    Next topic 18
    Swapping

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