First fit, best fit and worst fit: what actually changes
8 November 2026 · 1 min read
- Operating Systems
- Memory allocation
When a program asks for memory, the system looks through the free gaps and picks one. First fit, best fit and worst fit are three simple ways of picking. They all work, but they leave memory in different shapes.
The example
Say these are the free gaps, in address order (sizes in KB), and a program asks for 212 KB:
100 500 200 300 600- First fit takes the first gap that is big enough: 500 KB. It leaves 288 KB.
- Best fit takes the smallest gap that is big enough: 300 KB. It leaves 88 KB.
- Worst fit takes the largest gap: 600 KB. It leaves 388 KB.
What each one is trying to do
- First fit is fast because it stops searching as soon as it finds a gap.
- Best fit wastes the least space on this request, but it tends to leave very small gaps that are hard to use.
- Worst fit hopes the leftover piece is still big enough to be useful later.
The problem they all share
Over time, allocating and freeing blocks breaks free memory into many small gaps. You can end up with plenty of free memory in total, but no single gap big enough for a new request. This is called external fragmentation. Merging neighbouring free gaps when a block is freed helps, but it does not remove the problem.
Try it yourself
In the Memory Allocation experiment, enter a series of allocations and frees, then run the same list with each strategy and compare the memory maps and the largest free gap.