Next Fit In Java

Next Fit in Java

The Next Fit algorithm can also be said as the modified version of First Fit algorithm. The memory search method starts as a first fit to find a free memory space, but when the method is called the second time, it searches the memory from where it left off, not from the beginning. The concept uses a moving pointer which moves along the memory blocks to search for an empty space for the next fit.  The overall concept helps to avoid using the memory always from the starting point or the head of the free block chain.

The Next Fir Algorithm can also be said as the changed version of the First Fit algorithm as its basic concept is similar to the first fit method of searching memory. Also, this method does not allocate memory from the beginning of memory allocation. Therefore, the operating system uses scheduling algorithms for the memory allocation method.

Algorithm for memory allocation using Next Fit

Step 1. Start

Step 2. Enter the number of memory blocks.

Step 3. Enter the size of each memory block.

Step 4. Enter the number of processes with their sizes.

Step 5. Start by selecting each process to check if it can be assigned to the current memory block.

Step 6. If the condition in step 4 is true, then allocate the process with the required memory and check for the next process from the memory block where the searching was halted, not from the starting.

Step 7. If the current memory size is smaller, then continue to check the next blocks.

Step 8. Stop

Read Also: Next Fit in Python

Java Program for Next Fit Memory Management 

// Java code for next fit 

import java.util.Arrays; 

public class GFG { 
// Method to allocate memory to the blocks following the Next fit algorithm 

static void Next_Fit(int block_size[], int m, int process_size[], int n) 
 // The code will store the block id for a block which is assigned to a process
        int allocate[] = new int[n], j = 0; 
  // No block is assigned to any process at the beginning 
 Arrays.fill(allocate, -1); 
// Find a suitable block for each process as per its size and assign memory to it  
        for(int i = 0; i < n; i++) { 
            // Not starting from the beginning
            while (j < m) { 
                if (block_size[j] >= process_size[i]) { 
                    // block j is allocated to p[i] process 
                    allocate[i] = j; 
                    // Reduce available memory in this block. 
                    Block_size[j] -= process_size[i]; 

  // mod m will traverse the blocks from the starting when the pointer reaches at the end. 
                j = (j + 1) % m; 
        System.out.print("\nProcess No.\tProcess Size\tBlock no.\n"); 
        for (int i = 0; i < n; i++) { 
            System.out.print( i + 1 + "\t\t" + process_size[i] 
                    + "\t\t"); 
            if (allocate[i] != -1) { 
                System.out.print(allocate[i] + 1); 
            } else { 
                System.out.print("Not Allocated"); 
// Driver program 
    static public void main(String[] args) { 
        int block_size[] = {5, 10, 20}; 
        int process_size[] = {10, 20, 5}; 
        int m = block_size.length; 
        int n = process_size.length; 
        NextFit(block_size, m, process_size, n); 


Process No.	Process Size	Block No.
1		10		2
2		20		3
3		5		1