The file allocation strategy is adopted by the operating system to improve the space efficiency and reduce the time complexity. There are mainly two memory allocation techniques that the operating system utilizes to store the file, these are contiguous memory allocation and non-contiguous memory allocation.
Some of the operating systems either use one of these two such as IBM mainframe uses contiguous technique but many operating systems can use both of them, it depends on the need of the task that the operating system is utilized.
What is Contiguous memory allocation ?
Contiguous memory allocation is a method of file allocation where the file blocks are stored in the memory in a sequential manner.
In contiguous memory allocation, the main memory is subdivided into various file blocks and when the data of the files are stored one after other without consideration of size or priority then it is said to be contiguously allocated.
Also, there is a directory that exists in this allocation and whenever any new file is stored it is first broken into fixed-sized blocks to he stored in main memory and the starting file block address is stored in the directory so that when the operating system tries to find the same file later on it can easily search it in the directory and jump to the particular starting address.
The only problem that the contiguous memory allocation faces is the growth of the file since when the contiguous allocation of the file occurs it allocates a complete memory block to the file to store it sequentially. So later when the file needs to add more information it may cause problems because the memory size is already fixed during the allocation and this problem is solved in the non-contiguous memory allocation.
Example of Contiguous Memory Allocation
As already stated the contiguous memory allocation stores the information in a sequential manner. As represented in the above diagram, there are 4 Files in the allocated memory.
Every Allocation method maintains a directory which contains the information about the address of file blocks. Similarly in the contiguous allocation we have a directory which contains the starting address and the length of our 4 files (File_1, File_2, File_3, File_4). The advantage of the directory mechanism is the operating system does not have to scan the complete main memory, it can refer to the directory to find the required file location and then search for the required information in the particular file.
Now, when the first file says File_1 (8Kbs) is created, the operating system divides the File_1 into four equivalent file blocks of size (2Kb) and then stored sequentially in memory blocks (0100,0101,0102,0103). So when the Operating system finds the address of the first file block from the directory then it can search the sequentially placed file blocks to find the required information. That is also the reason that the searching in contiguous memory allocation is faster than non contiguous memory allocation.
Advantage of Contiguous Memory Allocation
- Easy to implement
- Faster Memory Access
- Supports Both Direct and Sequential Access
- Less Disk Head Movement
- Minimal Seek Time
Disadvantage of Contiguous Memory Allocation
- Wastage of Memory Spaces
- Suffers from both internal and external fragmentation
- File Size has to be initialized at time of creation
- File Size cannot grow as it is pre initialized
What is Noncontiguous memory allocation ?
In non-contiguous memory allocation, the file is allocated in a random address rather than sequential order. The reason why they are allocated in a random address is that the non-contiguous memory allocation compares the size of the file block and the size of the memory block and then allocated the block depending on the file size in the memory block whichever it finds first.
Also whenever the file needs to add more information then it allocates more blocks of memory that are present in the memory and the address of the new block is saved in the previous block. This way the non-contiguous memory helps the file to add more information according to the need. The non-contiguous memory allocation also has a directory similar to the contiguous memory allocation.
Example of Noncontiguous Memory Allocation
As stated in the Non Contiguous Memory allocation, the memory is allocated as per the requirement rather than sequentially as in Contiguous Memory Allocation.
In this Allocation method a directory is maintained similar to the Contiguous Memory allocation. The directory contains the starting and the last address of the allocated memory of file blocks.
In our example, a file (File_1) is divided into 4 file-subparts of unequal sizes and then they are stored in the memory blocks randomly(the main memory is split into various memory blocks which have pre-allocated size).
Say first file-subpart(File_1) is stored at a memory block 0, second file-subpart(File_1) is stored at memory block 5, third file-subpart(File_1) is stored at memory block 10,and fourth file-subpart(File_1) is stored at memory block 13. Every file block is linked to each other with the help of pointers.
So when the Operating system finds the starting address from the directory then it reaches to the first file and then it can reach to other file blocks with pointers present in each file.
Advantage of Noncontiguous Memory Allocation
- Better Utilization of Memory
- Does not suffer from external fragmentation
- Free blocks can be utilized for file block requests
- Directory only contains the starting block address
Disadvantages of Noncontiguous Memory Allocation
- Memory is required to store the pointer overhead
- Every file block has to be traversed
- Error in pointer links can corrupt the entire file
Difference between Contiguous and Noncontiguous memory allocation
CONTIGUOUS ALLOCATION | NON CONTIGUOUS ALLOCATION |
The blocks are allocated sequentially in the memory | The blocks are allocated randomly in the memory |
The technique is faster in execution because the searching of the file is quick | The technique is slower in execution because the searching of the file is slow |
There are fewer numbers overheads to store the address of the next file blocks and therefore less number of address translation | There is more number of overheads it requires to store the address of file blocks and therefore more number of address translation |
The internal and external fragmentation occurs in the contiguous memory allocation | The non-contiguous memory allocation completely eliminate the fragmentation that occurs in the main memory |
The file growth is limited and difficult to store more information | The file growth is not limited and the information storage can occur easily |
Fragmentation leads to wastage of memory space | There is no wastage of memory and there is non-fragmentation |
The size of the file has to be declared during the time of storage of the memory block and is fixed from thereon | The size of the file can be increased by adding overheads of the next block address therefore it does have to be declared earlier |
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.
Contiguous memory allocation is basically a method in which a single contiguous section/part of memory is allocated to a process or file needing it
This is most premium information, really we are totally inspired with this valuable content. Thank for sharing!