CS Engineering Gyan

Fragmentation in Operating System

Fragmentation is a memory-management problem in which some portion of available memory becomes difficult to use efficiently because of the way memory has been allocated to processes. The important point is that fragmentation does not simply mean that memory has become "empty." It describes a mismatch between the way free space is arranged and the way memory requests need to be satisfied.

During the lifetime of a system, processes are created, loaded into memory, suspended, terminated, and replaced by other processes. When these operations occur repeatedly, the arrangement of allocated and free memory can become less convenient for future allocations. Depending on the allocation technique, the unused space may occur inside an allocated block or may appear as separate gaps between allocated blocks.

Simple Definition

Fragmentation is the condition in which memory space is not utilized efficiently because some available space is either trapped inside allocated blocks or scattered into separate free regions.

Why Does Fragmentation Occur?

Fragmentation is closely related to the memory allocation strategy used by an operating system. Consider a system that initially has a large free memory area. Several processes may be loaded into that area. Later, some of those processes finish and release their memory. The newly released areas may not be adjacent to one another.

A future process may require a particular amount of memory. Even if the total amount of free memory is greater than the requested amount, the request may still be difficult to satisfy when the allocation method requires one sufficiently large contiguous region.

Important idea:
Total free memory and usable free memory are not always the same thing. The arrangement of free memory determines whether a particular allocation request can be satisfied.

Two Main Forms of Fragmentation

Fragmentation is generally discussed in two forms:


1. Internal Fragmentation

Internal fragmentation occurs when the allocation unit supplied to a process is larger than the amount of memory that the process actually needs. The unused portion remains inside the allocated unit. Since that block has already been assigned, the operating system cannot normally give the unused portion to another process as an independent allocation.

The key characteristic is therefore the location of the wasted space. The unused memory is located within the boundary of an allocated block.

Basic Example:

Suppose memory is allocated in blocks of 8 KB. A process requires only 6 KB. The operating system still allocates one complete 8 KB block.

Internal Waste = Allocated Block − Requested Memory
= 8 KB − 6 KB
= 2 KB

The 2 KB is inside the allocated block and is therefore an example of internal fragmentation.

Illustration of internal fragmentation showing unused space inside an allocated memory block

Figure 1: Internal fragmentation occurs when unused space remains inside an allocated memory block.

Internal Fragmentation in Paging

Paging is based on fixed-size pages and frames. A process is divided into pages, and each page occupies a complete frame. If the last page of a process does not completely fill its frame, the remaining portion of that frame is unused by that process.

For example, if the page size is 4 KB and a process requires 10 KB, the process needs three pages:

The third page occupies a complete 4 KB frame, leaving 2 KB unused in that frame. This unused portion represents internal fragmentation.

Maximum Internal Fragmentation for one process
can approach one allocation unit minus a very small amount, depending on the allocation scheme.

Characteristics of Internal Fragmentation


2. External Fragmentation

External fragmentation has a different structure. Here, memory that is currently free exists outside allocated blocks, but the free space is divided into multiple separated regions.

This becomes important when an allocation request needs one contiguous memory region. The operating system may have enough total free memory, but no individual free region may be large enough for the request.

Example:

Assume that free memory exists in three separate holes:

Total free memory is:

20 + 15 + 10 = 45 KB

Now suppose a process requires 30 KB of contiguous memory. None of the individual holes is 30 KB, so the request cannot be satisfied by simply selecting one of these holes.

The problem is not the total amount of free memory. The problem is its distribution.

Illustration of external fragmentation showing separated free memory holes

Figure 2: External fragmentation occurs when free memory is distributed across separate holes.

Where Does External Fragmentation Occur?

External fragmentation is commonly associated with variable-size contiguous allocation. When processes of different sizes are allocated and later released, holes of different sizes can appear between active processes.

Segmentation is another technique in which variable-sized segments can contribute to external fragmentation when segments need contiguous physical regions.

Characteristics of External Fragmentation

Internal vs External Fragmentation: Core Difference

Point Internal Fragmentation External Fragmentation
Where is space wasted? Inside an allocated block. Between allocated blocks as separate free holes.
Main reason Allocation unit is larger than the actual request. Free memory becomes divided into separate regions.
Nature of allocation Usually associated with fixed-size allocation. Usually associated with variable-size contiguous allocation.
Typical example Unused portion of the last page. Several small holes between processes.
Can total free space be misleading? Yes, because some allocated space may not be useful. Yes, because free space may be scattered.
Common approach Choose an appropriate allocation unit size. Use compaction or an allocation technique that reduces unusable holes.

