Main Memory: Allocation, Linking & Segmentation

first-fit

Imagine looking for a parking space by driving down the row and taking the very first spot big enough for your car. You do not cruise the whole lot comparing — you grab the first one that works. First-fit is exactly this strategy for placing a process into free memory: scan the list of holes from the start and use the first hole that is large enough.

Concretely, the OS keeps a list of free holes. When a process of size S arrives, first-fit walks the list from the beginning and stops at the first hole whose size is at least S. It carves S bytes out of that hole and leaves the remainder (hole size minus S) as a new, smaller hole. The search can start at the front each time, or it can resume from where the last search stopped (a variant called next-fit). Because it stops at the first match, first-fit does the least searching of the three classic strategies.

Why it matters: first-fit is the simplest and usually the fastest of first-fit, best-fit, and worst-fit, because it does not scan the whole list. In practice it also performs surprisingly well on memory utilization. Its weakness is a subtle one: because it always starts from the front, it tends to chew up the holes near the beginning of memory into many small fragments over time. Like all contiguous strategies, it cannot escape external fragmentation; it only changes how the leftovers are shaped.

Holes in order: 50 KB, 200 KB, 120 KB. A 100 KB request scans from the front, skips the 50 KB hole, and takes the 200 KB hole (the first one big enough), leaving a 100 KB hole behind. The 120 KB hole is never even examined.

Stop at the first hole that fits — fast, but biased toward the front of memory.

First-fit is generally both faster and no worse in space use than best-fit; the intuitive idea that best-fit must waste less memory is wrong in practice. No contiguous strategy avoids external fragmentation.

Also called
first-fit allocation首次適配