Module-4

Module 4: Memory Management

Memory management is a crucial aspect of operating systems that ensures efficient utilization of memory resources by subdividing memory to accommodate multiple processes. Effective memory allocation is fundamental to maximizing the number of processes that can concurrently execute, thereby optimizing overall system performance and resource use.

Memory Management Schemes

Contiguous Memory Management

In a contiguous memory management scheme, a program is allocated a single block of memory that occupies a continuous set of memory addresses. This approach improves access speed as data can be read sequentially from one single location in memory. There are two primary types of contiguous memory management:

  • Single Contiguous Management Schemes: Here, main memory is divided into two segments: one for the operating system (usually residing in lower memory) and the other for user processes. This method is straightforward but limits the ability to run multiple processes concurrently, as only one program can be executed at a time.

  • Multiple Partitioning: Single contiguous memory management is inefficient due to its limitation of executing one program only, leading to underutilized memory. Multiprogramming addresses this inefficiency by allowing multiple programs to run concurrently, effectively maximizing CPU time. Effective multiprogramming necessitates that when switching between processes, both processes must be loaded into main memory, leading to complex coordination of resource allocation.

Non-Contiguous Memory Management

In a non-contiguous memory management scheme, programs are divided into multiple blocks that can be loaded into disparate memory sections, allowing for greater flexibility without the need for physical adjacency. This method enhances memory utilization by allowing variable block sizes based on actual process need, which can reduce memory fragmentation and enhance overall efficiency.

Memory Management Requirements

Relocation

Programs in execution do not inherently know their physical memory locations. They may undergo swapping to and from disk storage, necessitating runtime address translation to the actual physical addresses where the program resides in memory.

Protection

To maintain security and isolation, processes must not reference memory locations belonging to other processes without permission. This requirement necessitates the implementation of robust access checks during execution to mitigate potential security breaches and protect data integrity.

Sharing

Memory sharing allows multiple processes to access shared resources, making it more efficient than maintaining separate copies for each process. This capability supports collaboration among processes and facilitates effective inter-process communication.

Logical and Physical Organization

Programs are organized modularly, allowing varying degrees of protection (accessible types like read-only and execute-only) that can be managed and shared among processes. Memory allocation must also be sufficient for both processes and their associated data, ensuring efficient execution without memory waste. Overlapping allows various modules to share the same memory region, enhancing resource utilization. The use of secondary memory for data that is not currently in use is a viable strategy to manage physical memory limitations.

Partitioning Techniques

Fixed Partitioning

In fixed partitioning, memory is subdivided into equal-sized segments, with each program occupying an entire partition regardless of its actual size. This approach can lead to internal fragmentation, where allocated partitions may contain unused memory that is not utilized by any process.

  • Unequal-size Partitions: This technique introduces partitions of varying sizes to reduce the inefficiencies associated with fixed partitioning, allowing better customization of resource allocation based on process requirements.

Dynamic Partitioning

Dynamic partitioning introduces variability in partition sizes. A process is allocated only the memory it requires, which enhances efficiency but can lead to external fragmentation. Periodic compaction of memory may be required to consolidate free memory spaces and counteract fragmentation issues.

Placement Algorithms

The method of allocating memory is determined by various placement algorithms:

  • First-fit: Allocates the first available memory block that fits the process size and searches from the start of memory.

  • Next-fit: Similar to first-fit but continues searching from the last allocation point, potentially improving speed in larger memory allocations.

  • Best-fit: Chooses the smallest block that fits, often leaving small fragments. This method can require frequent compaction as small unusable spaces accumulate over time.

  • Worst-fit: Allocates the largest available hole, leaving potentially useful smaller fragments behind.

Paging

What is Paging?

Paging is a memory management scheme that eliminates the need for contiguous allocation by dividing virtual memory into equal-size units called pages, and the main memory into equal-size units known as frames. A page table for each process maintains the mapping of page locations to frames, enabling efficient data retrieval.

Page Replacement

When a required page must be swapped in from secondary memory, the operating system must determine which existing page to evict from main memory. Common page replacement algorithms include:

  • First-In, First-Out (FIFO): Manages page replacement in a queue-like manner, replacing the oldest page first.

  • Optimal Replacement (OPT): Evicts the page that will not be used for the longest duration in the future, necessitating knowledge of future usage patterns.

  • Least Recently Used (LRU): Tracks the usage of pages, evicting the least recently referenced page to optimize memory access.

  • Not Frequently Used (NFU) and Least Frequently Used (LFU): Both rely on usage counting, with NFU employing counters to track access frequency, evicting the least used pages.

  • Random Replacement: Randomly replaces a page, which can yield performance that surpasses FIFO under certain conditions.

  • Working Set Algorithm: This algorithm focuses on preserving pages that are currently in use, evicting pages outside a defined working set, thus enhancing efficiency during page faults.