Memory Management Strategies

Definition

Memory Management Strategies are different approaches operating systems use to allocate memory to processes, track usage, and reclaim memory. Different strategies have different trade-offs between simplicity, efficiency, and flexibility. The OS must manage main memory (RAM) to support multiple processes with limited capacity.

Goals of Memory Management

1. Efficiency

Maximize memory utilization:

  • Minimize wasted space
  • Support many processes simultaneously
  • Minimize context switching

2. Protection

Isolate processes from each other:

  • Process A can’t access Process B’s memory
  • Prevent accidental/malicious corruption

3. Relocation

Processes should run anywhere in memory:

  • Don’t depend on specific addresses
  • Enable memory compaction

4. Sharing

Allow multiple processes to share code:

  • Shared libraries (single copy for many programs)
  • Shared data structures

Major Memory Management Strategies

Strategy 1: Monolithic Approach

Concept: OS loads at fixed location, rest of RAM for processes

Oldest approach (1960s computers)

Memory Layout:
┌──────────────┐ (0)
│    OS        │
├──────────────┤ (50KB)
│              │
│ Processes    │
│              │
└──────────────┘ (Max)

Problems:

  • ❌ No protection (processes can overwrite OS)
  • ❌ No relocation (must load at specific address)
  • ❌ Single process at a time (or no isolation)

Historical only

Strategy 2: Partitioned Memory (Fixed/Dynamic)

Concept: Divide memory into partitions for processes

Fixed Partitioning

OS pre-divides memory into fixed-size partitions:

Memory Layout:
┌──────────────┐
│    OS        │
├──────────────┤ Partition 1: 1MB
│ Process 1    │
├──────────────┤ Partition 2: 2MB
│ Process 2    │
├──────────────┤ Partition 3: 2MB
│ Process 3    │
├──────────────┤ Partition 4: 4MB
│              │ (empty)
└──────────────┘

Advantages:

  • ✓ Simple to implement
  • ✓ Protection (process in partition can’t exceed boundary)
  • ✓ Multiple processes

Disadvantages:

  • Internal Fragmentation: Process uses 500KB partition with 2MB → 1.5MB wasted
  • Inflexible: Can’t change partition sizes without reboot
  • Poor utilization: If processes need 1.5MB and 3MB, but partitions are 2MB and 2MB

Dynamic Partitioning

OS allocates partitions dynamically as processes request:

Initial:
┌──────────────┐
│    OS        │
├──────────────┤
│              │
│  Free Space  │
│              │
└──────────────┘

Process A requests 2MB:
├──────────────┤
│    OS        │
├──────────────┤
│ Process A    │ 2MB (exactly)
├──────────────┤
│  Free Space  │
└──────────────┘

Process B requests 1.5MB:
├──────────────┤
│    OS        │
├──────────────┤
│ Process A    │
├──────────────┤
│ Process B    │ 1.5MB
├──────────────┤
│  Free Space  │
└──────────────┘

Advantages:

  • ✓ No internal fragmentation (allocate exact size)
  • ✓ Better utilization
  • ✓ Flexible

Disadvantages:

  • External Fragmentation: Free space fragmented
    Free blocks: 500KB, 300KB, 200KB (total 1000KB)
    Process needs 600KB → Can't fit in any single block!
    Must compact memory (expensive)
  • ❌ Overhead (track free/allocated blocks)

Strategy 3: Paging

Concept: Divide memory into fixed-size pages, virtual memory pages map to physical pages

Virtual Memory:          Physical Memory:
Page 0                   Frame 0
Page 1  ────────────→    Frame 1
Page 2                   Frame 2
Page 3  ────────────→    Frame 3
Page 4                   Frame 4
Page 5  ────────────→    (Free)

Advantages:

  • ✓ No external fragmentation (fixed-size units)
  • ✓ Virtual memory support (page to disk)
  • ✓ Simple management

Disadvantages:

  • ❌ Internal fragmentation (last page usually not full)
  • ❌ Page table overhead (track all pages)

(Covered in detail in separate topic)

Strategy 4: Segmentation

Concept: Divide memory into logical segments (code, data, stack)

Virtual Memory:
┌──────────────┐
│ Code (4KB)   │ → Physical Address 1000KB
├──────────────┤
│ Data (8KB)   │ → Physical Address 5000KB
├──────────────┤
│ Stack (2KB)  │ → Physical Address 9000KB
└──────────────┘

Advantages:

  • ✓ Logical organization (matches program structure)
  • ✓ Dynamic sizing (segment grows as needed)
  • ✓ Sharing (share code segment)

Disadvantages:

  • ❌ External fragmentation
  • ❌ Complex management

(Covered in detail in separate topic)

Strategy 5: Virtual Memory with Paging/Segmentation

Concept: Combine virtual memory with paging or segmentation to support programs larger than RAM

Key Idea:

  • Process sees large virtual address space
  • Only active pages in RAM
  • Rest on disk (paging)
Virtual Space: 4GB
┌────────────────────┐
│ Program            │ (4GB visible to process)
└────────────────────┘

Physical RAM: 256MB
├────────────┐
│ Active     │ (only what's used right now)
│ Pages in   │
│ RAM        │
├────────────┤
│  More      │ (rest on disk, paged in as needed)
│  Pages on  │
│  Disk      │
└────────────┘

Advantages:

  • ✓ Program can be larger than RAM
  • ✓ Good utilization
  • ✓ Flexibility

Disadvantages:

  • ❌ Disk I/O slow (page faults)
  • ❌ Complex management

(Most common in modern systems)

Memory Allocation Algorithms

First Fit

Find first free block large enough:

Free blocks: [500KB, 300KB, 200KB, 600KB]
Process needs: 350KB
Use first block with 500KB (waste 150KB)

Fast but wastes space

Best Fit

Find smallest free block that fits:

Free blocks: [500KB, 300KB, 200KB, 600KB]
Process needs: 350KB
Use 500KB (waste only 150KB)
Not 600KB (would waste 250KB)

Better than First Fit

Worst Fit

Find largest free block:

Free blocks: [500KB, 300KB, 200KB, 600KB]
Process needs: 350KB
Use 600KB (waste 250KB)
Reasoning: Leave smaller blocks for small processes

Worst performance in practice

Comparison of Strategies

StrategyFragmentationProtectionSharingRelocationComplexity
MonolithicBadNoNoNoVery Low
Fixed PartitionInternalYesLimitedLimitedLow
Dynamic PartitionExternalYesYesYesMedium
PagingLittle InternalYesYesYesMedium
SegmentationExternalYesYesYesHigh
Virtual MemoryLittle InternalYesYesYesHigh

Modern Approach

Most modern systems use Virtual Memory with Paging (or combination with segmentation):

Windows: Paging + Segmentation
Linux: Paging (segmentation disabled)
macOS: Paging
Modern CPUs: Hardware support for paging

Why:

  • ✓ No fragmentation (paging)
  • ✓ Logical organization (segmentation)
  • ✓ Flexibility (virtual memory)
  • ✓ Hardware support (TLB, page tables)

Summary

Memory management strategies range from simple (fixed partitions) to complex (virtual memory with paging). Fixed partitions cause internal fragmentation. Dynamic partitions cause external fragmentation requiring compaction. Paging eliminates external fragmentation but has internal fragmentation. Segmentation provides logical organization but external fragmentation. Modern systems combine paging with virtual memory to support large programs in limited RAM. Choice depends on hardware capabilities, system requirements, and performance needs.