Skip to main content

First-fit vs Best-fit Allocation algorithms

What are they?

First-fit and Best-fit are memory allocation schemes that were typically used in dynamic and fixed partition memory allocation schemes to allocate memory.

To simplify what they are, consider the following list of jobs and the available resources.


The above figure shows a list of jobs and the resources to which these jobs could be allocated. 

Here to allocate the jobs, we use either the first-fit memory allocation scheme or the best-fit memory allocation scheme.


  • The first-fit memory allocation scheme allocates on the basis of the first partition fitting the requirements. 
  • The best-fit memory allocation scheme allocates on the basis of the best partition (one with the least memory wastage) fitting the requirements.


Let us allocate the jobs first on the basis of first-fit memory allocation and then on the basis of best-fit memory allocation to understand how jobs are allocated and the pros and cons of the two schemes.

Allocating using First-fit memory allocation

Consider the same image.


Here we are using First-fit memory allocation. Therefore, jobs have to be allocated on the basis of the first partition meeting the requirements.











First consider Job 1. The memory size of Job 1 is 50K. When we search through the available partitions, we can see that the first partition could hold up to 70K. Therefore, Job 1 could be allocated to this partition. Since it is the the first partition meeting the requirements, the memory size of other partitions need not be compared.

Now consider Job 2. Job 2 cannot be allocated to partition 1 as Job 1 is already allocated to it. Then the next available partition could hold up to 40K and the size of Job 2 is 30K. Therefore, Job 2 is allocated to partition 2.

Next consider Job 3. Job 3 has to be allocated to either partitions 3 or 4 as partitions 1 and 2 have already been allocated. The size of Job 3 is 25K and partition 3 could hold up to 30K. Therefore, Job 3 could be allocated to partition 3.

Finally consider Job 4. The only remaining partition that Job 4 could be allocated is partition 4. However, the maximum memory that this partition can hold is 25K and the size of Job 4 is 30K. Therefore, the size of Job 4 > the partition to which it could have been allocated. Therefore, Job 4 has to wait until either of Job 1 or Job 2 or Job 3 runs to completion.

The final memory allocation graph could be summarized as follows:
















The memory wastage due to the allocation on the basis of this scheme is as follows:


















Allocating using Best-fit memory allocation



Here we are using Best-fit memory allocation. Therefore, jobs have to be allocated on the basis of the best partition (partition with the least memory wastage) meeting the requirements.









First consider Job 1. The memory size of Job 1 is 50K. When we search through the available partitions, we can see that the best partition that could hold 50K is the first partition as other partitions cannot hold that amount of memory. Therefore, Job 1 could be allocated to this partition. 

Now consider Job 2. Job 2 cannot be allocated to partition 1 as Job 1 is already allocated to it. The second partition could hold up to 40K and the third partition could hold up to 30K. The size of Job 2 is 30K. Since we are considering a partition with the least wasted space or least memory wastage when allocated, Job 2 is allocated to partition 3. If allocated to partition 2 as done using first-fit, then there will be a 10K wastage. No wastage is incurred if allocated to partition 3. Therefore, partition 3 would be the ideal option for Job 2.

Next consider Job 3. Job 3 has to be allocated to either partitions 2 or 4 as partitions 1 and 3 have already been allocated. The size of Job 3 is 25K and partition 2 could hold up to 40K and partition 4 could hold up to 25K. Since we are considering a partition with the least wasted space or least memory wastage when allocated, Job 3 is allocated to partition 4. If allocated to partition 2  then there will be a 15K wastage. No wastage is incurred if allocated to partition 4. Therefore, partition 4 would be the ideal option for Job 3.Therefore, Job 3 could be allocated to partition 4.

Finally consider Job 4. The only remaining partition that Job 4 could be allocated is partition 2. Since the size of the partition is greater than the memory size of Job 4, Job 4 could be allocated to partition 2.

By this method and for this example, no jobs are waiting as opposed to first-fit memory allocation as described above. However, this might not be true for every example when using best-fit memory allocation (i.e. best-fit could also result in jobs to wait for allocation).

The final memory allocation graph could be summarized as follows:
















