Memory Allocation Strategies: First Fit, Best Fit, Worst Fit
- Process Management
- Memory Management
- Deadlocks
- OS Introduction & Fundamentals
- CPU Scheduling Algorithms
- Threads & Concurrency
- Inter-Process Communication (IPC)
- Process Synchronization
- Virtual Memory & Paging
- File Systems
- Disk Scheduling
- I/O Management
Deciding Which Free Gap a New Process Should Use
Under dynamic partitioning (covered earlier in this section), several free gaps of different sizes may exist at once. When a new process needs memory, the OS must choose which specific free gap to place it in - three classic strategies answer this differently.
A Shared Example for All Three
Free gaps available: Gap A: 20k Gap B: 8k Gap C: 15k
New process needs: 10kFirst Fit: The First Gap That's Big Enough
First Fit scans through gaps in order and picks the very first one large enough to fit the request - in this example, Gap A (20k), simply because it's encountered first and is big enough. It's fast, since scanning can stop the moment a suitable gap is found.
Best Fit: The Smallest Gap That's Still Big Enough
Best Fit scans every gap and picks the smallest one that's still large enough - in this example, Gap C (15k), since it's the tightest possible fit. This minimizes leftover wasted space in that specific gap, but requires examining every gap first.
Worst Fit: The Largest Available Gap
Worst Fit deliberately picks the largest available gap - in this example, Gap A (20k) again, but for a completely different reason than First Fit: the idea is that using the biggest gap leaves the largest possible leftover space, which might be more useful for a future request.
Comparing All Three Strategies
| Strategy | Choice in example | Main trade-off |
|---|---|---|
| First Fit | Gap A (20k) | Fast, but can leave awkwardly-sized leftovers early in memory |
| Best Fit | Gap C (15k) | Minimizes waste per allocation, but tends to create many tiny, unusable leftover gaps over time |
| Worst Fit | Gap A (20k) | Keeps remaining gaps larger, but can waste more space per individual allocation |
Why No Single Strategy Is Universally "Best"
Each strategy performs differently depending on the actual pattern of process sizes and free gaps in a real system - which is exactly why this remains a genuinely studied trade-off, rather than one strategy having simply replaced the other two entirely.