Chapter 7: Memory Management

Address Binding, Paging, Segmentation, Virtual Memory, Swapping, Fragmentation & Page Replacement

1. What is Memory Management?

Memory Management is the OS function that controls and coordinates use of the computer's main memory (RAM). It decides which process gets memory, how much, when to allocate or release it, and how to protect processes from each other.

ApplicationMemory Needed
Browser3 GB
IDE2 GB
Music Player500 MB
Antivirus1 GB
OS itself1.5 GB
Objective
Efficient utilization of memory
Protection between processes
Fast allocation and deallocation
Support multiprogramming
Maximize CPU utilization

2. Memory Hierarchy

Level (Fastest → Slowest)SpeedCost per BitCapacity
CPU RegistersHighestHighestLowest
CacheVery HighHighSmall
Main Memory (RAM)HighMediumMedium
SSD / HDDLowLowVery Large
Optical Disk / TapeLowestLowestLargest

📌 Exam Trick: Moving down the hierarchy — Speed ↓  |  Cost per bit ↓  |  Capacity ↑


3. Address Binding

Programs use symbolic addresses. These must be converted to actual memory addresses. This conversion is called Address Binding.

TypeWhenAdvantageDisadvantage
Compile-TimeDuring compilationSimple and fastCannot relocate program later
Load-TimeWhen program is loaded into memoryCan relocate before executionSlower startup
Execution-TimeWhile program is runningMost flexible; supports virtual memoryRequires MMU hardware support

📌 Most modern OS use Execution-Time binding. Memory Trick — CLE: C → Compile  |  L → Load  |  E → Execution

Memory Management Unit (MMU)

The MMU is hardware that converts logical (virtual) addresses into physical addresses.

Flow
CPU ↓ Logical Address ↓ MMU ↓ Physical Address ↓ RAM

4. Logical Address vs Physical Address

FeatureLogical AddressPhysical Address
Generated byCPUMMU
Also calledVirtual AddressReal Address
Visible toUser programsRAM hardware
PurposeProgram uses this addressActual location in RAM

5. Memory Allocation

MethodDescriptionAdvantageDisadvantage
Contiguous Allocation Each process occupies one continuous block of RAM Simple, fast access Causes fragmentation
Non-Contiguous Allocation Process divided into parts stored at different locations (Paging, Segmentation) Better utilization, flexible More complex; needs page/segment table

6. Fragmentation

Fragmentation means memory is wasted because of inefficient allocation.

TypeDefinitionExampleCommon In
Internal Fragmentation Unused space inside an allocated memory block Block = 8 KB, Process needs 6 KB → 2 KB wasted inside Fixed-size allocation (paging)
External Fragmentation Unused memory exists between allocated blocks; total free memory may be enough but not contiguous Process A | Free | Process B | Free | Process C Variable-size allocation (segmentation)

📌 Memory Trick: Inside = Internal  |  Outside (between) = External


7. Compaction

Compaction moves processes to make all free memory contiguous.

Before CompactionAfter Compaction
P1 | Free | P2 | Free | P3P1 | P2 | P3 | Free (one large block)
AdvantageDisadvantage
Removes external fragmentationTime-consuming; processes must pause

8. Paging

Paging divides logical memory into fixed-size pages and physical memory into frames of the same size. Pages are loaded into any available frame — no need for contiguous allocation.

TermIn Logical MemoryIn Physical Memory
PageFixed-size block of logical memory—
Frame—Fixed-size block of physical RAM
Page TableMaps each page number to a frame number

Page Table & Address Translation

Page NumberFrame Number
05
12
28
Address Translation Flow
Logical Address (Page Number, Offset) ↓ Page Table ↓ Frame Number ↓ Physical Address (Frame Number, Offset)
AdvantagesDisadvantages
Eliminates external fragmentationSmall internal fragmentation possible
Efficient memory utilizationPage table itself consumes memory

9. Segmentation

Segmentation divides memory into logical segments based on program structure (Code, Data, Stack, Heap). Each segment has a variable size.

Segment Table FieldMeaning
Base AddressStarting physical address of the segment
Limit (Size)Length of the segment
AdvantagesDisadvantages
Matches logical program structureSuffers from external fragmentation
Easier protection and sharingVariable-size segments harder to manage

Paging vs Segmentation

FeaturePagingSegmentation
Division sizeFixed-size pagesVariable-size segments
Division basisPhysical (hardware)Logical (program structure)
External fragmentationNonePossible
Internal fragmentationSmall amount possibleNone
Address formatPage number + OffsetSegment number + Offset

10. Virtual Memory

Virtual memory allows programs larger than physical RAM to execute by using disk space as an extension of memory.

ScenarioWithout Virtual MemoryWith Virtual Memory
RAM = 8 GB, Program = 20 GBCannot runRuns successfully
AdvantagesDisadvantages
Larger programs can runSlower than pure RAM access
Better multitaskingPage faults add overhead
Efficient RAM utilizationCan lead to thrashing if overused