The memory wastage due to the allocation on the basis of this scheme is as follows:

















As you could see the memory was best allocated using best-fit memory allocation, resulting in only 30K internal fragmentation as opposed to 35K using first-fit scheme.

However, the entire list needs to be searched when using best-fit memory allocation before a job is allocated to partitions. This is a very cumbersome operation if there a re large no. of partitions in the system.

Therefore, best-fit memory allocation is relatively slow in allocating memory.

Advantages and disadvantages of the two schemes











Note: Although partitions were shown by graphical figures in the above explanation, actually the OS accesses these partitions in the form of tables (the job list refers to the memory list). 

In fact for allocation two separate memory lists called the free list and the busy list are used. Free list shows the partitions that are free and if matched where jobs could be allocated. The busy list consist of the partitions where the jobs were allocated.

To enhance clarity of these two mechanisms and to get a rough idea of the actual memory allocation, the following two tables shoe how the memory is allocated in a tabulated manner. The list consist of both the busy list and the free list. At any time the two lists could be separated by identifying which partitions were allocated (shift to busy list) and which partitions are free (shift to free list).


Using first-fit memory allocation:
















Using best-fit memory allocation:



Comments

Popular posts from this blog

Honeypots and Honeynets

What is a honeypot? Honeypots are systems used to gather information about the activity of attackers or intruders to a system. It acts like a trap to detect how a user approaches/intercepts a system, how they behave once intercepted and stores these data into its database (here the database means a storage area, not a collection of data records). A honeypot placed within the DMZ What makes a honeypot? Building a honeypot requires a PC with more preferably a UNIX based operating system and a sniffer tool. (A sniffer tool provides the capability of seeing the traffic going between the firewall and the honeypot)  Where can honeypots be placed? Honeypots can be placed anywhere in the system. They may be placed outside the DMZ, inside the DMZ or even on the internal network.  Honeypots are additional security system. Honeypots differ from firewalls in that honeypots do not filter the traffic passing them and honeypots differ from intrusion detection systems

OS security (Windows NT logon process)

What is the Windows NT logon process? Windows NT process is the process by which the operating systems belonging to the Windows NT family start up. The logon process for operating systems introduced by Microsoft since Windows Vista uses a slightly different architecture (methodical steps), but many steps in the windows NT process have been repeated There are many steps involved with the Windows NT logon process but this blog post summarizes the most important steps and definitions. The basic architecture of this process can be summarized as follows: A simplified Windows NT logon process Now let us understand what each section of the above flow diagram does in the Windows NT logon process. Main sections of the logon process- Security reference monitor- The security reference monitor is used to ensure which subjects have authorization to which objects. Thus, it uses the access control policy used in the operating system for its basic functioning. A security refere

Goals of computer security

What are the goals of computer security? The number of goals concerning computer security is highly debatable. However, every thoery that is been presented to date ensures three main types of goals: confidentiality, integrity and availability. In this blog post I have mentioned five types of security goals: confidentiality, integrity, availability, authenticity and non-repudiation / accountability. The following figure summarizes what each of these goals mean and who are involved with these goals and what needs to be done to ensure the goals' performance. Click on image to zoom Confidentiality- Confidentiality means that information or services should only be accessed by authorized personnel.  Click on image to zoom   Integrity-   Integrity means that information or services should only be modified by authorized personnel. Click on image to zoom Availability- Information or services should be available to authorized personnel when

Multiprocessing Configurations

Multiprocessing is the use of two or more processors to share system resources. Multiprocessing enhances a system's performance by increasing reliability and enhancing faster processing. For example, consider a simple set of jobs as follows: When the execution is done using a single processor, only one job could be executed at a time. The other jobs have to wait until the job which is being currently executed runs to completion or preempted. This seriously degrades system performance since the waiting queues increase over time and the throughput is decreased. Some jobs may even lead to starvation (more about these concepts in the blog post on deadlocks and starvation). However, when the execution is done using multiple processors, many jobs can execute at the same time. This does not eliminate waiting queues or aging, but generally reduce them. Moreover the throughput is increased while decreasing the turnaround time. However, with every new concept comes a n
DMCA.com Protection Status