BIT204 ยท 2079 ยท 5 marks
What is memory compaction? Explain best fit memory allocation with suitable example.
What is memory compaction? Explain best fit memory allocation with suitable example. [5]
Quick answer
Memory compaction is a memory management technique used to overcome the problem of external fragmentation. When processes are loaded and removed from memory over time, free memory becomes scattered in small, non-contiguous blocks. Even if the total free memory is sufficient to accommodate a new process, no single contiguous block may be large enough.
Memory Compaction and Best Fit Memory Allocation
Memory Compaction
In compaction, the OS shuffles all occupied memory partitions together (toward one end of memory), merging all the scattered free holes into one large contiguous free block.
Key Points:
- Requires dynamic relocation (base register support)
- It is time-consuming and costly in terms of CPU overhead
- Not always possible if relocation cannot be done at runtime
Before Compaction: After Compaction:
| P1 (20KB) | | P1 (20KB) |
| Free (10KB)| | P2 (15KB) |
| P2 (15KB) | ----> | P3 (5KB) |
| Free (8KB) | | Free (33KB)|
| P3 (5KB) | | |
| Free (15KB)| | |
Best Fit Memory Allocation
Best Fit is a contiguous memory allocation strategy in which the OS searches the entire list of free memory holes and allocates the smallest hole that is large enough to satisfy the process's memory request.
Idea:
Allocate the hole that leaves the least leftover free space after allocation.
Advantages:
- Minimizes wasted space in each individual allocation
- Leaves larger holes available for bigger future requests
Disadvantages:
- Slower -- must search the entire free list every time
- Tends to create many tiny, unusable leftover fragments (small holes that are too small for any process) -- leading to external fragmentation
Example
Free Memory Holes (initially):
| Hole | Size |
|---|---|
| H1 | 100 KB |
| H2 | 500 KB |
| H3 | 200 KB |
| H4 | 300 KB |
| H5 | 50 KB |
Process Requests:
| Process | Memory Required |
|---|---|
| P1 | 212 KB |
| P2 | 417 KB |
| P3 | 112 KB |
Step 1: Allocate P1 (212 KB)
Search all holes for the smallest hole >= 212 KB:
- H1 = 100 KB -- too small
- H2 = 500 KB -- fits, leftover = 288 KB
- H3 = 200 KB -- too small
- H4 = 300 KB -- fits, leftover = 88 KB <-- smallest leftover
- H5 = 50 KB -- too small
Best Fit: H4 (300 KB) Remaining in H4 = 300 - 212 = 88 KB
Step 2: Allocate P2 (417 KB)
Updated holes: H1=100, H2=500, H3=200, H4=88, H5=50
- H2 = 500 KB -- fits, leftover = 83 KB <-- only hole large enough
Best Fit: H2 (500 KB) Remaining in H2 = 500 - 417 = 83 KB
Step 3: Allocate P3 (112 KB)
Updated holes: H1=100, H2=83, H3=200, H4=88, H5=50
- H1 = 100 KB -- too small
- H2 = 83 KB -- too small
- H3 = 200 KB -- fits, leftover = 88 KB <-- smallest leftover
- H4 = 88 KB -- too small
- H5 = 50 KB -- too small
Best Fit: H3 (200 KB) Remaining in H3 = 200 - 112 = 88 KB
Final State of Memory:
| Hole | Original | Remaining |
|---|---|---|
| H1 | 100 KB | 100 KB (unused) |
| H2 | 500 KB | 83 KB (leftover) |
| H3 | 200 KB | 88 KB (leftover) |
| H4 | 300 KB | 88 KB (leftover) |
| H5 | 50 KB | 50 KB (unused) |
Notice that best fit leaves behind many small leftover fragments, which is its main drawback despite minimizing waste per allocation.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.