Best Fit Algorithm in Python

The Best Fit algorithm is a type of algorithm used for memory allocation for searching the available memory block which fits in the process. Allocating memory is an important task. The most common and simple method of allocating memory is by using the best-fit algorithm.

Best Fit Algorithm in Memory Management

The Best Fit Algorithm searches for an empty memory block. It aims to find a memory block having minimum memory wastage. Then, it allocates that memory block to the process. After the allocation, it compares the process with the memory block, and the block with minimum leftover memory is selected. The memory partition with minimum memory loss used to allocate the process is called the best fit algorithm partition.

Working of Best Fit Algorithm

The best fit algorithm repeats the list of memory blocks and searches for the ones that can fit into the process. The memory blocks are assigned with unique tags, and each process gets the block ID, which determines their corresponding memory block. The unique tag ID tells which process needs to be deleted to free up space.

There are two approaches to implementing the best fit algorithm in Python including:

  • Using arrays
  • Using a linked list

Let's implement the best fit algorithm in Python.

1. Using Arrays

Algorithm:

  1. Declare memory blocks and processes with sizes.
  2. Initialise all memory blocks as free.
  3. Search the minimum block size in every process and assign it to the current process.
  4. If the search is not found, start the process again and find the minimum block in the other process.

Implementing the best fit algorithm using arrays in Python

Code:

Output:

Process ID   Process Size   Block Number
1              127           2
2              122           1
3              567           3
4              126           4

We made a function bestFit_array( ) and taken the block size and process size as inputs to implement the best fit algorithm using an array. Then, we stored the block IDs of all the blocks assigned to a process in a variable a. Then, we iterate for each process, search for the appropriate block according to its size, and assign it to the corresponding block number. We will then find the block best fitted to the current process. If the block is found, then the block is allocated to the process. If the block is not in the range of the process size, then no memory will be allocated to it. After the allocation, we will reduce the available memory in the block. Then, we will call the function and print the Process ID, Process Size, Block Size, and Block Number.

2. Using Linked List

Algorithm:

  1. Create a list with the memory blocks and a list of processes.
  2. Search for the best memory block and allocate it to the process.
  3. If the block is not found, print the memory block and add it to the list.
  4. Free up space for other processes by deleting the process node from the linked list.
  5. The block ID of the deleted node will be assigned to other processes to increase the memory block size.

Implementing the best fit algorithm using the linked list

Code:

Output:

Cannot allocate Block Size [ ]
Tag ID	Block ID	Size
0		1		471	
1		0	      121	
2		1		85	
After deletion:
Tag ID	Block ID	Size
1		0		121	
2		1		85	
3		1		426

Explanation:

We have made classes for the allocated list and free memory list. Then, we created a function to create a list with the given size. A function to print the free list will print the free memory blocks. Then, the memory is allocated to the process according to the best fit algorithm. Then, we will delete the nodes to free the space from the list. We have called the functions with block size and object size as parameters and printed the Tag ID, Block ID, and size after allocation and deletion.