Visual Understanding of Internal Fragmentation

Allocated Memory Block
Process Data
6 KB
Unused
2 KB

The entire 8 KB block has been allocated to the process. The 2 KB unused portion is located inside the allocated block, so it is internal fragmentation.

Visual Understanding of External Fragmentation

Main Memory
Process A
Free 10 KB
Process B
Free 8 KB
Process C
Free 12 KB

The free regions are separated by allocated processes. Their total size may be large, but a process requiring one contiguous block may not fit into any individual hole.


Fragmentation and Memory Allocation Strategies

Variable-size memory allocation strategies can influence how free holes are created and reused. Three commonly discussed strategies are First Fit, Best Fit, and Worst Fit. These algorithms do not completely eliminate fragmentation; instead, they determine how a requested block is selected from available free holes.

First Fit

First Fit searches the free list from the beginning and selects the first hole that is large enough for the requested process.

Idea: Select the first suitable hole.

Best Fit

Best Fit searches for the smallest available hole that can satisfy the request. The intention is to avoid wasting a large hole for a relatively small process.

Idea: Select the smallest suitable hole.

Worst Fit

Worst Fit chooses the largest available hole for the request. The idea is to leave a reasonably large remainder after allocation.

Idea: Select the largest available hole.
Exam Tip:

First Fit, Best Fit and Worst Fit are allocation strategies. They should not be described as complete solutions that eliminate fragmentation. Their effect depends on the sequence and sizes of memory requests.

Solved Numerical Problem 1: Internal Fragmentation

Problem

A system uses fixed-size memory blocks of 4 KB. A process requires 13 KB. Determine the number of blocks required and the internal fragmentation.

Step 1: Calculate Number of Blocks

Number of Blocks = Ceiling(13 / 4)
= Ceiling(3.25)
= 4 blocks

Step 2: Calculate Allocated Memory

Allocated Memory = 4 × 4 KB
= 16 KB

Step 3: Calculate Internal Fragmentation

Internal Fragmentation = 16 KB − 13 KB = 3 KB
Final Answer:
Blocks required = 4
Allocated memory = 16 KB
Internal fragmentation = 3 KB

Solved Numerical Problem 2: Paging and Internal Fragmentation

Problem

A paging system uses a page size of 1 KB. A process occupies 7.5 KB. Find the number of pages required and the unused memory in the final page.

Step 1: Number of Pages

Pages Required = Ceiling(7.5 / 1) = 8 pages

Step 2: Total Allocated Memory

Total Allocation = 8 × 1 KB = 8 KB

Step 3: Unused Space

Internal Fragmentation = 8 KB − 7.5 KB = 0.5 KB
Answer: 8 pages are required and the final page contains 0.5 KB of unused space.

Solved Numerical Problem 3: External Fragmentation

Problem

A memory contains free holes of 12 KB, 7 KB, 18 KB and 9 KB. A new process requires 20 KB of contiguous memory. Determine whether the request can be satisfied without compaction.

Step 1: Calculate Total Free Memory

Total Free Memory = 12 + 7 + 18 + 9 = 46 KB

Step 2: Check Individual Holes

The largest individual free hole is only 18 KB. The process requires 20 KB of contiguous memory.

Result:
The request cannot be satisfied even though the system has 46 KB total free memory. This is an example of the problem caused by external fragmentation.

Solved Numerical Problem 4: Effect of Compaction

Problem

Suppose free holes of 6 KB, 11 KB, 9 KB and 14 KB are separated by allocated processes. A process requires 30 KB of contiguous memory. Can compaction make the allocation possible?

Before Compaction

Total Free Memory = 6 + 11 + 9 + 14 = 40 KB

However, the largest individual hole is only 14 KB. Therefore, a 30 KB contiguous request cannot currently be placed.

After Compaction

If the operating system can move allocated processes and combine the separated free regions, the free space can be consolidated.

Consolidated Free Space = 40 KB

Since 40 KB is larger than the requested 30 KB, the process can now be placed in the consolidated free region.

Conclusion: Compaction does not create additional memory. It rearranges allocated regions so that scattered free memory becomes a larger contiguous region.

What is Memory Compaction?

Compaction is a technique used to reduce external fragmentation by moving allocated memory blocks so that separate free holes are brought together into one larger free region.

