- OS - Home
- OS - Overview
- OS - History
- OS - Evolution
- OS - Functions
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Processes in Operating System
- States of a Process
- Process Schedulers
- Process Control Block
- Operations on Processes
- Process Suspension and Process Switching
- Process States and the Machine Cycle
- Inter Process Communication (IPC)
- Context Switching
- Threads
- Types of Threading
- Multi-threading
- System Calls
- Scheduling Algorithms
- Process Scheduling
- Types of 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
- Starvation and Aging
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Solutions For Process Synchronization
- Hardware-Based Solution
- Software-Based Solution
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Mutual Exclusion Using Interrupt Disabling
- Peterson's Algorithm
- Dekker's Algorithm
- Bakery Algorithm
- Semaphores
- Binary Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Monitors
- Sleep and Wake
- Race Condition
- Classical Synchronization Problems
- Dining Philosophers Problem
- Producer Consumer Problem
- Sleeping Barber Problem
- Reader Writer Problem
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Resource Allocation Graph
- Livelock
- Memory Management
- Memory Management
- Logical and Physical Address
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Buffering
- Fragmentation
- Compaction
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Free Space Management
- Locality of Reference
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- I/O Programmed
- I/O Interrupt-Initiated
- Direct Memory Access
- 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 - Compaction
Compaction is a technique used by operating systems to reduce fragmentation in memory. 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. Read this chapter to understand how compaction works, its advantages, and drawbacks.
- What is Compaction?
- How Compaction Works?
- Dynamic Relocation For Compaction
- Advantages of Compaction
- Drawbacks of Compaction
What is Compaction?
Compaction refer to the process of rearranging free memory space to together into a single contiguous block. This is done by moving the allocated memory blocks to one end of the memory and combining all the free memory blocks into a single block. The main aim of compaction is to reduce fragmentation of memory, and allocate memory to large processes more easily.
Compaction is performed when the operating system detects that there is a large amount of fragmentation in memory, which can occur once in a week or month depending on the system usage. Compaction can be a time and resource consuming process, as it requires moving processes in memory. Therefore, it is not performed frequently.
How Compaction Works?
The image below shows an example of how compaction works in main memory −
In the image above,
- Initially, there are three processes P1, P2, and P3 allocated memory contiguously.
- When P2 is completed it's execution, it is removed from memory. This creates a gap in memory between P1 and P3.
- Now, compaction is performed to rearrange the memory. P3 is moved to the end of P1, and all the free memory blocks are combined into a single block.
Dynamic Relocation For Compaction
The compaction can only be performed if the operating system supports dynamic relocation. Dynamic relocation refers to the ability of the operating system to move processes in memory while they are running. This is done by using a relocation register, which holds the base address of the process in memory. When a process is moved to new location, the base address in the relocation register is updated accordingly.
The logical address generated by the CPU is added to the base address in the relocation register to get the physical address in memory. i.e., the formula used is −
$$\mathrm{( \text{Logical Address} = \text{Base Address} + \text{Physical Address} )}$$
Where, Base Address = Value in Relocation Register Physical Address = Address generated by the CPU
If dynamic relocation is not supported, then compaction cannot be performed. In such cases, the operating system may have to use other techniques to reduce fragmentation, such as paging or segmentation.
Advantages of Compaction
The advantages of compaction are −
- Reduces Fragmentation − The main aim of compaction is to reduce fragmentation in memory. This is achieved by rearranging the memory and combining all the free memory blocks into a single block.
- Improves Memory Utilization − When fragmentation is reduced, it improves the overall memory utilization and efficiency of the system.
- Allocation for Large Processes − Compaction makes it easier to allocate memory to large processes. Since all the free memory blocks are combined into a single block, it is easier to find a contiguous block of memory large enough to accommodate the process.
Drawbacks of Compaction
The drawbacks of compaction are −
- CPU Overhead − Compaction requires extra effort and CPU time to move processes in memory. This can lead to increased CPU overhead and reduced system performance.
- Process Interruption − Compaction requires interrupting the running processes to move them in memory.
- Not Always Possible − Compaction can only be performed if the operating system supports dynamic relocation.
Conclusion
Compaction is a technique where free memory blocks are rearranged to form a single contiguous block in memory. This helps to reduce fragmentation of memory and improves efficiency of memory utilization. To perform compaction, the operating system must support dynamic relocation, which allows moving processes in memory while they are running. The main drawbacks of compaction can be noted as increased CPU overhead and Interruption of running processes. But overall, compaction is an effective technique to manage memory in operating systems.