Sub topic 3
Internal and External Fragmentation
Assume you know binary units, process memory, and contiguous allocation. In contiguous allocation, each process receives one continuous region of physical memory. Fragmentation occurs when free memory exists but cannot be used efficiently.
Internal Fragmentation
Internal fragmentation is unused space inside an allocated memory region. The operating system gives the process more space than it requested because of a fixed allocation rule.
Suppose memory is divided into fixed partitions of 16 KB. A process needs 10 KB, so it receives one partition:
- Allocated partition: 16 KB
- Requested memory: 10 KB
- Unused memory inside the partition: 6 KB
The 6 KB is internal fragmentation. Another process cannot use it because it lies inside the first process's assigned partition.
The general calculation is:
internal fragmentation = allocated size - requested size
For several allocations, add the unused space in every allocated unit.
Internal fragmentation also appears in paging. A process usually does not occupy every byte of its final page. With 4 KB pages, a process that needs 10 KB uses three pages, or 12 KB. Up to almost 4 KB may be unused in the last page.
External Fragmentation
External fragmentation is free memory outside allocated regions, split into separate holes. The total free memory may be large enough for a request, but no individual hole is large enough.
Consider this memory layout:
| Region | Size | Status |
|---|---|---|
| Process A | 20 KB | Allocated |
| Hole 1 | 8 KB | Free |
| Process B | 12 KB | Allocated |
| Hole 2 | 7 KB | Free |
| Process C | 10 KB | Allocated |
| Hole 3 | 9 KB | Free |
There are 24 KB of free memory in total. A request for 15 KB still fails under contiguous allocation because the largest individual hole is only 9 KB. The free space is externally fragmented.
Compaction
Compaction moves allocated regions so that scattered holes become one larger hole. In the example, the operating system could move the processes toward one end of memory and combine the 24 KB of free space.
Compaction requires relocatable processes. The system must update addresses safely while a process is moved. It also consumes CPU and memory bandwidth, so it is not free.
A relocation register can help with dynamic address translation. However, moving processes still requires coordination, and compaction may pause or slow applications.
Comparing the Two Types
| Property | Internal fragmentation | External fragmentation |
|---|---|---|
| Location of waste | Inside an allocated block | Between allocated blocks |
| Common cause | Fixed-size allocation | Variable-size contiguous allocation |
| Typical remedy | Use smaller or more flexible units | Compaction or noncontiguous allocation |
| Example | 6 KB unused in a 16 KB partition | Three holes totaling 24 KB |
Paging removes external fragmentation because any free frame can hold any page. It can still cause internal fragmentation in the last page. Thus, changing the allocation method changes which fragmentation problem dominates; it does not guarantee that every byte is useful.
Exercise
A system has free holes of 5 KB, 11 KB, and 8 KB. A process requests 20 KB. Is this request blocked by internal or external fragmentation, assuming contiguous allocation? What happens if the system can compact memory?
Show answer
It is blocked by external fragmentation. The total free memory is 24 KB, but no single hole is 20 KB. Compaction can combine the holes into one 24 KB region, allowing the request.