Memory Management Notes

Memory Management Overview

Introduction

  • Memory management is a critical function in operating systems.

  • It addresses several aspects of how memory is allocated, used, and freed.

Need for Memory Management

  • Spatial Issues in Allocation:

    • Importance of effective allocation and freeing of memory.

  • Physical Memory Limitations:

    • DRAM (Dynamic Random Access Memory) is limited.

    • A single process may not fit entirely within available memory.

    • Multiple processes must fit in memory simultaneously.

  • Security Needs:

    • Prevent one process from accessing another's memory space.

Address Space of a Process

Segmentation of Memory

  • Components:

    • Code Segment: Contains executable instructions.

    • Data Segment: Holds global and static variables.

    • Heap Pointer (HP): Dynamically allocated memory area.

    • Stack Pointer (SP): Used for function call management (local variables, return addresses).

  • Growth Directions:

    • Heap grows towards higher addresses.

    • Stack grows towards lower addresses.

Dynamic Memory Allocation

Malloc() Function

  • Usage:

    • Allocates memory dynamically.

    • Example: P1 = malloc(100); requests 100 bytes.

  • Mechanism:

    • When malloc is called, if there is no space in the heap, a system call sbrk() is executed to move the end of the heap pointer.

  • Subsequent Allocations:

    • If additional memory requests (e.g., malloc(200), malloc(100)) are made and space is unavailable, sbrk() will again be invoked.

Memory Allocation Problem

Allocation Strategy

  • Goals:

    • Allocation must be contiguous for efficient access.

    • Allocation and freeing operations should be fairly efficient.

    • Handle requests so the total number of holes (free spaces) is minimized.

  • Solution Strategies:

    • Contiguous allocation:

    • Allocates a single contiguous block of memory.

    • Methods include first-fit, best-fit, worst-fit, buddy system, and slab allocation.

    • Non-contiguous allocation:

    • Physically non-contiguous but virtually contiguous allocation (using techniques such as paging).

  • Fragmentation Issues:

    • Can lead to external fragmentation when small holes are left over.

External Fragmentation and Compaction

Managing Fragmentation

  • Compaction:

    • Merging small memory holes to create larger contiguous spaces.

    • Can only be executed with relocatable code and is costly; should be used sparingly.

  • Allocation Strategies to Reduce Fragmentation:

    • First-fit: Use first available hole that fits the request but might lead to fragmentation.

    • Best-fit: Find the smallest sufficient hole; it reduces wasted space but can leave very small holes leading to eventual inefficiency.

    • Worst-fit: Pick the largest available hole to maintain larger available sizes; however, it can be inefficient due to excessive searching.

Freeing Memory

Free(p) Mechanism

  • Need to check neighboring regions during free operations to create larger available holes.

  • Strategy:

    • Maintain information about the allocated sizes and statuses of neighboring blocks.

    • Allocate extra space for headers managing sizes and status.

Free Cases

  • Case 1: Freeing memory between two free blocks.

  • Case 2: Freeing a block adjacent to one allocated on one side and free on the other.

  • Case 3: Freeing a block adjacent to two allocated blocks on both sides.

  • Case 4: Freeing a block surrounded entirely by allocated blocks.

Costs of Memory Operations

  • Freeing Memory Cost: Checking only neighboring blocks yields O(1) complexity.

  • Allocation Cost: Searching through lists of blocks results in O(N) complexity.

Efficient Strategies

  • Buddy System:

    • Utilizes a logarithmic strategy to manage block sizes and allocation.

    • Rounds up allocation requests to the next power of 2.

Slab Allocator Model

Overview

  • Continues allocation using larger chunks (known as slabs) with uniform sized objects.

  • Allows allocation without internal fragmentation by packing objects in a continuous manner.

  • Slab types match allocation sizes; new slabs are added as needed through buddy system.

  • Descriptor Table for Slab Management:

    • Contains type, size, number of objects, and allocation markers.

Conclusion

Summary of Key Concepts

  • Memory management involves dynamic allocation, fragmentation management, and structuring allocation strategies to minimize overhead and space wastage.

  • Various allocation strategies like first-fit, best-fit, worst-fit, along with advanced structures like buddy and slab allocations influence programming efficiency and memory utilization.