1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
minimum block size that can be assigned to
current process i.e., find min(bockSize,
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes.
Below is implementation.
// C++ implementation of Best - Fit algorithm
// Function to allocate memory to blocks as per Best fit
Process No. Process Size Block no.
1 212 4
2 417 2
3 112 3
4 426 5
Is Best-Fit really best? Although, best fit minimizes the wastage space, it consumes a lot of processor time for searching the block which is close to required size. Also, Best-fit may perform poorer than other algorithms in some cases. For example, see below exercise.
Example: Consider the requests from processes in given order 300K, 25K, 125K and 50K. Let there be two blocks of memory available of size 150K followed by a block size 350K.
Best Fit: 300K is allocated from block of size 350K. 50 is left in the block. 25K is allocated from the remaining 50K block. 25K is left in the block. 125K is allocated from 150 K block. 25K is left in this block also. 50K can’t be allocated even if there is 25K + 25K space available.
First Fit: 300K request is allocated from 350K block, 50K is left out. 25K is be allocated from 150K block, 125K is left out. Then 125K and 50K are allocated to remaining left out partitions. So, first fit can handle requests.
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes