Memory Management in Operating Systems

1/28/20243 min read

Complete guide to memory management techniques and algorithms

operating-systemsmemorymanagementalgorithms

Memory Management in Operating Systems

Memory management is a crucial function of the operating system that handles allocation and deallocation of memory to processes.

Introduction

The OS must efficiently manage memory to ensure that processes have enough memory to execute while maximizing system performance.

Memory Hierarchy

  1. Registers: Fastest, smallest, most expensive
  2. Cache: Fast, small, expensive
  3. Main Memory (RAM): Medium speed, medium size
  4. Secondary Storage: Slow, large, cheap

Memory Allocation Techniques

Contiguous Memory Allocation

Processes are allocated contiguous blocks of memory.

Fixed Partitioning: Memory divided into fixed-size partitions

  • Simple to implement
  • Internal fragmentation

Variable Partitioning: Memory divided into variable-size partitions

  • No internal fragmentation
  • External fragmentation

Non-Contiguous Memory Allocation

Processes can be allocated non-contiguous blocks of memory.

Paging: Physical memory divided into fixed-size frames, logical memory into pages

  • No external fragmentation
  • Simple memory management

Segmentation: Memory divided into variable-size segments

  • Logical division
  • External fragmentation

Virtual Memory

Virtual memory allows processes to use more memory than physically available by using disk storage.

Paging with Virtual Memory

  • Page Table: Maps virtual pages to physical frames
  • Page Fault: When required page is not in memory
  • Page Replacement: Selecting which page to evict

Page Replacement Algorithms

FIFO (First In First Out)

Oldest page is replaced first.

Optimal Page Replacement

Replace page that will be used farthest in future (theoretical).

LRU (Least Recently Used)

Replace page that hasn't been used for longest time.

Clock Algorithm

Circular list with reference bit, second chance algorithm.

Memory Protection

  • Base and Limit Registers: Define address space boundaries
  • Memory-Mapped I/O: I/O devices mapped to memory addresses
  • Access Control: Read, write, execute permissions

Swapping

Moving processes between main memory and secondary storage.

Advantages:

  • Allows more processes than physical memory
  • Better memory utilization

Disadvantages:

  • High overhead
  • Slow performance

Memory Allocation Algorithms

First Fit

Allocate first block large enough.

Best Fit

Allocate smallest block large enough.

Worst Fit

Allocate largest available block.

Next Fit

Similar to first fit, but starts from last allocation point.

Fragmentation

Internal Fragmentation

Unused memory within allocated block.

External Fragmentation

Free memory blocks scattered throughout memory.

Solutions:

  • Compaction
  • Paging
  • Segmentation with paging

Modern Memory Management

  • ASLR (Address Space Layout Randomization): Security feature
  • Memory-Mapped Files: Files mapped to memory
  • Copy-on-Write: Efficient process creation
  • Memory Overcommit: Allocating more than available