Contiguous Memory Allocation: First Fit, Best Fit, and Worst Fit

Adeena Khanzada
3 min readApr 30, 2023

--

💾 Memory allocation is a critical task in modern operating systems, and one of the most commonly used techniques is contiguous memory allocation. Contiguous memory allocation involves allocating memory to processes in contiguous blocks, where the starting address of each block is adjacent to the previous one. In this blog post, we’ll explore three cases of contiguous memory allocation: First Fit, Best Fit, and Worst Fit, with humor, emojis, and solved examples.

🎉 First Fit

The first-fit algorithm searches for the first free partition that is large enough to accommodate the process. The operating system starts searching from the beginning of the memory and allocates the first free partition that is large enough to fit the process.

For example, suppose we have the following memory partitions:

| 10 KB | 20 KB | 15 KB | 25 KB | 30 KB |

Now, a process requests 18 KB of memory. The operating system starts searching from the beginning and finds the first free partition of 20 KB. It allocates the process to that partition and keeps the remaining 2 KB as free memory.

🧐 Best Fit

The best-fit algorithm searches for the smallest free partition that is large enough to accommodate the process. The operating system searches the entire memory and selects the free partition that is closest in size to the process.

For example, suppose we have the following memory partitions:

| 10 KB | 20 KB | 15 KB | 25 KB | 30 KB |

Now, a process requests 18 KB of memory. The operating system searches for the smallest free partition that is larger than 18 KB, and it finds the partition of 20 KB. It allocates the process to that partition and keeps the remaining 2 KB as free memory.

🤔 Worst Fit

The worst-fit algorithm searches for the largest free partition and allocates the process to it. This algorithm is designed to leave the largest possible free partition for future use.

For example, suppose we have the following memory partitions:

| 10 KB | 20 KB | 15 KB | 25 KB | 30 KB |

Now, a process requests 18 KB of memory. The operating system searches for the largest free partition, which is 30 KB. It allocates the process to that partition and keeps the remaining 12 KB as free memory.

👍 Pros and Cons

First Fit is fast and simple to implement, making it the most commonly used algorithm. However, it can suffer from external fragmentation, where small free partitions are left between allocated partitions.

Best Fit reduces external fragmentation by allocating processes to the smallest free partition, but it requires more time to search for the appropriate partition.

Worst Fit reduces external fragmentation by leaving the largest free partition, but it can lead to inefficient use of memory.

🤖 Conclusion

Contiguous memory allocation is an essential technique used in modern operating systems to allocate memory to processes. First Fit, Best Fit, and Worst Fit are popular algorithms used for contiguous memory allocation. Each algorithm has its advantages and disadvantages, but all are designed to optimize memory allocation and reduce fragmentation. With these techniques, operating systems can efficiently manage memory, making them a critical component of computer science and software engineering.

--

--

Adeena Khanzada

Skilled graphic designer & computer science student. Past member of WTM Hyd & ACM-MUET. A Book Worm.