WikiGalaxy

Personalize

Fragmentation and Compaction in Operating Systems

Fragmentation:

  • Fragmentation occurs when memory is allocated and deallocated in a way that leaves small, unusable gaps.
  • Two types: Internal Fragmentation (within fixed-size blocks) and External Fragmentation (between variable-sized blocks).
  • Internal fragmentation happens when memory blocks are larger than necessary.
  • External fragmentation is caused by scattered free memory blocks.
  • Leads to inefficient memory usage and can impact system performance.

Compaction:

  • Compaction is a technique used to eliminate fragmentation by reorganizing memory contents.
  • It involves moving programs and data to create contiguous free space.
  • Helps in reducing external fragmentation significantly.
  • Requires additional CPU time and resources for rearranging memory.
  • Not always applicable in real-time systems due to time constraints.

Example: Internal Fragmentation

  • Occurs when a process requires less memory than allocated block size.
  • Example: Allocating 10KB block for a 7KB process, leaving 3KB unused.
  • Common in systems using fixed-size memory blocks.
  • Leads to wasted memory within allocated blocks.
  • Can be minimized by using variable-sized blocks.

    // Internal Fragmentation Example
    int blockSize = 10; // 10KB block
    int processSize = 7; // 7KB process
    int internalFragmentation = blockSize - processSize; // 3KB unused
    System.out.println("Internal Fragmentation: " + internalFragmentation + "KB");
    
  • Shows how internal fragmentation results in unused memory.
  • Highlights the importance of efficient memory allocation strategies.
  • Encourages consideration of dynamic memory allocation techniques.

Example: External Fragmentation

  • Happens when free memory is split into small blocks scattered across.
  • Example: Multiple processes allocated and deallocated, leaving fragmented free space.
  • Free space is not contiguous, making it hard to accommodate large processes.
  • Requires memory compaction or paging to resolve.
  • Can lead to system performance degradation.

    // External Fragmentation Example
    int[] memoryBlocks = {5, 10, 15}; // Different sized blocks
    int processSize = 12; // Process requiring 12KB
    // Unable to fit process due to non-contiguous free space
    System.out.println("External Fragmentation prevents allocation.");
    
  • Demonstrates challenges in allocating large processes in fragmented memory.
  • Emphasizes the need for memory management techniques like compaction.
  • Highlights the impact of external fragmentation on system efficiency.

Example: Memory Compaction

  • Compaction is used to consolidate free memory into contiguous space.
  • Involves moving processes to eliminate fragmentation.
  • Example: Rearranging memory blocks to create a single large free block.
  • Can be resource-intensive and not suitable for real-time systems.
  • Improves memory allocation efficiency by reducing fragmentation.

    // Memory Compaction Example
    int[] memoryBlocks = {5, 0, 10, 0, 15}; // Fragmented memory
    // Compaction process to consolidate free space
    int compactedMemory = 30; // Consolidated free space
    System.out.println("Compacted Memory: " + compactedMemory + "KB");
    
  • Illustrates how compaction can resolve fragmentation issues.
  • Highlights the benefits of creating contiguous free space.
  • Discusses the trade-offs involved in using compaction techniques.

Example: Paging and Segmentation

  • Paging and segmentation are techniques to manage memory efficiently.
  • Paging divides memory into fixed-size pages to minimize fragmentation.
  • Segmentation divides memory into segments based on logical units.
  • Both techniques help in reducing fragmentation and improving memory utilization.
  • Example: Using paging to allocate memory for processes.

    // Paging Example
    int pageSize = 4; // Page size in KB
    int processSize = 10; // Process size in KB
    int pagesRequired = (int)Math.ceil((double)processSize / pageSize);
    System.out.println("Pages Required: " + pagesRequired);
    
  • Demonstrates how paging reduces fragmentation by using uniform page sizes.
  • Highlights the flexibility of paging in memory allocation.
  • Explains the role of segmentation in logical memory organization.

Example: Buddy System

  • The Buddy System is a memory allocation technique to minimize fragmentation.
  • Divides memory into blocks of power-of-two sizes.
  • Combines adjacent free blocks to form larger blocks as needed.
  • Example: Allocating memory using the Buddy System.
  • Reduces both internal and external fragmentation.

    // Buddy System Example
    int totalMemory = 1024; // Total memory in KB
    int blockSize = 64; // Initial block size
    int requiredSize = 128; // Required memory size
    // Allocate memory using Buddy System
    System.out.println("Memory allocated using Buddy System.");
    
  • Illustrates the efficiency of the Buddy System in memory allocation.
  • Explains how the system reduces fragmentation through block merging.
  • Highlights the scalability of the Buddy System for different memory sizes.

Example: Slab Allocation

  • Slab Allocation is used for efficient memory management in kernel space.
  • Divides memory into slabs for specific object types.
  • Reduces fragmentation by reusing memory for similar objects.
  • Example: Allocating memory for kernel objects using Slab Allocation.
  • Enhances memory allocation speed and efficiency.

    // Slab Allocation Example
    int slabSize = 256; // Slab size in bytes
    int objectSize = 64; // Object size in bytes
    int objectsPerSlab = slabSize / objectSize;
    System.out.println("Objects per Slab: " + objectsPerSlab);
    
  • Shows how Slab Allocation optimizes memory usage for kernel objects.
  • Highlights the benefits of reusing memory for similar object types.
  • Discusses the role of Slab Allocation in enhancing system performance.

Example: Garbage Collection

  • Garbage Collection automatically reclaims memory in managed environments.
  • Identifies and frees memory occupied by unreachable objects.
  • Reduces fragmentation by compacting memory during collection.
  • Example: Java's Garbage Collector managing heap memory.
  • Improves memory utilization and system stability.

    // Garbage Collection Example
    class GarbageCollectionExample {
        public static void main(String[] args) {
            // Creating objects
            Object obj1 = new Object();
            Object obj2 = new Object();
            // Nullifying references
            obj1 = null;
            obj2 = null;
            // Requesting garbage collection
            System.gc();
            System.out.println("Garbage collection requested.");
        }
    }
    
  • Illustrates how garbage collection manages memory in Java applications.
  • Explains the role of garbage collection in reducing fragmentation.
  • Highlights the benefits of automated memory management for developers.
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025