GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Compare first fit and best fit memory traces

3 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Simulate ordered contiguous allocations under first-fit and best-fit. Preserve address order, splitting and the explicitly stated release/coalescence convention.

Descriptive
Report issue

Trace first fit and best fit

Memory Allocation & Segmentation

Question 5(a), GATE 1995: 1000K of contiguous free memory receives J1=200K, J2=350K, J3=300K; J1 finishes; then J4=120K, J5=150K and J6=80K arrive, in that order. Assume allocations start at the low-address end of a selected hole, free holes coalesce, no compaction and first fit searches from the lowest address. Draw both memory allocation sequences.
Question 5(a), GATE 1995: 1000K of contiguous free memory receives J1=200K, J2=350K, J3=300K; J1 finishes; then J4=120K, J5=150K and J6=80K arrive, in that order. Assume allocations start at the low-address end of a selected hole, free holes coalesce, no compaction and first fit searches from the lowest address. Draw both memory allocation sequences.
Tap to enlarge
Answer & solution

Answer: First fit admits all jobs; best fit cannot admit J6. (no official key was published)

Use half-open address intervals in K so that [0,200) has size 200. Both algorithms initially allocate J1=[0,200), J2=[200,550), J3=[550,850), leaving [850,1000) free. When J1 finishes, the holes are [0,200) and [850,1000).

First fit: J4 takes [0,120), leaving holes [120,200) of size 80 and [850,1000) of size 150. J5 cannot fit in 80, so it occupies [850,1000). J6 then exactly fills [120,200). Final low-to-high layout is J4:120 | J6:80 | J2:350 | J3:300 | J5:150.

Best fit: J4 takes the smaller adequate hole [850,1000), occupying [850,970) and leaving 30K. J5 takes [0,150), leaving [150,200) of size 50. J6 needs 80K but neither the 50K nor 30K hole suffices. Final layout is J5:150 | free:50 | J2:350 | J3:300 | J4:120 | free:30, with J6 waiting.

The total free space is 80K, equal to J6’s request, but it is split into nonadjacent holes. This is external fragmentation, not a shortage of aggregate space.

Original paper, page 6

Descriptive
Report issue

Compare allocation outcomes

Memory Allocation & Segmentation

Question 5(b), GATE 1995: 1000K of contiguous free memory receives J1=200K, J2=350K, J3=300K; J1 finishes; then J4=120K, J5=150K and J6=80K arrive, in that order. Assume allocations start at the low-address end of a selected hole, free holes coalesce, no compaction and first fit searches from the lowest address. Which policy performs better for this sequence?
Question 5(b), GATE 1995: 1000K of contiguous free memory receives J1=200K, J2=350K, J3=300K; J1 finishes; then J4=120K, J5=150K and J6=80K arrive, in that order. Assume allocations start at the low-address end of a selected hole, free holes coalesce, no compaction and first fit searches from the lowest address. Which policy performs better for this sequence?
Question 5(b), GATE 1995: 1000K of contiguous free memory receives J1=200K, J2=350K, J3=300K; J1 finishes; then J4=120K, J5=150K and J6=80K arrive, in that order. Assume allocations start at the low-address end of a selected hole, free holes coalesce, no compaction and first fit searches from the lowest address. Which policy performs better for this sequence?
Tap to enlarge
Answer & solution

Answer: First fit. (no official key was published)

First fit places the 120K job in the initial 200K hole, preserving an 80K remainder. It then puts the 150K job in the separate 150K hole and admits the final 80K job into the exact remainder. Consequently every arriving job is resident at the end, filling all 1000K.

Best fit uses the 150K hole for the 120K request, leaving 30K. The later 150K request takes 150K out of the 200K hole, leaving 50K. Although 50+30=80, the final 80K request requires one contiguous region and cannot be admitted without relocation or compaction. First fit therefore performs better on the stated admission/utilization criterion. This trace is a counterexample to the intuition that the locally smallest leftover always leads to the best future allocation. It is not a proof that first fit dominates best fit for every workload.

Original paper, page 6

Report issue

First fit versus best fit

Memory Allocation & Segmentation

Question 1.24, GATE 1994: The pictured heap has allocated lengths 50,300,600 separating free holes of lengths 150 and 350. Can requests 300,25,125,50 be satisfied by first fit, best fit, both or neither?
Tap to enlarge
Your answer
Answer & solution

Answer: B: first fit but not best fit (no official key was published)

Both policies allocate the first request in the 350 hole, leaving free lengths [150,50]. First fit places 25 in the first hole, leaving [125,50], then 125 there, leaving [0,50], and finally fits 50. Best fit places 25 in the 50 hole, leaving [150,25], then 125 in the 150 hole, leaving [25,25]. The final 50 fits neither hole, though total free space is 50. Hence only first fit succeeds.

Original paper, page 3