Contiguous & Non-Contiguous Allocation, Fragmentation & Compaction
1. Contiguous vs Non-Contiguous Memory Allocation
When a process needs to be loaded into Main Memory (RAM), the Operating System can allocate memory to it using two fundamental approaches:
- Each process is contained in a single continuous (adjacent) block of RAM addresses
- Simple & fast address translation using Base + Limit registers
- Types: Fixed Partitioning (MFT) & Variable/Dynamic Partitioning (MVT)
- Drawback: Suffers from Internal & External Fragmentation
- A single process is divided into blocks and placed in scattered, non-adjacent locations in RAM
- Uses a Page Table or Segment Table to keep track of blocks
- Types: Paging and Segmentation
- Benefit: Eliminates External Fragmentation (in Paging)!
2. Contiguous Memory Allocation Techniques
In Contiguous Memory Allocation, every process must occupy a single unbroken block of contiguous physical memory. It is implemented in two ways:
A. Fixed-Size Partitioning (MFT — Multiprogramming with a Fixed Number of Tasks)
- Main Memory (RAM) is divided at system boot time into several fixed-size partitions (blocks) (e.g., four partitions of
100 KBeach). - Each partition can hold exactly one process.
- Problem: If a small
30 KBprocess is placed inside a100 KBfixed partition, the remaining70 KBinside that partition is wasted and cannot be given to any other process! This wasted space is called Internal Fragmentation.
B. Variable / Dynamic Partitioning (MVT — Multiprogramming with a Variable Number of Tasks)
- Initially, all user RAM is treated as one large block of free memory called a Hole.
- When a process arrives (e.g., needing
75 KB), the OS carves out a partition of exact size75 KBfor it. - As processes enter and leave RAM over time, memory becomes a checkerboard of allocated blocks and scattered small free Holes of varying sizes (
20 KB,15 KB,40 KB), leading to External Fragmentation.
3. Dynamic Storage Allocation Algorithms (First Fit, Best Fit, Worst Fit)
When a new process of size N arrives and there are several free Holes (empty blocks) scattered across RAM, how does the OS choose which hole to allocate? Using one of 3 Dynamic Allocation Strategies (a very popular numerical & theory question in DAVV exams!):
- First Fit:
- Scans the list of free holes from the beginning and allocates the FIRST hole that is big enough (≥ N).
- Pros: Fastest algorithm because it searches as little as possible.
- Best Fit:
- Searches the entire list of free holes and allocates the SMALLEST hole that is big enough (≥ N), so that the leftover wasted hole is as tiny as possible.
- Cons: Slower (must scan entire list unless sorted) and leaves behind tiny, unusable leftover slivers (
1 KB,2 KB) across RAM!
- Worst Fit:
- Searches the entire list and allocates the LARGEST available hole, so that the leftover fragment after allocation is still large enough to hold another process later.
- Cons: Slower and breaks up large holes quickly.
Solved Example: First Fit vs Best Fit vs Worst Fit
Suppose RAM has 4 Free Holes in order: 100 KB, 500 KB, 200 KB, and 300 KB.
Where will a new process of size 212 KB be placed?
- First Fit: Scans from left:
100 KB(too small) →500 KB(First hole ≥ 212 KB! Allocated here, leaving288 KB). - Best Fit: Checks all holes ≥ 212 KB (
500 KBand300 KB). Smallest fit is300 KB! (Allocated in300 KB, leaving88 KB). - Worst Fit: Checks all holes ≥ 212 KB. Largest hole is
500 KB! (Allocated in500 KB).
4. Fragmentation: Internal vs External Fragmentation
Fragmentation refers to memory space in RAM that is wasted and cannot be used efficiently to allocate processes. There are two types of fragmentation:
- Occurs when memory is divided into Fixed-Size blocks (Fixed Partitioning / Paging)
- A process of
18 KBis given a20 KBblock →2 KBwasted INSIDE the block! - Belongs to the allocated partition, so no other process can use it
- Occurs in Variable-Size Contiguous Allocation & Segmentation
- Total free RAM is
100 KB, but split into scattered holes (`30 KB + 40 KB + 30 KB`), so a60 KBprocess cannot fit! - Solved using Compaction or Paging
Comparison Table: Internal Fragmentation vs External Fragmentation
This table is a guaranteed 5-mark question in DAVV BCA exams!
| Basis | Internal Fragmentation | External Fragmentation |
|---|---|---|
| 1. Definition | Wasted memory INSIDE an allocated memory block when the block is slightly larger than the process requested. | Wasted memory OUTSIDE allocated blocks when enough total free RAM exists, but it is not contiguous (scattered in small holes). |
| 2. Cause | Caused by Fixed-Size memory allocation (Fixed Partitioning and Paging). | Caused by Variable-Size contiguous allocation as processes are loaded and removed repeatedly. |
| 3. Who Owns the Waste? | The wasted space is assigned to a specific partition/process. | The wasted holes are free, belonging to no process, yet too small individually. |
| 4. Solution / Remedy | Use Variable-sized partitions or smaller page sizes. | Use Compaction (shuffling RAM) or Non-Contiguous Paging! |
5. Compaction (Defragmentation of RAM)
What is Compaction?
Compaction is a technique used by the Operating System to solve External Fragmentation!
- How It Works: The OS shifts (shuffles) all currently allocated processes toward one end of Main Memory (RAM) so that all the scattered small free holes combine together at the other end to form one single large contiguous block of free memory!
- Now a
60 KBor100 KBprocess can easily fit into the combined100 KBblock!
Important Requirement & Drawback of Compaction:
- Requires Dynamic (Execution-Time) Address Binding: Because Compaction physically moves running processes to new addresses in RAM, it is ONLY possible if address binding is done dynamically at Execution Time using relocation registers! (If addresses were bound statically at Compile or Load time, Compaction cannot be done).
- High CPU Overhead: Moving megabytes of processes in RAM takes CPU time, so an alternative solution is to allow non-contiguous allocation (Paging).
💡 Exam Preparation Tips for DAVV BCA
- Always draw Figure 4.6 (Before Compaction vs After Compaction) and the Internal vs External Fragmentation table—these two visuals make your Unit 4 answer crystal clear!
- Remember that Compaction only works when Execution-Time (Dynamic) Address Binding is used.