Operating System - Non-Contiguous Memory Allocation



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. In the last chapter, we discussed contiguous memory allocation. This chapter will focus on non-contiguous memory allocation, how it is implemented in operating systems, and its advantages and disadvantages.

Non-Contiguous Memory Allocation

Non-contiguous memory allocation is a memory allocation technique where a process is allocated multiple blocks of memory at different locations in the main memory. Sometimes the entire process may not be stored in the main memory. i.e., some parts of the process may be stored in the secondary storage (like hard disk). When the process needs to access a part of the program that is not in the main memory, the OS will immediately swap that part to the main memory.

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

Non-Contiguous Memory Allocation

In the image above, the process is divided into pages (Page 1, Page 2, Page 3, ... page 7). The first 2 pages are loaded into the physical memory (RAM), and rest are stored in the hard disk.

We have two types of non-contiguous memory allocation techniques −

  • Paging
  • Segmentation

Paging in Non-Contiguous Memory Allocation

Paging is a memory management technique used by modern operating systems to manage the allocation of memory to processes. In this technique, the physical memory (i.e., the RAM) is divided into blocks of fixed size called frames, and the logical memory (i.e., the memory where the process is stored) is divided into blocks of the same size called pages. When a process is executed, its pages are loaded into available frames in the physical memory.

The image below shows working of paging in operating system −

Paging in Operating System
  • The idea behind implementing paging is to store the part of the program that is currently in use in the physical memory (RAM) and keep the rest of the program in the hard disk.
  • When the process needs to access a part of the program, the OS will check page table to see if the page is in the physical memory or not.
  • If the page is in the physical memory, the CPU can directly access it. If not, a page fault will occur. So the OS will load the page from the hard disk into the physical memory and update the page table.
  • While loading a new page into the physical memory, if the space is full, the OS will use a page replacement algorithm to decide which page to remove and make space for the new page in RAM.

Segmentation in Non-Contiguous Memory Allocation

Segmentation is another memory management technique used by modern operating systems to manage the allocation of memory to processes. Here, the logical memory (i.e., the memory where the process is stored) is divided into variable-sized segments based on the logical structure of a program, such as code segment, data segment, stack segment, etc. The physical memory (i.e., the RAM) is also divided into variable-sized blocks called segments. When a process is executed, its segments are loaded into available segments in the physical memory.

The main difference between paging and segmentation is that in paging, the memory is divided into fixed-size blocks. In segmentation, the memory is divided into blocks of variable size based on the logical structure of a program.

Features of Segmentation

The key features of segmentation are −

  • Logical division of memory − Segments represent meaningful units like code, stack, data, or modules.
  • Variable-sized divisions − Segments can have different lengths, depending on the program's requirements.
  • No internal fragmentation − Since segments are not fixed in size.
  • External fragmentation − Can occur when free memory is divided into small scattered blocks.
  • Protection and sharing − Different segments can have access rights (read, write, execute) and can be shared among processes.

Advantages of Non-Contiguous Memory Allocation

Most of the modern operating systems uses non-contiguous memory allocation technique. It have several advantages over contiguous memory allocation technique. Some of the key advantages are −

  • Reduced Memory Wastage − The chances of fragmentation are reduced in non-contiguous memory allocation. This will eliminate memory wastage issues and improve overall memory utilization.
  • Run Large Processes − By implementing non-contiguous memory allocation, the operating system can run processes that are larger than the available physical memory (RAM). This is done by storing some parts of the process in the secondary storage (like hard disk).
  • Flexibility − Non-contiguous memory allocation provides more flexibility in memory management.

Disadvantages of Non-Contiguous Memory Allocation

Even though non-contiguous memory allocation has several advantages, it also comes with some drawbacks. Some of the key disadvantages are −

  • Complexity − Non-contiguous memory allocation is more complex to implement and manage compared to contiguous memory allocation. The operating system needs to maintain data structures like page tables and segment tables to keep track of the memory blocks.
  • Slower Access Time − Due to repeated swapping of pages/segments and additional lookups in page/segment tables, the overall access time may be slower.
  • Overhead − Non-contiguous memory allocation has higher overhead due to the need for additional data structures and algorithms to manage memory allocation.

Conclusion

Non-contiguous memory allocation is a memory allocation is advanced technique used by modern operating systems to manage memory without any wastage. In this technique, a process is allocated multiple blocks of memory at different locations in the main memory. The two main types of non-contiguous memory allocation techniques are paging and segmentation. By non-contiguous memory allocation, you can even run processes that are larger than the available physical memory (RAM).

Advertisements