Contiguous Memory Allocation in OS

Learn via video courses
Topics Covered

Overview

Contiguous Memory Allocation is a type of memory allocation technique where processes are allotted a continuous block of space in memory. This block can be of fixed size for all the processes in a fixed-size partition scheme or can be of variable size depending on the requirements of the process in a variable-size partition scheme.

What is Contiguous Memory Allocation in OS?

Contiguous memory allocation in the operating system is a memory allocation technique. But what is memory allocation? When a program or process is to be executed, it needs some space in the memory. For this reason, some part of the memory has to be allotted to a process according to its requirements. This process is called memory allocation.

One such memory allocation technique is contiguous memory allocation. As the name implies, we allocate contiguous blocks of memory to each process using this technique. So, whenever a process wants to enter the main memory, we allocate a continuous segment from the totally empty space to the process based on its size.

Contiguous Memory Allocation Techniques

Whenever a process has to be allocated space in the memory, following the contiguous memory allocation technique, we have to allot the process a continuous empty block of space to reside. This allocation can be done in two ways:

  1. Fixed-size Partition Scheme
  2. Variable-size Partition Scheme

Let us look at both of these schemes in detail, along with their advantages and disadvantages.

Fixed-size Partition Scheme

In this type of contiguous memory allocation technique, each process is allotted a fixed-size continuous block in the main memory. That means there will be continuous blocks of fixed size into which the complete memory will be divided, and each time a process comes in, it will be allotted one of the free blocks. Because irrespective of the size of the process, each is allotted a block of the same size memory space. This technique is also called static partitioning.

Fixed-size Partition Scheme

In the diagram above, we have 3 processes in the input queue that have to be allotted space in the memory. As we are following the fixed-size partition technique, the memory has fixed-sized blocks. The first process, which is of size 3MB is also allotted a 5MB block, and the second process, which is of size 1MB, is also allotted a 5MB block, and the 4MB process is also allotted a 5MB block. So, the process size doesn't matter. Each is allotted the same fixed-size memory block.

It is clear that in this scheme, the number of continuous blocks into which the memory will be divided will be decided by the amount of space each block covers, and this, in turn, will dictate how many processes can stay in the main memory at once.

Note: The number of processes that can stay in the memory at once is called the degree of multiprogramming. Hence, the degree of multiprogramming of the system is decided by the number of blocks created in the memory.

Advantages

The advantages of a fixed-size partition scheme are:

  1. Because all of the blocks are the same size, this scheme is simple to implement. All we have to do now is divide the memory into fixed blocks and assign processes to them.
  2. It is easy to keep track of how many blocks of memory are left, which in turn decides how many more processes can be given space in the memory.
  3. As at a time multiple processes can be kept in the memory, this scheme can be implemented in a system that needs multiprogramming.

Disadvantages

Though the fixed-size partition scheme has many advantages, it also has some disadvantages:

  1. As the size of the blocks is fixed, we will not be able to allot space to a process that has a greater size than the block.
  2. The size of the blocks decides the degree of multiprogramming, and only that many processes can remain in the memory at once as the number of blocks.
  3. If the size of the block is greater than the size of the process, we have no other choice but to assign the process to this block, but this will lead to much empty space left behind in the block. This empty space could've been used to accommodate a different process. This is called internal fragmentation. Hence, this technique may lead to space wastage.

internal fragmentation

Variable-size Partition Scheme

In this type of contiguous memory allocation technique, no fixed blocks or partitions are made in the memory. Instead, each process is allotted a variable-sized block depending upon its requirements. That means, that whenever a new process wants some space in the memory, if available, this amount of space is allotted to it. Hence, the size of each block depends on the size and requirements of the process which occupies it.

Variable-size Partition Scheme

In the diagram above, there are no fixed-size partitions. Instead, the first process needs 3MB memory space and hence is allotted that much only. Similarly, the other 3 processes are allotted only that much space that is required by them.

As the blocks are variable-sized, which is decided as processes arrive, this scheme is also called Dynamic Partitioning.

Advantages

The advantages of a variable-size partition scheme are:

  1. As the processes have blocks of space allotted to them as per their requirements, there is no internal fragmentation. Hence, there is no memory wastage in this scheme.
  2. The number of processes that can be in the memory at once will depend upon how many processes are in the memory and how much space they occupy. Hence, it will be different for different cases and will be dynamic.
  3. As there are no blocks that are of fixed size, even a process of big size can be allotted space.

Disadvantages

Though the variable-size partition scheme has many advantages, it also has some disadvantages:

  1. Because this approach is dynamic, a variable-size partition scheme is difficult to implement.
  2. It is difficult to keep track of processes and the remaining space in the memory.

Strategies Used for Contiguous Memory Allocation Input Queues

So far, we've seen the two types of schemes for contiguous memory allocation. But what happens when a new process comes in and has to be allotted a space in the main memory? How is it decided which block or segment it will get?

Processes that have been assigned continuous blocks of memory will fill the main memory at any given time. However, when a process completes, it leaves behind an empty block known as a hole. This space could also be used for a new process. Hence, the main memory consists of processes and holes, and any one of these holes can be allotted to a new incoming process. We have three strategies to allot a hole to an incoming process:

First-Fit

This is a very basic strategy in which we start from the beginning and allot the first hole, which is big enough as per the requirements of the process. The first-fit strategy can also be implemented in a way where we can start our search for the first-fit hole from the place we left off last time.

Best-Fit

This is a greedy strategy that aims to reduce any memory wasted because of internal fragmentation in the case of static partitioning, and hence we allot that hole to the process, which is the smallest hole that fits the requirements of the process. Hence, we need to first sort the holes according to their sizes and pick the best fit for the process without wasting memory.

Worst-Fit

This strategy is the opposite of the Best-Fit strategy. We sort the holes according to their sizes and choose the largest hole to be allotted to the incoming process. The idea behind this allocation is that as the process is allotted a large hole, it will have a lot of space left behind as internal fragmentation. Hence, this will create a hole that will be large enough to accommodate a few other processes.

Conclusion

  • In contiguous memory allocation, we allocate contiguous blocks of memory to each process when it is brought in the main memory to be executed.
  • There are two techniques for contiguous memory allocation:
    • Fixed Size Partitioning: Each process is allotted to a fixed size continuous block in the main memory.
    • Variable Size Partitioning: Each process is allotted space depending upon its requirements. There is no defined fixed-size block.
  • There are three strategies to allot a hole to an incoming process:
    • First-Fit: Allot the process to the first hole, which is big enough.
    • Best-Fit: Allot the smallest hole that satisfies the requirements of the process.
    • Worst-Fit: Allot the largest size hole among all to the incoming process.

See More