Theory
The Missing 40 MB Mystery
Suppose you are sitting in your college computer lab, trying to load a heavy Android development project that needs 40 MB of continuous RAM. You check the system monitor, and it clearly shows 50 MB of free memory. Perfect, right? But when you click run, you get a nasty 'Out of Memory' crash. How can a system with 50 MB of free space fail to accommodate a 40 MB program? The answer lies in a hidden memory-wasting disease called fragmentation, which forces operating systems to completely change how they view your code.
Theory
The Movie Theater Seating Trap
Imagine you and three friends go to a movie theater. The ticket window executive tells you, 'Yes, we have 4 empty seats available!' But when you walk inside, you find one empty seat in the front row, two in the middle, and one in the last row. There are enough seats for your group, but you cannot sit together as a contiguous block of 4. This is external fragmentation. Now imagine a different rule where the theater forces every person to book an entire 3-seat couch even if they come alone. If you sit alone on that couch, 2 seats are wasted inside your block. That is internal fragmentation!
Theory
Understanding Fragmentation and Segmentation
In memory management, memory waste happens in two ways. Internal Fragmentation occurs when memory is divided into fixed partitions, and a process is given a slot slightly larger than it needs, leaving the leftover space inside that partition completely wasted and unusable by anyone else. External Fragmentation happens when variable partitions create small, scattered blocks of free memory over time: the total free memory is large enough for a new process, but it cannot be allocated because it is not continuous. To fix external fragmentation, operating systems use Segmentation, a non-contiguous memory allocation technique where a program is split into its natural logical units (like main functions, stacks, or global variables) called segments, allowing them to be stored in different parts of the physical RAM.
At a glance
Key distinctions between Internal and External memory fragmentation.
| Feature | Internal Fragmentation | External Fragmentation |
|---|---|---|
| Where it occurs | Inside an allocated partition block | Between allocated partition blocks |
| Primary Cause | Fixed size memory partitioning | Variable size memory partitioning |
| The Solution | Use variable partitioning or smaller slots | Compaction or non-contiguous allocation |
| Process Status | Process occupies the slot but wastes a part | No process can occupy the scattered slots |
Think first
Solving a Classic University Exam Numerics Problem
A system has three free memory blocks in this order: 20 KB, 50 KB, and 30 KB. Two processes arrive: P1 needing 15 KB and P2 needing 45 KB. If the system uses fixed partitioning where each process takes an entire block, calculate the internal fragmentation. If a third process P3 needing 40 KB arrives later, explain why it fails and name the fragmentation type.
Show the answer
Let us calculate step by step:
1. P1 (15 KB) is allocated the first block (20 KB). Wasted space inside this block = 20 KB - 15 KB = 5 KB. This is Internal Fragmentation.
2. P2 (45 KB) is allocated the second block (50 KB). Wasted space inside this block = 50 KB - 45 KB = 5 KB. This is also Internal Fragmentation.
3. Total Internal Fragmentation so far = 5 KB + 5 KB = 10 KB.
4. Now, P3 (40 KB) arrives. The only block left is the third block (30 KB), which is too small. P3 cannot load.
What if we used variable partitioning instead? P1 would take exactly 15 KB from the 20 KB block (leaving a 5 KB hole). P2 would take exactly 45 KB from the 50 KB block (leaving a 5 KB hole). The third block remains 30 KB. The total free space in the system would be 5 KB + 5 KB + 30 KB = 40 KB. P3 needs exactly 40 KB, but it cannot load because the free space is scattered as three separate holes (5 KB, 5 KB, 30 KB). This failure is called External Fragmentation.
Quiz
How does the Operating System locate a specific memory address when using Segmentation?
- By scanning the entire RAM from address 0000 every single time
- By looking up the Segment Table using a pair: Segment Number and Offset
- By converting the whole program into fixed 4 KB blocks blindly
- By asking the user to manually enter the physical address hex codes
Show the answer
By looking up the Segment Table using a pair: Segment Number and Offset
Segmentation uses a logical address consisting of a tuple: (segment number, offset). The CPU looks up the segment number in a Segment Table to find that segment's base physical address and limit. It then adds the offset to the base address to find the exact location in RAM, ensuring protection and flexibility.
Think first
The Segment Table Guard Check
Suppose a segment has a base address of 2000 and a limit of 150. What happens if a process tries to access an offset of 160 within that segment? Attempt a guess before expanding.
Show the answer
The system will instantly generate a trap or a segmentation fault error! The segment limit represents the legal size of the segment. Because the offset (160) is greater than the allowed limit (150), the hardware's address-translation unit detects an illegal memory access attempt and stops it immediately to protect other programs.
Watch out
The Fragmentation Nomenclature Trap
In university exams, external fragmentation does not mean memory is wasted outside the RAM! Students often make this funny mistake in their answer sheets. External means the waste happens outside and between the allocated blocks because the holes are too scattered. Internal means the waste happens inside a specific block allocated to a process. Keep this clear to score full marks.
Theory
Real World Connection: Segmentation Faults
Have you ever written a C or C++ program and encountered the infamous 'Segmentation Fault (core dumped)' crash? Now you know exactly what happens behind the scenes. Your code tried to access an offset that exceeded a segment's limit or attempted to write into a read-only code segment. Modern systems combine segmentation with paging to provide rock-solid security for application execution.
Summary
Key takeaways
- Internal fragmentation is memory wasted inside an allocated fixed-size block.
- External fragmentation occurs when total free space exists but is scattered in small chunks.
- Segmentation breaks a program into logical blocks matching a programmer's perspective.
- Each segment has an entry in the Segment Table storing its physical base address and limit size.
- Offsets are strictly checked against segment limits to prevent unauthorized memory access.
- Memory hook: Internal is inside the box, External is between blocks, Segmentation respects logical structures!