First Fit program in JAVA

The operating system is also refereed as the resource manager which allocates the different resources to the processes. One of such processes is the operating system memory which is allocated based on the demand of the processes.  The operating system uses different schemes for this purpose, the most common among them are, first fit, best fit, and worst fit.

The first fit memory allocation scheme is a technique through which the operating system allocates memory to the processes based on the memory demands. In this method, the operating system allocates the first memory slot it finds empty to a process in demand. This memory allocation is not considered as a good technique as it allocates huge empty memory spaces even to small process which results in massive memory wastages.

Read AlsoFirst Fit Algorithm in python

Program for First Fit in JAVA

class PrepInsta


// Function to implement First Fit allocation scheme

    static void Firstfit(int blockSize[], int p,

    int processSize[], int s)


// Code to store the block id for block allocation

        int allocate[] = new int[s];

// No block assigned at program initiation

        for (int i = 0; i < allocate.length; i++)

        allocate[i] = -1;

// Pick every process and look for a suitable block

        for (int i = 0; i < s; i++)


        for (int j = 0; j < p; j++)


        if (blockSize[j] >= processSize[i])


// allocation of block j to p[i] process

        allocate[i] = j;

// Available memory in the block is reduced

        blockSize[j] -= processSize[i];





       System.out.println("\nProcess Number\tProcess Size\tBlock Number.");

       for (int i = 0; i < s; i++)


       System.out.print(" " + (i+1) + "\t\t" +processSize[i] + "\t\t");

       if (allocate[i] != -1)

       System.out.print(allocate[i] + 1);


         System.out.print("Not Allocated");




// Driver Code

       public static void main(String[] args)
       int blockSize[] = {100, 500, 200, 300, 600};

       int processSize[] = {212, 417, 112, 426};

       int p = blockSize.length;

       int s = processSize.length;

       firstFit(blockSize, p, processSize, s);




Process Number               Process Size                        Block Number

1                                              212

2                                              417

3                                              112

4                                             426                                         Not Allocated

Please Login/Signup to comment