Memory Allocation Algorithms
1. First Fit
How it works:
Scan memory from the beginning and allocate the first free block that is large enough.
Example:
Free blocks: [100 KB, 500 KB, 200 KB, 300 KB]
Request: 212 KB -> Allocated to 300 KB block.
Advantages:
- Fast, simple to implement.
Disadvantages:
- Can leave many small gaps (fragments) at the start.
2. Best Fit
How it works:
Find the smallest free block that is big enough for the request.
Example:
Free blocks: [100 KB, 500 KB, 200 KB, 300 KB]
Request: 212 KB -> Allocated to 300 KB block.
Advantages:
- Reduces internal fragmentation.
Disadvantages:
- Slower (must search entire list), can create many small unusable holes.
3. Worst Fit
How it works:
Allocate the largest available free block.
Example:
Free blocks: [100 KB, 500 KB, 200 KB, 300 KB]
Request: 212 KB -> Allocated to 500 KB block.
Advantages:
- Leaves large free blocks, reducing external fragmentation.
Disadvantages:
- Often wastes big blocks unnecessarily.
4. Next Fit
How it works:
Similar to First Fit, but it starts searching from where it last left off.
Example:
Free blocks: [100 KB, 500 KB, 200 KB, 300 KB]
After serving a previous request at 200 KB, it starts checking from 300 KB block next.
Advantages:
- Distributes free space more evenly.
Disadvantages:
- Can still fragment memory, slightly slower than First Fit.
Summary Table
Algorithm | Search Pattern | Goal
----------- | ------------------------ | --------------------------
First Fit | Start from beginning | Fastest, simple
Best Fit | Smallest sufficient block| Save space
Worst Fit | Largest available block | Leave large blocks
Next Fit | Continue from last spot | Balance search effort