The first-fit algorithm is a general allocation strategy that scans available resources in order and assigns the first one large enough for the request, and its exact meaning depends on the setting: in dynamic memory allocation it places a block in the first free hole that fits, in bin packing it puts each item into the first bin with remaining capacity, and in file systems it allocates from the first sufficiently large free region it encounters.
That is the essential rule in every variant: take the first acceptable candidate and stop looking. The algorithm is greedy and usually online — it commits to a placement before seeing later requests. The three common versions allocate different things and are evaluated with different measures, so the useful answer distinguishes them early rather than treating "first fit" as a single procedure.
The shared mechanism
In all its forms, first fit keeps a collection of candidates in some order and tests them one at a time:
1: Take the next request. 2: Walk the candidate list or free-list in order. 3: Select the first candidate with enough capacity (or, in simple versions, the first free slot or first address). 4: Allocate, and stop scanning.
The stopping condition is what separates first fit from best fit (choose the tightest sufficient candidate after scanning all) and worst fit (choose the largest). First fit trades optimality for a short scan that can end early. It is normally fast when large-enough space is common near the front of the list, but it can become slow when many small or unusable candidates sit ahead of the first suitable one — the list gets walked further on each request.
First fit in dynamic memory allocation
This is the classic operating-systems context and the one most textbooks describe. A memory allocator maintains a free list of holes (unallocated regions). For a request of size n:
First fit takes the first hole whose size is at least n. Next fit continues scanning from where the previous search stopped, wrapping around. Best fit takes the smallest hole that is at least n. Worst fit takes the largest hole.
Worked example with illustrative numbers
Suppose the free list, in address order, contains holes of sizes 100, 500, 200, 600 bytes. Requests arrive for 150, then 400, then 120 bytes. Using first fit:
| Request | Scan result | Allocation | Free list afterward (address order) |
|---|---|---|---|
| 150 | 100 is too small; 500 fits first | 150 taken from the 500 hole | 100, 350, 200, 600 |
| 400 | 100, 350 too small; 200 too small; 600 fits | 400 taken from the 600 hole | 100, 350, 200, 200 |
| 120 | 100 too small; 350 fits | 120 taken from the 350 hole | 100, 230, 200, 200 |
These numbers are illustrative only; the arithmetic is reproducible from the stated rule. Note what happened: the large 500-byte hole was split before the 600-byte hole, and small holes at the front (the 100) survived because they were too small for the request. Repeated requests can leave scattered small holes — external fragmentation, where total free memory is sufficient but no single contiguous hole is large enough.
Allocation policies and where splits go
The phrase "first fit" often hides two decisions beyond candidate choice. When a hole is larger than the request, the allocator must decide whether to split it and whether the remainder is worth keeping. Many implementations use a threshold: if the leftover is smaller than some minimum, the entire hole is allocated rather than leaving an unusable sliver. Also, in list-based allocators the allocated piece is often taken from the beginning of the hole, but a variant can carve from the end to keep list order simple. These details change fragmentation behavior even though the selection rule is identical, so two systems both "using first fit" may perform differently.
Why first fit is popular
First fit tends to be fast because it stops at the first success, and it tends to preserve large holes at the back of the list — a large hole is only used when nothing smaller fits. However, it fragments the front of the free list with small leftover slivers. Best fit often leaves even smaller unusable remnants, while worst fit leaves large remainders; comparisons among them are workload-dependent. A textbook caution worth remembering: no single policy is best for all workloads, and simulation on representative allocation patterns matters more than slogans.
A related technique is the segregated free list, where holes are bucketed by size class and first fit is applied within a class. This is common in real allocators because it bounds search time; it is a refinement, not a different principle.
First fit in bin packing
In bin packing, items must be packed into bins of fixed capacity, minimizing the number of bins. First Fit (FF) processes items in the given order and places each item into the first bin with enough remaining capacity; if none fits, a new bin is opened.
With capacity 10 and items arriving in order 6, 5, 4, 5, 3, 4:
6 → bin A (remaining 4) 5 → no fit in A; bin B (remaining 5) 4 → A fits (remaining 0) 5 → B fits (remaining 0) 3 → no fit in A or B; bin C (remaining 7) 4 → C fits (remaining 3)
Result: three bins. The same items in a different order can produce different results, which is why bin-packing variants care about ordering.
First Fit Decreasing (FFD) sorts items from largest to smallest before applying first fit. Sorting is an offline step that requires knowing all items in advance. FFD is a famous approximation: it never uses more than about 11/9 of the optimal number of bins plus a constant. That bound is a proven worst-case guarantee, not a typical performance figure; typical results are usually closer to optimal, but the guaranteed ratio is what textbooks state precisely.
Distinguish first fit (online, order given) from first fit decreasing (offline, sorted). Confusing them is one of the most common errors in discussions of bin packing.
First fit in file systems and disk allocation
File systems allocate space for new files or blocks. In a first-fit policy, the allocator scans the free-space structure (a free list, bitmap, or similar) and allocates the first contiguous free region large enough for the request.
This differs from next fit, which resumes scanning after the last allocated location, and from best fit, which hunts for the smallest sufficient region. First fit on disk tends to be simple and fast but can pack new files into early free space, potentially increasing fragmentation and scattering related blocks. Real file systems add heuristics — locality preferences, extent allocation, group-based placement — so the bare first-fit rule is a starting principle rather than a complete design.
A caution about terminology: "first fit" anywhere the candidate space is ordered will allocate the first acceptable slot, but whether the "slot" is contiguous blocks, fixed-size bins, or variable holes changes the consequences. A fixed-size slot allocation has no splitting decision and thus much milder fragmentation than variable-size hole allocation.
First fit in memory caches and replacement policies
Some caches and buffer pools use a first-fit-like admission: scan for the first free or reusable entry, use it, and stop. This shares the greedy, stop-early character of first fit. However, when the context is eviction (deciding what to discard when full), first fit is not the same as a replacement policy such as Least Recently Used; the similarity is only the scan-and-take behavior. Keep the concepts distinct: first fit selects a resource to allocate, while replacement policies decide which resource to remove.
What first fit does not guarantee
Optimality: In bin packing, FF can use notably more bins than optimal; in memory, fragmentation can accumulate despite enough total free space. Stability of ordering: The allocation result depends on the order candidates and requests appear; reordering can change outcomes. Uniform performance: Worst-case scan time can approach the number of candidates when the list is cluttered. A universal speed ranking: Whether first fit is faster than best fit depends on data structures and workload.
The most common confusion is assuming all "first fit" policies have one analysis. Try to remember this: the rule is the stopping behavior, not the outcome. First fit says "take the first acceptable candidate"; it says nothing about whether that placement is good.
Common questions
Is first fit the same as greedy? It is a greedy strategy — it makes the locally cheapest choice without lookahead — but "greedy" is broad; best fit and worst fit are also greedy.
Is first fit always online? First fit itself uses only current state, so it can run online. When an application sorts items first (FFD), it becomes offline, and that distinction changes its guarantees.
Does first fit reduce internal or external fragmentation? It tends toward external fragmentation in variable-size memory allocation (small leftover holes) but has no splitting problem in fixed-size bin packing. Internal fragmentation (unused space inside an allocated unit) comes from allocation granularity, not from first fit's selection rule per se.
Why is next fit mentioned alongside first fit? Next fit is first fit with a moving scan pointer; it spreads allocations, sometimes reducing front-of-list clutter and sometimes worsening fragmentation elsewhere. It is a distinct variant, not a synonym.
In one sentence
First fit allocates the first candidate resource with enough capacity, whether that candidate is a free memory hole, a partially filled bin, or a contiguous free region on disk, and its simplicity and speed are bought at the cost of fragmentation and no optimality guarantee.