Simulators

Memory allocation simulator

Enter the free holes in memory and the sizes of the requests, choose first fit, next fit, best fit or worst fit, and step through the requests. Each step shows which holes were checked, which one was chosen and what is left.

The simulator

How the Memory allocation simulator works

Memory has free holes of different sizes, and programs ask for memory one request at a time. The allocation algorithm picks which hole each request goes into. The simulator shows, for every request, which holes were checked, which one was chosen and what is left of it.

First fit takes the first hole that is big enough. Next fit does the same but starts where the last search stopped. Best fit takes the smallest hole that is big enough. Worst fit takes the biggest hole.

With variable partitions the chosen hole is split: the request gets exactly what it asked for and the rest stays as a smaller hole. With fixed partitions the whole block is used and the unused part inside it is internal fragmentation. A request that fits no single hole fails.

Worked example: the same requests, four algorithms

Holes (in KB): 100, 500, 200, 300, 600. Requests in order: 212, 417, 112, 426. Variable partitions.

The hole each request is placed in (its size before the split)
AlgorithmRequest 212Request 417Request 112Request 426PlacedFailed
First fit500600288fails31
Next fit500600183fails31
Best fit30050020060040
Worst fit600500388fails31

First fit places 3 of the 4 requests. It fails on 426: 959 KB is free in total, but the biggest hole is only 300 KB, which is external fragmentation. Best fit places all 4, because it saves the large holes for the large requests.

Best fit is not always the winner: it leaves many tiny holes, and other request lists favour other algorithms. Change the numbers in the simulator and compare.

Questions about the Memory allocation simulator

What is the difference between first fit, best fit and worst fit?
First fit takes the first hole that is big enough, best fit the smallest that is big enough, worst fit the biggest. For the holes 100, 500, 200, 300, 600 and requests 212, 417, 112, 426, first fit places 3 requests, best fit 4, worst fit 3 and next fit 3.
What is external fragmentation?
Free memory that is split into holes that are each too small for a request. In the example, request 426 fails under first fit although 959 KB is free, because the biggest hole is 300 KB.
What is internal fragmentation?
With fixed partitions, a request gets a whole block, and the part it does not use is wasted inside the block. With the same holes and requests, fixed partitions with first fit waste 559 KB inside the blocks that were given out. Switch the simulator to fixed partitions to see it.
Which memory allocation algorithm is best?
None is best for every input. Best fit placed every request in the example (4 of 4), but it tends to leave small useless holes, and first fit is faster because it stops at the first hole that fits.

Memory allocation simulator: how to use it

Choose variable partitions (the hole is split, and the leftover is a smaller hole) or fixed partitions (the whole block is used). The tool shows internal fragmentation, and explains a request that cannot be placed: either there is not enough free memory, or it is split into holes that are each too small, which is external fragmentation.

Lessons that use it

  • Memory: addresses and allocation (Operating Systems)Logical and physical addresses, base and limit registers, fixed and variable partitions, first fit, best fit and worst fit, and internal and external fragmentation.

See also