Worst Fit Algorithm
Worst Fit Algorithm
Worst Fit Algorithm
Systems
Last Updated : 03 Feb, 2021
For both fixed and dynamic memory allocation schemes, the operating system must keep
a list of each memory location noting which are free and which are busy. Then as new
jobs come into the system, the free partitions must be allocated.
These partitions may be allocated in 4 ways:
1. First-Fit Memory Allocation
2. Best-Fit Memory Allocation
3. Worst-Fit Memory Allocation
4. Next-Fit Memory Allocation
These are Contiguous memory allocation techniques.
Worst-Fit Memory Allocation :
In this allocation technique, the process traverses the whole memory and always search
for the largest hole/partition, and then the process is placed in that hole/partition. It is a
slow process because it has to traverse the entire memory to search the largest hole.
Here is an example to understand Worst Fit-Allocation –
Program for Worst Fit algorithm in
Memory Management
Difficulty Level : Easy
Last Updated : 28 Jun, 2021
// algorithm
int n)
{
// process
allocation[i] = -1;
{
{
{
wstIdx = j;
wstIdx = j;
}
}
// If we could find a block for current process
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
{
System.out.print(allocation[i] + 1);
else
System.out.print("Not Allocated");
System.out.println();
}
}
{
int m = blockSize.length;
int n = processSize.length;
}
Output:
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
Here Process P1=30K is allocated with the Worst Fit-Allocation technique, so it traverses
the entire memory and selects memory size 400K which is the largest, and hence there is
an internal fragmentation of 370K which is very large and so many other processes can
also utilize this leftover space.
Advantages of Worst-Fit Allocation :
Since this process chooses the largest hole/partition, therefore there will be large internal
fragmentation. Now, this internal fragmentation will be quite big so that other small
processes can also be placed in that leftover partition.
Disadvantages of Worst-Fit Allocation :
It is a slow process because it traverses all the partitions in the memory and then selects
the largest partition among all the partitions, which is a time-consuming process.
#include<bits/stdc++.h>
// algorithm
int n)
// process
int allocation[n];
{
{
{
wstIdx = j;
wstIdx = j;
}
}
{
// allocate block j to p[i] process
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
else
}
}
// Driver code
int main()
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;