Skip to content
We areTOP 1%

Compare the first-fit, best-fit and worst-fit memory allocation strategies.

MediumConceptMemory Management
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

When memory is allocated in variable-size contiguous blocks, these strategies decide which free hole a new process gets.

  • First fit: take the first hole that is big enough. It is fast and works well in practice.
  • Best fit: take the smallest hole that is big enough. It leaves the smallest leftover, but it must search the whole list and creates tiny holes too small to use.
  • Worst fit: take the largest hole, hoping the leftover stays useful. It usually performs worst.
  • Simulations show that first fit and best fit use memory better than worst fit, and first fit is generally faster.

Example: free holes, in order, of 100, 500, 200, 300 and 600 KB; requests of 212, 417, 112 and 426 KB arrive in that order.

RequestFirst fitBest fitWorst fit
212500 (288 left)300600 (388 left)
417600500500
112the 288 left over200the 388 left over
426must wait600must wait

Only best fit places all four here. With first fit and worst fit, more than 426 KB is free in total, but no single hole is large enough.

More in Memory Management

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account