Imagine that memory contains several active processes with small gaps between them. If those processes can be relocated safely, the operating system can move them toward one side of memory. The gaps then merge into a larger continuous free area.

Important:

Compaction is not a solution to internal fragmentation. It is primarily useful for external fragmentation where free space is scattered across memory.

Advantages of Compaction

Limitations of Compaction

Can Fragmentation Be Completely Eliminated?

There is no single memory-management technique that removes every form of memory wastage in every situation. The choice of allocation method determines which type of fragmentation is more likely and how it can be controlled.

Fixed-size allocation can make free-space management simpler, but it may introduce unused space inside allocated units. Variable-size allocation can use requested sizes more directly, but repeated allocation and release can create separated free holes.

Paging is particularly useful for avoiding the need for a process to occupy one contiguous physical memory region. However, paging can still have internal fragmentation, especially in the final page of a process.

Compaction can address scattered free space in systems where relocation is practical, but it introduces its own overhead.

Fragmentation in Paging and Segmentation

Technique Fragmentation Concern Reason
Paging Internal Fragmentation The final page of a process may not completely fill its allocated frame.
Segmentation External Fragmentation Segments are variable in size and may require contiguous physical space.
Fixed Partitioning Internal Fragmentation A partition can be larger than the process assigned to it.
Variable Partitioning External Fragmentation Repeated allocation and release can create separated free holes.

Internal Fragmentation Percentage

In numerical questions, fragmentation may also be expressed as a percentage of the allocated memory.

Internal Fragmentation Percentage = (Unused Space / Allocated Space) × 100

Example

Suppose 20 KB is allocated to a process, but the process uses only 18 KB.

Unused Space = 20 − 18 = 2 KB

Fragmentation Percentage = (2 / 20) × 100 = 10%

External Fragmentation: A Numerical Way to Think About It

External fragmentation does not have one universally useful percentage formula comparable to internal fragmentation because the practical issue is often whether a particular contiguous request can fit into an individual hole.

Therefore, numerical questions involving external fragmentation usually provide a list of free holes and ask whether a requested process can be allocated using a particular strategy.

Remember:
For external fragmentation, always check the individual free holes, not just their total.

Common Mistakes Students Make

  1. Confusing total free memory with a contiguous free block.
    A system may have enough total free memory but still fail a contiguous request.
  2. Calling every unused byte internal fragmentation.
    Internal fragmentation specifically refers to unused space within an allocated unit.
  3. Assuming compaction creates new memory.
    Compaction rearranges memory; it does not increase physical memory capacity.
  4. Assuming Best Fit always produces the least fragmentation.
    The result depends on the sequence of allocations and releases.
  5. Mixing page size with process size.
    In paging problems, the number of pages is obtained by rounding the required number of pages upward.

Advantages and Disadvantages of Studying Fragmentation Techniques

Why Fragmentation Management Matters

  • Improves practical memory utilization.
  • Helps the OS satisfy future allocation requests.
  • Provides a basis for selecting suitable memory-management techniques.
  • Helps explain the design choices behind paging and segmentation.
  • Important for understanding memory-allocation numerical problems.

Trade-offs

  • Smaller fixed units may increase management overhead.
  • Compaction can consume CPU time.
  • Variable-size allocation can create scattered holes.
  • Allocation strategies can require searching free-memory structures.
  • No single strategy is optimal for every workload.

Frequently Asked Questions

1. What is fragmentation in an operating system?

Fragmentation is a condition in which memory is not utilized efficiently because unused space is either present inside allocated blocks or distributed across separate free regions.

2. What is internal fragmentation?

Internal fragmentation is unused memory inside a block that has already been allocated to a process.

3. What is external fragmentation?

External fragmentation occurs when free memory is divided into separate holes between allocated regions.

4. Which fragmentation occurs in paging?

Paging can produce internal fragmentation because the final page of a process may not completely fill its allocated frame.

5. Which fragmentation is associated with segmentation?

Segmentation can suffer from external fragmentation because segments are variable in size and may require contiguous physical memory.

6. Does external fragmentation mean that memory is completely full?

No. External fragmentation means that free memory exists but is scattered into separate regions. The system may still be unable to satisfy a large contiguous allocation request.

7. What is compaction?

Compaction rearranges allocated memory regions so that separated free holes are combined into a larger contiguous free area.

8. Can compaction solve internal fragmentation?

No. Compaction primarily addresses external fragmentation. Internal fragmentation exists inside allocated blocks and therefore cannot normally be removed by simply moving those blocks together.

