Operating SystemsUnit 413 min read
Memory Management – Allocation, Paging, Segmentation & Replacement Strategies
Unit 4 of Operating Systems: this note explains memory allocation concepts, fragmentation, paging, segmentation, virtual memory, page‑replacement algorithms, and practical calculations, providing definitions, mechanisms, examples, tables and exam‑focused tips.
Key points
- Internal fragmentation occurs inside allocated blocks, while external fragmentation leaves unusable gaps between blocks.
- Multiprogramming raises CPU utilization by overlapping I/O wait times; utilization can be computed with the formula \(U = 1 - p^n\).
- Optimal Page Replacement (OPR) yields the fewest page faults but is impossible to implement because it needs future reference knowledge.
- Common replacement policies (FIFO, LRU, Clock) trade off implementation cost against fault rate; their behavior can be traced with reference strings.
- Buddy system and paging both eliminate external fragmentation, but paging introduces internal fragmentation.
- Understanding the working‑set model helps prevent thrashing and maintain system performance.
1. Introduction to Memory Management
Memory management is the OS component that controls the allocation and deallocation of primary memory to processes, ensuring efficient use of the limited physical RAM while providing the illusion of a large, contiguous address space. The main objectives are:
- Protection – isolate processes so one cannot corrupt another’s data.
- Sharing – allow multiple processes to access common code or data.
- Efficiency – minimize wasted memory (fragmentation) and maximize CPU utilization.
2. Memory Allocation Schemes
2.1 Fixed‑size Partitioning
- The memory is divided into a small number of fixed partitions at boot time.
- Each partition can hold exactly one process.
- Advantages: simple implementation, low overhead.
- Disadvantages: severe internal fragmentation; limited flexibility.
2.2 Variable‑size Partitioning
- Partitions are created dynamically to fit the size of incoming processes.
- Allocation strategies determine which free hole a process receives.
| Strategy | Description | Typical Use | Fragmentation |
|---|---|---|---|
| First‑Fit | Scan from the beginning; allocate the first hole large enough. | Fast, good for low‑traffic systems. | May leave many small holes (external). |
| Best‑Fit | Scan entire list; allocate the smallest hole that fits. | Minimizes leftover space per allocation. | Produces many tiny holes → high external fragmentation. |
| Worst‑Fit | Allocate the largest available hole. | Attempts to leave large holes for future big processes. | Can waste large amounts of memory. |
Worked Example – First‑Fit Allocation
Memory size = 100 KB, free holes: 20 KB, 15 KB, 30 KB, 10 KB, 25 KB.
Process P (size = 18 KB) arrives.
- Scan: first hole (20 KB) fits → allocate 18 KB, leaving a 2 KB internal fragment.
- Remaining holes: 2 KB, 15 KB, 30 KB, 10 KB, 25 KB.
2.3 Buddy System
- Memory is a power‑of‑two sized block.
- When a request arrives, the block is recursively split into halves (“buddies”) until the smallest block ≥ request size is found.
- When both buddies become free, they are coalesced.
Advantages: fast allocation/deallocation, low external fragmentation.
Disadvantages: internal fragmentation up to 50 % of the allocated block.
2.4 Slab Allocation (Kernel‑level)
- Used for objects of the same size (e.g., kernel data structures).
- Pre‑allocated caches (slabs) reduce fragmentation and allocation overhead.
3. Fragmentation
3.1 Internal Fragmentation
Occurs when allocated memory exceeds the requested size, leaving unused space inside the allocated block. Typical in fixed‑size partitions, paging, and buddy systems.
3.2 External Fragmentation
Occurs when free memory is split into many small non‑contiguous holes, none of which is large enough to satisfy a request, even though the total free memory would be sufficient.
Example from Past Exam
Memory = 100 KB, partitions: 150 KB, 200 KB, 250 KB, 100 KB, 300 KB (note: total > 100 KB, implying a typo; assume these are hole sizes after some allocations). Processes A = 175 KB, B = 125 KB.
First‑Fit:
- A fits into 200 KB hole → internal fragmentation = 200 − 175 = 25 KB.
- B fits into 150 KB hole → internal fragmentation = 150 − 125 = 25 KB.
Best‑Fit:
- A fits into 250 KB hole → internal frag = 75 KB (worse).
- B fits into 150 KB hole → internal frag = 25 KB.
The example illustrates how allocation policy influences internal fragmentation.
4. Paging
Paging eliminates external fragmentation by dividing both physical memory and logical address space into fixed‑size blocks called pages (logical) and frames (physical).
- Logical address = (page number, offset).
- Physical address = (frame number, offset).
4.1 Page Table
A data structure that maps page numbers to frame numbers.
| Page No. | Frame No. | Valid/Invalid |
|---|---|---|
| 0 | 5 | V |
| 1 | – | I |
| 2 | 3 | V |
| … | … | … |
Valid/Invalid bit indicates whether the page is resident in memory.
4.2 Hierarchical & Inverted Page Tables
- Hierarchical: two‑level tables reduce size for large address spaces.
- Inverted: one entry per frame, indexed by frame number; saves memory but requires hashing for lookup.
4.3 Demand Paging
Pages are loaded only when a process references them (page fault). This reduces initial load time and memory consumption.
Example – Demand Paging Fault Count
Reference string: 1, 3, 4, 2, 3, 5, 4, 3, 1, 2, 4, 6
Assume 3‑frame memory, initially empty.
| Step | Reference | Frames (FIFO) | Page Fault? |
|---|---|---|---|
| 1 | 1 | 1 – – | Yes |
| 2 | 3 | 1 3 – | Yes |
| 3 | 4 | 1 3 4 | Yes |
| 4 | 2 | 2 3 4 | Yes (replace 1) |
| 5 | 3 | 2 3 4 | No |
| 6 | 5 | 2 5 4 | Yes (replace 3) |
| 7 | 4 | 2 5 4 | No |
| 8 | 3 | 3 5 4 | Yes (replace 2) |
| 9 | 1 | 1 5 4 | Yes (replace 3) |
| 10 | 2 | 1 2 4 | Yes (replace 5) |
| 11 | 4 | 1 2 4 | No |
| 12 | 6 | 6 2 4 | Yes (replace 1) |
Total FIFO faults = 9.
The same reference string can be traced for LRU and Clock algorithms (shown later).
5. Page Replacement Algorithms
When a page fault occurs and all frames are occupied, the OS must decide which resident page to evict.
5.1 Optimal Page Replacement (OPR)
- Definition: Replace the page whose next reference is farthest in the future.
- Why “best”: Guarantees the minimum possible page faults for a given reference string.
- Practicality: Requires future knowledge → impossible to implement; used only for benchmarking.
5.2 Least Recently Used (LRU)
- Definition: Replace the page that has not been used for the longest time.
- Implementation: Counter per page, stack, or hardware “reference bits”.
- Pros/Cons: Near‑optimal performance, but hardware support or high overhead is needed.
5.3 Clock (Second‑Chance)
- Definition: Approximation of LRU using a circular list and a single reference bit per frame.
- Algorithm:
- Pointer (hand) scans frames.
- If reference bit = 0 → replace.
- If reference bit = 1 → set to 0 and advance hand (give a second chance).
5.4 Comparative Table
| Algorithm | Knowledge Required | Implementation Cost | Approximation of Optimal? | Typical Fault Rate |
|---|---|---|---|---|
| OPR | Future references | None (theoretical) | Exact | Minimum |
| LRU | Past references | High (counters/stack) | Very close | Low (slightly > OPR) |
| Clock | Recent use (bit) | Low (single bit) | Moderate | Moderate (≈ LRU) |
5.5 Worked Calculation – OPR, LRU, Clock
Reference string (same as above): 1 3 4 2 3 5 4 3 1 2 4 6, 3 frames.
OPR
We look ahead each step to decide which page will be used farthest later.
| Step | Ref | Frames before | Page to evict (if needed) | Fault? |
|---|---|---|---|---|
| 1 | 1 | – – – | – | Yes (load 1) |
| 2 | 3 | 1 – – | – | Yes (load 3) |
| 3 | 4 | 1 3 – | – | Yes (load 4) |
| 4 | 2 | 1 3 4 | 4 (next use at step 7, farther than 1 at step 9, 3 at step 5) | Yes (replace 4) |
| 5 | 3 | 1 3 2 | – | No |
| 6 | 5 | 1 3 2 | 1 (next use at step 9, later than 3 at step 8, 2 at step 10) | Yes (replace 1) |
| 7 | 4 | 5 3 2 | 5 (next use never) | Yes (replace 5) |
| 8 | 3 | 4 3 2 | – | No |
| 9 | 1 | 4 3 2 | 4 (next use at step 11, later than 2 at step 10) | Yes (replace 4) |
| 10 | 2 | 1 3 2 | – | No |
| 11 | 4 | 1 3 2 | 1 (next use never) | Yes (replace 1) |
| 12 | 6 | 4 3 2 | 3 (next use never) | Yes (replace 3) |
OPR faults = 9 (same as FIFO in this particular string, but often lower).
LRU (using stack order: most recent on top)
| Step | Ref | Stack (top→bottom) | Fault? |
|---|---|---|---|
| 1 | 1 | 1 | Yes |
| 2 | 3 | 3 1 | Yes |
| 3 | 4 | 4 3 1 | Yes |
| 4 | 2 | 2 4 3 | Yes (evict 1) |
| 5 | 3 | 3 2 4 | No |
| 6 | 5 | 5 3 2 | Yes (evict 4) |
| 7 | 4 | 4 5 3 | Yes (evict 2) |
| 8 | 3 | 3 4 5 | No |
| 9 | 1 | 1 3 4 | Yes (evict 5) |
| 10 | 2 | 2 1 3 | Yes (evict 4) |
| 11 | 4 | 4 2 1 | Yes (evict 3) |
| 12 | 6 | 6 4 2 | Yes (evict 1) |
LRU faults = 10.
Clock (reference bits start 0)
Assume hand starts at frame 0.
| Step | Ref | Frames (F0,F1,F2) | Ref bits before | Action | Fault? |
|---|---|---|---|---|---|
| 1 | 1 | – – – | 0 0 0 | Load 1 into F0, set bit=1 | Yes |
| 2 | 3 | 1 – – | 1 0 0 | Load 3 into F1, set bit=1 | Yes |
| 3 | 4 | 1 3 – | 1 1 0 | Load 4 into F2, set bit=1 | Yes |
| 4 | 2 | 1 3 4 | 1 1 1 | Hand at F0: bit=1 → set 0, advance. F1: bit=1 → set 0, advance. F2: bit=1 → set 0, advance. Back to F0 (now 0) → replace with 2, set bit=1. | Yes |
| 5 | 3 | 2 3 4 | 1 0 0 | Page 3 already in F1 → set its bit=1. | No |
| 6 | 5 | 2 3 4 | 1 1 0 | Hand at F1 (bit=1) → clear, advance. F2 (bit=0) → replace with 5, set bit=1. | Yes |
| 7 | 4 | 2 3 5 | 1 0 1 | Page 4 not present → hand at F2 (now holding 5, bit=1) → clear, advance to F0 (bit=1) → clear, advance to F1 (bit=0) → replace with 4. | Yes |
| 8 | 3 | 2 4 5 | 0 1 1 | Page 3 not present → hand at F2 (bit=1) → clear, advance to F0 (bit=0) → replace with 3. | Yes |
| 9 | 1 | 3 4 5 | 1 0 1 | Page 1 not present → hand at F1 (bit=0) → replace with 1. | Yes |
| 10 | 2 | 3 1 5 | 0 1 1 | Page 2 not present → hand at F2 (bit=1) → clear, advance to F0 (bit=0) → replace with 2. | Yes |
| 11 | 4 | 2 1 5 | 1 1 0 | Page 4 not present → hand at F1 (bit=1) → clear, advance to F2 (bit=0) → replace with 4. | Yes |
| 12 | 6 | 2 1 4 | 1 1 1 | Page 6 not present → hand cycles clearing bits until a 0 is found; after two clears, replace F0 (2). | Yes |
Clock faults = 11 (higher than LRU because of limited reference‑bit information).
5.6 Why OPR Is “Best but Not Practically Feasible”
- Optimality Proof: For any reference string, OPR yields the minimum possible page faults because it always discards the page whose next use is farthest away.
- Impracticality: The algorithm requires knowledge of future references, which no real system possesses. It can only be used in simulators or for theoretical comparison.
6. Virtual Memory
Virtual memory extends the apparent size of RAM by using secondary storage (disk) to hold pages that are not currently needed.
- Paging + Swapping: When a page fault occurs and no free frame exists, a victim page is written to disk (swap space) and the needed page is read in.
- Working‑Set Model: The set of pages referenced in the last time units. Keeping the working set resident prevents thrashing.
6.1 Thrashing
Occurs when the system spends most of its time swapping pages rather than executing user code. Indicators:
- High page‑fault rate.
- Low CPU utilization despite many processes.
Control measures:
- Reduce degree of multiprogramming.
- Increase RAM.
- Use locality‑aware scheduling.
7. Memory Management in Linux (Brief Overview)
- Paging: 4 KB pages (default) with a two‑level page table (pgd → pte).
- Swap: Dedicated swap partition/file;
swapon/swapoffcommands. - OOM Killer: Terminates processes when memory is exhausted.
/proc/meminfoprovides real‑time statistics.
8. Worked Example – CPU Utilization in Multiprogramming
Question: Compute CPU utilization when 6 processes are in memory, each spends 60 % of its time waiting for I/O.
Formula:
where = probability a process is waiting (0.60), = number of processes (6).
Thus, with six processes, CPU utilization rises to about 95 %, illustrating the benefit of multiprogramming over monoprogramming (which would be only 40 % utilization).
9. Summary of Key Concepts
| Concept | Core Idea | Typical Metric |
|---|---|---|
| Internal Fragmentation | Wasted space inside allocated block | % of allocated block unused |
| External Fragmentation | Unusable gaps between allocated blocks | % of total free memory in holes |
| Paging | Fixed‑size pages eliminate external frag | Page‑fault rate |
| Segmentation | Variable‑size logical segments, can cause external frag | Segment‑fault rate |
| OPR | Replace page with farthest future use | Minimum faults (theoretical) |
| LRU | Replace least recently used page | Near‑optimal faults |
| Clock | Approximate LRU with reference bits | Moderate faults, low overhead |
| Working Set | Pages used in recent interval | Size influences thrashing |
10. Frequently Asked Exam Questions
- Define internal and external fragmentation. Provide a numeric example.
- Explain the difference between multiprogramming and monoprogramming and compute CPU utilization for given parameters.
- Why is OPR optimal? Why can’t it be used in real systems?
- Given a reference string and a fixed number of frames, calculate page faults for OPR, LRU, and Clock.
- Compare First‑Fit, Best‑Fit, and Worst‑Fit allocation strategies in a table.
11. Exam Tip
- Memorize formulas: CPU utilization and working‑set size concepts.
- Practice trace tables: Write down frames, reference bits, and page‑fault counts step‑by‑step; examiners love clear, ordered traces.
- Know the “why”: For OPR, be ready to explain the optimality proof in one sentence (“it evicts the page whose next reference is farthest in the future”).
- Fragmentation: When asked to differentiate, list cause, where it appears, and how each allocation scheme mitigates it.
- Time‑management: Allocate ~2 minutes per sub‑question in a 20‑minute paper; use the comparison tables you prepared to answer quickly.
Based on the TU BSc CSIT syllabus for Operating Systems (CSC264), unit 4.
Discussion
Loading…