11. Swapping

Swapping temporarily moves an entire process from RAM to disk (swap space) and brings it back later.

StepAction
1RAM becomes full
2Inactive process moved to disk (swap out)
3Memory becomes available for other processes
4Process brought back when needed (swap in)
AdvantageDisadvantage
Supports multiprogramming beyond RAM sizeDisk is much slower than RAM; high latency

12. Demand Paging

Pages are loaded into RAM only when they are actually needed, not all at program startup.

Advantages
Saves RAM — unused pages stay on disk
Faster program startup
Supports large programs on limited RAM

13. Page Fault

A page fault occurs when a requested page is not present in RAM.

StepAction
1CPU requests a page
2Page not found in RAM (miss)
3Page fault interrupt raised
4OS loads the required page from disk into RAM
5Program resumes execution

Frequent page faults slow the system significantly and can lead to thrashing.


14. Page Replacement Algorithms

When RAM is full and a new page is needed, the OS must select a victim page to remove. This is page replacement.

FIFO (First-In, First-Out)

The oldest page in memory is removed first.

AdvantageDisadvantage
Simple to implementMay suffer from Belady's Anomaly
Fair orderingDoes not consider how recently a page was used

Belady's Anomaly: Increasing the number of page frames can increase the number of page faults in FIFO (counterintuitive).

LRU (Least Recently Used)

Remove the page that has not been used for the longest time.

AdvantageDisadvantage
Generally performs better than FIFORequires tracking usage history (overhead)
No Belady's AnomalyMore complex to implement

Optimal Page Replacement

Replace the page that will not be used for the longest time in the future.

AdvantageDisadvantage
Produces minimum possible page faultsRequires future knowledge — not practical
Used as a theoretical benchmarkCannot be implemented in real systems

Algorithm Comparison

AlgorithmStrategyPerformancePractical?Belady's Anomaly?
FIFORemove oldest pageAverageYesYes
LRURemove least recently usedGoodYesNo
OptimalRemove page not needed longestBest (minimum faults)No (needs future info)No

📌 Memory Trick — FLO: F → FIFO  |  L → LRU  |  O → Optimal


15. Thrashing

Thrashing occurs when the system spends more time swapping pages than executing programs.

CausesSymptomsSolutions
Too many processes runningHigh disk activity (constant page reads/writes)Increase RAM
Insufficient RAM for active processesVery low CPU utilizationReduce multiprogramming level
Excessive page faultsSystem appears frozen / extremely slowUse better page replacement algorithm

Memory Protection

The OS protects memory using: Base Register, Limit Register, MMU, and Access Permissions. This ensures no process can access another process's memory.

MechanismPurpose
Base RegisterStores the starting address of a process's memory
Limit RegisterStores the size of a process's memory region
MMUChecks every memory access against base/limit registers
Access PermissionsRead / Write / Execute flags per region

Memory Tricks

MnemonicFull Form
Address Binding — CLE C → Compile-Time   L → Load-Time   E → Execution-Time
Fragmentation Inside = Internal  |  Outside (between blocks) = External
Page Replacement — FLO F → FIFO   L → LRU   O → Optimal
Memory Hierarchy — RCRSD Registers → Cache → RAM → SSD/HDD → Disk/Tape

Frequently Asked Exam Questions

QuestionAnswer
What is memory management?The OS function that manages allocation and deallocation of RAM
Difference between logical and physical address?Logical: generated by CPU; Physical: generated by MMU, used by RAM
Difference between paging and segmentation?Paging: fixed-size pages (physical division); Segmentation: variable-size logical segments
Internal vs external fragmentation?Internal: waste inside allocated block; External: waste between blocks
What is virtual memory?Technique using disk to extend RAM so larger programs can execute
What is a page fault?Requested page not present in RAM; OS loads it from disk
Optimal page replacement?Replace page not needed for longest future time — theoretical minimum faults
What is thrashing?System spends more time swapping pages than executing programs
What is Belady's Anomaly?In FIFO, adding more frames can increase page faults

Practice MCQs

  • 1. Which hardware converts logical addresses into physical addresses?
    A. ALU   B. Cache   C. MMU   D. PCB
    ✅ Answer: C — MMU (Memory Management Unit)
  • 2. Which fragmentation occurs inside an allocated memory block?
    A. External   B. Internal   C. Paging   D. Thrashing
    ✅ Answer: B — Internal Fragmentation
  • 3. Paging divides memory into:
    A. Segments   B. Frames and Pages   C. Blocks and Files   D. Tracks and Sectors
    ✅ Answer: B — Frames and Pages
  • 4. Which page replacement algorithm is theoretically optimal?
    A. FIFO   B. LRU   C. Optimal   D. Round Robin
    ✅ Answer: C — Optimal Page Replacement
  • 5. A page fault occurs when:
    A. RAM is damaged   B. Requested page is not in main memory   C. CPU overheats   D. Cache becomes full
    ✅ Answer: B — Requested page is not in main memory