 
- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- OS - Processes
- OS - Process Control Block
- OS - Operations on Processes
- OS - Inter Process Communication
- OS - Context Switching
- OS - Multi-threading
- Scheduling Algorithms
- OS - Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- OS - TAT & WAT
- Predicting Burst Time in SJF Scheduling
- Process Synchronization
- OS - Process Synchronization
- OS - Critical Section Problem
- OS - Critical Section Synchronization
- OS - Mutual Exclusion Synchronization
- OS - Semaphores
- OS - Counting Semaphores
- OS - Mutex
- OS - Turn Variable
- OS - Bounded Buffer Problem
- OS - Reader Writer Locks
- OS - Test and Set Lock
- OS - Peterson's Solution
- OS - Monitors
- OS - Sleep and Wake
- OS - Race Condition
- OS Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- Memory Management
- OS - Memory Management
- OS - Contiguous Memory Allocation
- OS - Non-Contiguous Memory Allocation
- OS - First Fit Algorithm
- OS - Next Fit Algorithm
- OS - Best Fit Algorithm
- OS - Worst Fit Algorithm
- OS - Fragmentation
- OS - Virtual Memory
- OS - Segmentation
- OS - Buddy System
- OS - Allocating Kernel Memory
- OS - Overlays
- Paging and Page Replacement
- OS - Paging
- OS - Demand Paging
- OS - Page Table
- OS - Page Replacement Algorithms
- OS - Optimal Page Replacement Algorithm
- OS - Belady's Anomaly
- OS - Thrashing
- Storage and File Management
- OS - File Systems
- OS - File Attributes
- OS - Structures of Directory
- OS - Linked Index Allocation
- OS - Indexed Allocation
- I/O Systems
- OS - I/O Hardware
- OS - I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Fragmentation
Fragmentation in a memory is a condition where the available memory is divided into small and non-contiguous blocks after allocating and deallocating memory to various processes. In simpler words, fragmentation means there are some gaps in the memory after running multiple processes at random memory locations.
Fragmentation can lead to wastage of memory. Even though there is enough total memory available, it may not be possible to allocate memory to a new process due to fragmentation. The image below shows fragmentation happening in main memory −
 
This is an example of external fragmentation. Here the process P1 is allocated three frames of memory (F1, F2, and F3). P2 is allocated two frames of memory (F5 and F6). Similarly, P3 is allocated F8 frame.
Now, if a new process P4 requires three frames of memory, it cannot be allocated memory even though there are three free frames (F4, F7, and F9) available in the memory. This is because these free frames are not contiguous.
Types of Fragmentation
Fragmentation can be classified into two types −
Internal Fragmentation
Internal fragmentation occurs when a process gets more memory than it actually needed. The operating system allocates memory in fixed size blocks or frames. If a process uses only a part of the allocated memory, then some memory will be wasted. This can happen for large number of processes running in the system, which will lead to wastage of large amount of memory.
The following image shows internal fragmentation taking place in main memory −
 
In the above image, the process P1 is allocated F1, F2, and F3 frames of memory. But it is only using F1 and F2 frames. The F3 frame is wasted because the process does not need it. Similarly, the F6, F7, and F9 frames are also wasted because the processes P2 and P3 are holding them.
Causes of Internal Fragmentation
Internal fragmentation can occur due to the following reasons −
- Fixed Size Memory Allocation − Some operating systems allocate memory in fixed size blocks or frames. If a process requires less memory than the allocated block size, then the remaining memory will be wasted. For example, consider a system that allocates memory to all the processes in blocks of 4 KB. If a process requires 6 KB of memory, then OS should allocate 8 KB of memory (2 blocks) to it. So 2 KB of memory will be wasted.
- Management Overheads − Some systems reserve a part of the allocated memory for purpose like bookkeeping and management. Sometimes this reserved memory may not be used by the process.
- Memory Alignment − Some systems require memory to be aligned to certain boundaries (like 4-byte or 8-byte boundaries). This can lead to wastage of memory and internal fragmentation.
External Fragmentation
External fragmentation refers to gaps generated in memory when multiple processes are loaded and unloaded from memory. To understand this, suppose that a process P1 is allocated with 3 frames of memory (F1, F2, and F3). P2 allocated with 1 frame F4 and P3 allocated with 2 frames F5 and F6. Now, if P2 is terminated, then the frame F4 will be free. But this introduced a gap in memory between the memory allocated to P1 and P3.
The following image shows how external fragmentnation takes place −
 
The gaps generated in memory will add up over time as more processes are loaded and unloaded from memory. This can lead to a situation where even though there is enough total memory available, the system will not be able to allocate memory to a new process. Because the available memory is scattered in small non-contiguous blocks.
Causes of External Fragmentation
External fragmentation can occur due to the following reasons −
- Dynamic Memory Allocation − Some systems use dynamic memory allocation. In such systems, memory is allocated to processes when they request it and deallocated when they are done. This can cause fragmentation as there is no rule to allocate memory.
- Process Termination − When a process is completed, it will be terminated from memory. This will create gaps in memory, if the terminated process was allocated memory in between other processes.
- Variable Size Memory Allocation − Some systems allocate memory to processes in variable sizes. This can lead to fragmentation as there is no rule to allocate memory. For example, if a process requires 5 KB of memory, then OS should allocate 8 KB of memory (2 blocks of 4 KB) to it. So 3 KB of memory will be wasted.
How to Reduce Fragmentation?
There are several techniques to reduce fragmentation in memory. Some of the common techniques are:
- Compaction − Compaction is a technique where the operating system rearranges the memory contents to place all free memory together in one large block. This will remove the gaps in memory and reduce fragmentation.
- Paging − Paging is a memory management technique that uses virtual memory to store the process and then maps it to frames of physical memory. This will eliminate external fragmentation as the process can be stored in non-contiguous frames of memory.
- Segmentation − Segmentation divides memory into variable sized segments based on the logical structure of a program. This will reduce fragmentation as the segments can be allocated in non-contiguous memory locations.
Conclusion
Fragmentation is a common problem in memory management. It lead to wastage of memory even though there is enough total memory available. We have two types of fragmentation: internal fragmentation and external fragmentation. Internal fragmentation occurs when a process gets more memory than it actually needed. External fragmentation occurs when there are gaps in memory due to loading and unloading of processes. The techniques like compaction, paging, and segmentation can be used to reduce fragmentation in memory.