Memory Allocation Strategies: First Fit, Best Fit, Worst Fit

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:    10k

First 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

StrategyChoice in exampleMain trade-off
First FitGap A (20k)Fast, but can leave awkwardly-sized leftovers early in memory
Best FitGap C (15k)Minimizes waste per allocation, but tends to create many tiny, unusable leftover gaps over time
Worst FitGap 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.

Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.