9. Why does paging reduce external fragmentation?

Paging allows pages of a process to occupy different physical frames, so the process does not need one large contiguous physical memory region.

10. Does Best Fit completely eliminate fragmentation?

No. Best Fit is a placement strategy. Its actual effect on fragmentation depends on the workload and sequence of memory allocations and releases.

Interview Questions on Fragmentation

  1. What is the main difference between internal and external fragmentation?
    Internal fragmentation wastes space inside an allocated block, whereas external fragmentation refers to free space scattered between allocated blocks.
  2. Why does paging generally avoid external fragmentation?
    Because a process's pages can be placed in separate physical frames instead of requiring one contiguous physical region.
  3. Why can paging still have internal fragmentation?
    Because a process's final page may use only part of its allocated frame.
  4. What is the purpose of compaction?
    Compaction combines scattered free holes into a larger contiguous region.
  5. Can external fragmentation exist even when total free memory is large?
    Yes. If the free memory is divided into small holes and a request requires one contiguous block, the request may fail.
  6. What are First Fit, Best Fit and Worst Fit?
    They are memory allocation strategies used to select an appropriate free hole for a process request.
  7. Which allocation strategy always gives the best performance?
    There is no universally best strategy. Performance depends on the memory workload and implementation.

Practice Problems

Try the following questions without opening the solutions first. These problems cover both conceptual and numerical aspects of fragmentation.

Practice Problem 1: Internal Fragmentation

A system uses blocks of 8 KB. A process requires 19 KB. Find the number of blocks required and the internal fragmentation.

Show Answer
Blocks Required = Ceiling(19 / 8) = 3
Allocated Memory = 3 × 8 = 24 KB
Internal Fragmentation = 24 − 19 = 5 KB

Practice Problem 2: External Fragmentation

Free holes are 14 KB, 9 KB, 22 KB and 7 KB. A process requires 25 KB of contiguous memory. Can it be allocated without compaction?

Show Answer
Total free memory = 14 + 9 + 22 + 7 = 52 KB. However, the largest individual hole is only 22 KB. Therefore, the 25 KB contiguous request cannot be satisfied without rearranging memory or using a different allocation mechanism.

Practice Problem 3: Paging

A process requires 15 KB of memory and the page size is 4 KB. Calculate the number of pages and unused space in the final page.

Show Answer
Pages Required = Ceiling(15 / 4) = 4
Allocated Memory = 4 × 4 = 16 KB
Unused Space = 16 − 15 = 1 KB

Practice Problem 4: Conceptual

A system has 60 KB total free memory divided into several holes. A process requires 35 KB contiguous memory. Is total free memory alone enough to conclude that the process can be loaded?

Show Answer
No. The individual sizes and arrangement of the free holes must be checked. If no single hole is at least 35 KB, the process cannot be loaded using contiguous allocation even though the total free memory is 60 KB.

Quick Revision: Fragmentation

Exam-Oriented Short Notes

Internal Fragmentation: Internal fragmentation occurs when a fixed-size allocated memory unit contains unused space. Paging is a common example where the final page may not be completely filled.

External Fragmentation: External fragmentation occurs when free memory is available but divided into separate holes. A large contiguous allocation may fail even though the total free memory is sufficient.

Compaction: Compaction reduces external fragmentation by moving allocated regions so that separated free holes are combined.

Conclusion

Fragmentation is an important concept in operating-system memory management because the usefulness of memory depends not only on how much memory is available but also on how that memory is organized. Internal fragmentation represents unused space within allocated units, while external fragmentation represents separated free regions that may be difficult to use for contiguous allocation.

Understanding this distinction makes several other operating-system topics easier to study. Paging explains how fixed-size pages and frames can avoid the need for contiguous physical allocation, while segmentation demonstrates the advantages and challenges of variable-size logical memory regions. Memory-allocation algorithms such as First Fit, Best Fit and Worst Fit provide different ways of selecting free regions, while compaction can consolidate scattered free space when relocation is possible.

For examinations and interviews, the most important points to remember are the location of wasted space, the reason fragmentation occurs, the type of fragmentation associated with common allocation techniques, and the numerical method used to calculate unused space or determine whether a contiguous request can be satisfied.

One-Line Revision:
Internal = waste inside an allocated block; External = free space scattered outside allocated blocks.
← Previous: Paging and Segmentation Next: Virtual Memory →
Home Visit Our YouTube Channel