Operating System - Contiguous Memory Allocation



In the context of operating systems, memory allocation refers to the process of assigning blocks of memory to various programs and processes running on a computer. The blocks of memory can be allocated continuously or non-continuously. Based on this, we have two types of memory allocation techniques: contiguous memory allocation and non-contiguous memory allocation.

In this chapter, we will discuss contiguous memory allocation, how to implement it in operating systems, and its advantages and disadvantages.

Contiguous Memory Allocation

Contiguous memory allocation as the name suggests, allocates a single continuous block of memory to a process. The block of memory is allocated to a process is enough to hold the entire process. This means that the process can access all its memory locations without any interruptions.

The image below shows the main memory allocates storage to three processes P1, P2, and P3 using contiguous memory allocation −

Contiguous Memory Allocation

In the image above, the process P1 is allocated 3 blocks of memory (F1, F2, and F3). P2 is allocated 2 blocks of memory (F5 and F6). Similarly, P3 is allocated 1 block of memory (F8). There some gaps in between the allocated memory blocks ( at F4 and F7 ). How these gaps are formed even though we are continuously allocating memory? To understand this, read our chapter on Fragmentation.

We have two types of contiguous memory allocation techniques −

  • Fixed Partitioning (Static)
  • Dynamic Partitioning (Variable)

Fixed Partitioning in Contiguous Memory Allocation

The fixed partitioning or static partitioning is a memory allocation technique where the main memory is divided into partitions of fixed sizes. Each partition can hold exactly one process. When a new process loaded into memory, it is allocated a partition of fixed size. If the process is smaller than the partition size, the remaining memory in the partition will be wasted. If the process is larger than the partition size, it cannot be loaded into memory.

The image below shows internal fragmentation happening in main memory during fixed partitioning −

Fixed Partitioning in Contiguous Memory Allocation

As the image above shows, one of main drawbacks of fixed partitioning is internal fragmentation. We can see that process are not using the entire allocated memory. This will lead to a situation where even though there is enough total memory available, it may not be possible to allocate memory to a new process, because the available memory may not be contiguous.

Dynamic Partitioning in Contiguous Memory Allocation

The dynamic partitioning or variable partitioning is a memory allocation technique where the main memory is divided into partitions of varying sizes depending on the size of the processes being loaded into memory. When a new process is loaded into memory, it is allocated to the first available partition that is large enough to hold the process. If no such partition is available, the process has to wait until a partition becomes free.

The image below shows external fragmentation happening in main memory during dynamic partitioning −

Dynamic Partitioning in Contiguous Memory Allocation

The major issue with dynamic partitioning is external fragmentation. In the image above, we can see that there are some gaps in the memory after allocating and deallocating memory to various processes. This situation happens when a process is terminated from memory, and it's neighboring processes are still running.

These gaps will add up over time as more processes are loaded and unloaded from memory. In such case, you need to restart the system to make the main memory free again.

Advantages of Contiguous Memory Allocation

The contiguous memory allocation technique is preferred in some scenarios due to the following advantages −

  • Simplicity − Contiguous memory allocation is simple to implement and manage. The operating system can easily keep track of the memory blocks allocated to each process.
  • Fast Access − The entire process is stored in a single block of memory. No any calculation is needed to find the memory location of a process. So overall access time is faster.
  • Low Overhead − Contiguous memory allocation has low overhead as there is no need for complex data structures to manage memory allocation.

Disadvantages of Contiguous Memory Allocation

Even though contiguous memory allocation is simple and fast, it comes with some drawbacks −

  • Wastage of Memory − During contiguous memory allocation, there are gaps created in memory due loading and unloading of processes. This can lead to wastage of memory.
  • Fragmentation − Contiguous memory allocation can lead to internal and external fragmentation. This reduce the overall efficiency of memory usage.
  • Limited Flexibility − Contiguous memory allocation is not suitable for virtual memory systems and in the case where process are larger than the available memory itself.

Conclusion

Contiguous memory allocation is a simple and fast memory allocation technique used by operating systems. There are two types of contiguous memory allocation techniques: fixed partitioning and dynamic partitioning. The main drawback of contiguous memory allocation is wastage of memory due to fragmentation. However, it is still used in the case OS focuses on simplicity and speed over memory efficiency.

Advertisements