What Does Contiguous Memory Allocation Mean?
Contiguous memory allocation is a classical memory allocation model. Here, a system assigns consecutive memory blocks (that is, memory blocks having consecutive addresses) to a process.
Contiguous memory allocation is one of the oldest memory allocation methods. Here’s how it works: when a process needs to execute, memory is requested by the process. The size of the process is compared with the amount of contiguous main memory available to execute the process.
If sufficient contiguous memory is found, the memory is allocated and the process starts its execution. Otherwise, the process is added to a queue of waiting processes until sufficient free contiguous memory is available.
Techopedia Explains Contiguous Memory Allocation
Contiguous memory allocation can be implemented in operating systems with the help of two registers, known as the base and limit registers.
When a process is executing in main memory, its base register contains the starting address of the memory location where the process is executing, while the amount of bytes consumed by the process is stored in the limit register.
A process does not directly refer to the actual address for a corresponding memory location. Instead, it uses a relative address with respect to its base register. All addresses referred by a program are considered as virtual addresses. The CPU generates the logical or virtual address, which is converted into an actual address with the help of the memory management unit (MMU).
The base address register is used for address translation by the MMU. Thus, a physical address is calculated as follows:
Physical Address = Base register address + Logical address/Virtual address
The address of any memory location referenced by a process is checked to ensure that it does not refer to an address of a neighboring process. This processing security is handled by the underlying operating system.
Benefits and Disadvantages of Contiguous Memory Allocation
Another way to understand continuous memory allocation is to contrast it with its opposite method: non-contiguous memory allocation. In non-contiguous memory allocation, memory does not have to be assigned in contiguous blocks of addresses.
In contrast to non-contiguous memory allocation, contiguous memory allocation has some distinct benefits. It tends to be faster to execute, easier for the operating system to handle, and often involves less overhead.
However, there are drawbacks to contiguous memory allocation, as well. One of the main ones is that memory can be wasted this way, if the smaller amounts of memory are not utilized due to the need for contiguous memory blocks. Also, processes may wait longer for execution, because the contiguous block is not readily accessible.
Stack and Heap Allocation
Contiguous memory allocation is also interesting in the context of various memory allocation techniques. For example, experts often contrast two types of memory allocation known as “stack allocation” and “heap allocation.”
In stack allocation, memory allocation is pre-arranged and implemented during compiling, where heap allocation is done later at runtime.
It's important to note that stack allocation often takes the form of contiguous memory allocation, where the memory allocation is assigned in blocks. By contrast, in heap allocation, contiguous memory allocation is difficult, because the system has to find, not just sufficient memory, but also memory that is in a large enough contiguous block. As a result, heap allocation is often non-contiguous memory allocation.
Experts also talk about static and dynamic memory allocation methods, and how to implement those using either contiguous or non-contiguous memory allocation methods.