Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In computer science and operating systems, memory management stands as one of the foundational pillars. Within this realm, allocation strategies play a pivotal role in how resources are distributed and utilized. Two prominent methods, Contiguous and Non-Contiguous Allocation, govern this distribution, each offering unique advantages and challenges.
In this article, we will study the difference between contiguous and non-contiguous memory allocation methods.
To understand the differences, we must know what contiguous and non-contiguous memory allocation techniques are. Let us briefly look at the contiguous memory allocation technique.
What is Contiguous Allocation?
Before getting towards contiguous allocation, we must know that the memory is divided into two parts: the operating system and the other for processes. The operating system can reside either in the lower part of the memory or the upper part.
Contiguous memory allocation is a method of allocating a single contiguous block of memory to a process that requires it. As a result, all available memory space resides in the same location, which means unused available memory partitions are not distributed randomly across the entire memory space.
There are two ways of performing contiguous memory allocation. These are-
Also known as static partitioning, the memory is partitioned into blocks of fixed size.
For example, in the below diagram, the memory is divided into five blocks, each of size 4 MB.
If a process of size 4 MB comes, it will be easily allocated to any of the 4 MB memory blocks.
If a process of less than 4 MB comes, we can easily allocate that process in the memory. Still, that process will suffer internal fragmentation since a whole block of 4 MB will be allocated, which is not required, and thus the leftover memory will be wasted.
Moreover, if a process size greater than 4 MB comes, we cannot allocate that process in the memory since the spanning is not allowed in fixed-sized partitioning.
Variable size partitioning
Also known as dynamic partitioning, the memory is divided into blocks of varying sizes. At run-time, processes request blocks of main memory from variable-sized memory partitions. If enough main memory is available, the process will be allotted a block of main memory with the same size as the process requires. For example, a process P1 of size 4 MB comes into the memory. After that, another process P2 of size 12 MB comes into the memory, and then another process P3 of size 5 MB comes into the memory. In the memory, these processes will look like this-
This method overcomes internal fragmentation but suffers from external fragmentation. If the process P1 frees the memory block, then that space will only be used by a process less than or equal to 4 MB. Till then, the space will remain wasted.
Advantages of Contiguous Memory Allocation
Efficiency: Contiguous allocation allows for faster memory access and manipulation since the entire block of memory is contiguous, reducing overhead.
Simplicity: Managing contiguous memory is relatively simpler, requiring less complex data structures and algorithms compared to non-contiguous allocation methods.
Cache Performance: Contiguous memory layouts tend to result in better cache performance, as data stored closer together can be fetched more efficiently by the processor.
Disadvantages of Contiguous Memory Allocation
Fragmentation: Over time, contiguous memory allocation can lead to fragmentation, where the available memory is divided into small, unusable chunks.
Limited Flexibility: Contiguous allocation may impose restrictions on memory usage, as it requires contiguous blocks of memory, limiting the ability to allocate large chunks of memory in a fragmented system.
External Fragmentation: External fragmentation occurs when free memory blocks are scattered throughout memory, making it challenging to allocate contiguous blocks of memory, especially for larger requests.
What is Non-Contiguous Allocation?
Another method of memory management is non-contiguous allocation. This method allocates memory space present in different locations to the process based on its needs. Since all the available memory space is distributed, the freely available space is also scattered here and there.
Paging is a non-contiguous memory management technique that allows the operating system to fetch processes from secondary memory and store them in the main memory in the form of pages.
In paging, the process in secondary memory is divided into multiple pages, and the main memory is divided into various frames. Both the pages and frames are of equal sizes.
For example, there is a process P of size 500 KB. It is divided into five pages P1, P2, P3, P4, and P5, each size 100 KB in the secondary memory. Main memory is also divided into frames of size 100 KB. Based on the availability of the frames, these five pages will be loaded from secondary memory to main memory.
For getting the address of these pages from secondary memory, the operating system maintains a page table of each process.
Now let’s look at the second type of non-contiguous technique i.e., segmentation.
Segmentation
Segmentation is similar to paging, but in segmentation, the process is divided into parts of variable sizes, unlike paging, which is divided into pages of fixed sizes. Each segmented part is known as a segment. Segmentation is basically done to achieve the user's view of memory allocation.
For example, there is a process P of size 500 KB. It is divided into five segments, S0, S1, S2, S3 and S4, each of variable size in the secondary memory. These five segments will be loaded non-contiguously from the secondary memory into the main memory.
For getting the address of these segments from secondary memory, the operating system maintains a segment table of each process.
Since we have an overview of the contiguous and non-contiguous memory allocation techniques, we can now study the differences between them.
Advantages of Non-Contiguous Memory Allocation
Flexibility: Non-contiguous allocation allows for more flexible memory management, as it can allocate memory in non-contiguous chunks, accommodating fragmented memory spaces efficiently.
Reduced Fragmentation: Non-contiguous allocation helps reduce fragmentation by fitting smaller memory blocks into available spaces, mitigating the issue of wasted memory.
Dynamic Memory Allocation: Non-contiguous allocation techniques, such as paging and segmentation, support dynamic memory allocation, enabling efficient use of memory resources.
Disadvantages of Non-Contiguous Memory Allocation
Overhead: Non-contiguous allocation methods often come with additional overhead, such as page tables or segment tables, to manage the mapping between logical and physical memory addresses.
Complexity: Managing non-contiguous memory can be more complex, requiring sophisticated algorithms and data structures to handle fragmented memory spaces efficiently.
Potential for Thrashing: In certain scenarios, non-contiguous allocation techniques like paging may lead to thrashing, where the system spends more time swapping pages between memory and disk than executing useful work.
The task of dividing the memory efficiently among various processes by the operating system is known as memory management. This is basically done to achieve a high degree of multiprogramming and manage the memory space wisely. There are two ways to allocate memory to a process-
Contiguous allocation
Non-contiguous allocation
What is the difference between contiguous allocation and linked allocation?
Contiguous allocation assigns memory as a single, continuous block, while linked allocation divides memory into smaller, linked chunks. Contiguous allocation may cause fragmentation, whereas linked allocation mitigates this issue.
What is the difference between fixed-size partitioning and variable-size partitioning?
Fixed-size partitioning allocates memory into fixed-size blocks, simplifying management but potentially leading to internal fragmentation. Variable-size partitioning allocates memory dynamically, reducing fragmentation but requiring more complex management.
What is the difference between paging and segmentation?
Paging divides memory into fixed-size blocks, while segmentation divides it into variable-sized segments. Paging simplifies memory management but can cause internal fragmentation, while segmentation provides flexibility but requires more complex handling.
Conclusion
In this article, we went through the difference between contiguous and non-contiguous memory management techniques. First, we got a quick overview of both methods and then discussed their differences.
Attention Reader! Don’t stop here. Start your learning journey in the operating system with Coding Ninjas by taking the OS course.