3. MEMORY MANAGEMENT

 

Memory Management in Operating Systems

Memory management is a crucial function of an operating system (OS) responsible for managing the computer’s memory resources. It ensures efficient utilization of the system’s memory and provides the necessary mechanisms to allocate, track, and protect memory spaces used by various programs and processes.

1. What is Memory Management?

Memory management refers to the process of controlling and coordinating computer memory, assigning memory blocks to various running programs, and managing virtual memory. The goal is to optimize the use of memory, ensure fair allocation, and protect the memory spaces of different processes.

2. Objectives of Memory Management

  • Allocation: Allocate memory space to processes and deallocate it when no longer needed.
  • Protection: Ensure that one process does not access the memory space of another process.
  • Relocation: Provide flexibility to move processes in memory during execution.
  • Sharing: Enable processes to share the same memory spaces when necessary.
  • Logical and Physical Memory: Provide abstraction so that users interact with logical memory without worrying about its physical arrangement.

3. Memory Management Techniques

3.1 Single Contiguous Allocation

In this technique, all processes are loaded into a single continuous section of memory. There’s only one process in memory at a time, leading to inefficiency.

3.2 Fixed Partitioning

Memory is divided into fixed-sized partitions. Each partition can hold exactly one process. The partition size is defined at system startup.

  • Pros: Easy to implement and manage.
  • Cons: Can lead to internal fragmentation, where memory within a partition is unused.

3.3 Dynamic Partitioning

In dynamic partitioning, memory is allocated dynamically based on process needs. This technique solves internal fragmentation but introduces external fragmentation, where free memory is scattered in small blocks.

  • Pros: More efficient memory utilization.
  • Cons: Susceptible to external fragmentation, which can be mitigated using compaction (reorganizing memory to consolidate free blocks).

3.4 Paging

Paging is a memory management scheme that eliminates external fragmentation by dividing both the physical memory and logical memory into fixed-sized blocks called pages and frames.

  • Pages: Logical divisions of a program.
  • Frames: Corresponding divisions in physical memory. When a program is executed, its pages are loaded into available frames.

Illustration: Paging Mechanism

Paging-in-Operating-System
Logical Memory: [Page 1][Page 2][Page 3]... Physical Memory: [Frame 1][Frame 2][Frame 3]... Mapping: Page 1 -> Frame 3, Page 2 -> Frame 1, Page 3 -> Frame 2

Page Table: Maintains the mapping between pages and frames.

  • Pros: Eliminates external fragmentation and allows processes to use non-contiguous memory.
  • Cons: Introduces page table overhead, requiring additional memory for page tables.

3.5 Segmentation

Segmentation divides the program into different segments based on logical divisions (such as code, data, stack). Each segment has a varying size.

  • Segment Table: Maps each segment to its physical address in memory.
  • Pros: Provides logical division, which aligns with how programmers think about memory (code, data).
  • Cons: Leads to external fragmentation.

Illustration: Segmentation

Segmentation in OS (Operating System) - javatpoint
Logical Segments: Segment 1 (Code) -> Base Address 1000, Length 200 Segment 2 (Data) -> Base Address 3000, Length 100 Segment 3 (Stack) -> Base Address 5000, Length 50

3.6 Virtual Memory

Virtual memory is a memory management technique that allows processes to execute even if they are not fully loaded into physical memory. It uses both paging and segmentation mechanisms and relies on disk space (swap space) to extend memory.

  • Demand Paging: Only the needed pages are loaded into memory.
  • Page Replacement Algorithms: If physical memory is full, the OS uses algorithms to swap pages in and out of memory. Common algorithms include:
    • FIFO (First-In-First-Out): Replaces the oldest page.
    • LRU (Least Recently Used): Replaces the least recently accessed page.
    • Optimal: Replaces the page that won’t be used for the longest time (ideal but not practical in real systems).

Illustration: Virtual Memory with Demand Paging

less
Process Pages: [Page 1][Page 2][Page 3]... Physical Memory: [Frame 1: Page 1][Frame 2: Page 3]... Swap Space on Disk: [Page 2]

4. Memory Allocation Strategies

  • First-Fit: Allocates the first block of memory that is large enough for the process.
  • Best-Fit: Allocates the smallest block of memory that is large enough to fit the process (minimizes wasted space).
  • Worst-Fit: Allocates the largest available block (to create a large remainder, in the hope it can be used later).

5. Memory Protection

Memory protection ensures that processes do not interfere with each other’s memory. Some mechanisms include:

  • Base and Limit Registers: Used to define the valid memory addresses a process can access. A process cannot access addresses outside this range.
  • Protection Bits: Set at the page or segment level, indicating the type of access allowed (read, write, or execute).

6. Swapping

Swapping is a process where a process is moved temporarily from main memory to disk (swap space) and then brought back into memory when needed. This is used to optimize memory usage and allow multitasking.

7. Fragmentation

Fragmentation refers to wasted memory space. There are two types:

  • Internal Fragmentation: Occurs when allocated memory blocks are larger than the memory needed by a process, leaving unused space within a partition.
  • External Fragmentation: Occurs when free memory is scattered in small blocks across the system, making it difficult to allocate memory to new processes.

Illustration: External Fragmentation

External-Fragmentation-in-OS
Memory Layout: [Process A][Free Space][Process B][Free Space]... Cannot allocate a new large process even though enough free memory exists, but it’s scattered.

8. Compaction

To combat external fragmentation, compaction can be used. It involves rearranging the contents of memory to place all free memory together in one block, making it easier to allocate memory.

Illustration: Compaction

os Compaction
Before Compaction: [Process A][Free][Process B][Free][Process C]... After Compaction: [Process A][Process B][Process C][Free]...

Conclusion