Sub topic 3
Internal and External Fragmentation
This chapter assumes you know how contiguous allocation works: base and limit registers, fixed and variable partitions, and the first-fit, best-fit and worst-fit placement strategies from the previous chapter. You should also be comfortable with powers of two and with reading short Python code.
Two Kinds of Wasted Memory
Fragmentation is memory that exists but cannot be used. It comes in two forms, and GATE questions often ask you to tell them apart.
- Internal fragmentation is memory inside an allocated block that the process does not use. The block belongs to the process, so no one else can use the waste.
- External fragmentation is free memory that is split into many small holes. The total free space may be enough for a request, but no single hole is large enough.
| Property | Internal | External |
|---|---|---|
| Where the waste is | Inside allocated blocks | Between allocated blocks |
| Typical cause | Fixed-size units (fixed partitions, pages) | Variable-size allocation (variable partitions, segments) |
| Visible to allocator as | Allocated memory | Free memory |
| Fixed by | Smaller or better-fitting units | Compaction, or non-contiguous allocation |
Internal Fragmentation
Internal fragmentation appears whenever memory is handed out in units larger than the request.
Fixed partitions. Suppose memory is divided into partitions of 100 KB, 200 KB and 400 KB. A 150 KB process must take the 200 KB partition. The unused 50 KB is internal fragmentation. For a set of processes, the total is the sum of (partition size minus process size) over all allocated partitions.
Variable partitions. Internal fragmentation can occur here too. Suppose a hole is 18,464 bytes and a process needs 18,462. Tracking a 2-byte hole costs more than the hole is worth. So the allocator gives the whole hole to the process, and the 2 bytes become internal waste.
Paging. Physical memory is divided into frames of fixed size, and a process is divided into pages of the same size. The last page of a process is usually only partly filled. A process of 72,766 bytes with 2,048-byte pages needs:
- pages = ceil(72,766 / 2,048) = ceil(35.53) = 36
- allocated = 36 × 2,048 = 73,728 bytes
- internal fragmentation = 73,728 − 72,766 = 962 bytes
The last page is anywhere from almost empty to completely full, so the average waste is about half a page per process. The worst case is one byte less than a page, which is page size − 1 bytes.
The page-size trade-off
A small page reduces internal waste but makes the page table larger. A large page does the reverse. For an average process of size s bytes, page size p bytes and e bytes per page-table entry:
- page-table overhead is about (s / p) × e bytes
- internal fragmentation is about p / 2 bytes
- total overhead is s·e / p + p / 2
Setting the derivative with respect to p to zero gives the minimum at p = √(2·s·e). With s = 2^20 bytes (1 MB) and e = 8 bytes, p = √(2^24) = 4,096 bytes. The overhead is then 2,048 bytes of page table plus 2,048 bytes of fragmentation, 4,096 bytes in total. This is one reason common page sizes are around 4 KB.
External Fragmentation
External fragmentation appears when blocks of different sizes are allocated and freed over time. Free memory ends up as holes scattered between live processes.
Take five free holes, in address order: 100 KB, 500 KB, 200 KB, 300 KB and 600 KB. The total free space is 1,700 KB. Four requests arrive in this order: 212 KB, 417 KB, 112 KB and 426 KB. The program below places them with each strategy.
def place(holes, requests, strategy):
holes = holes[:]
placed = []
for r in requests:
fits = [i for i, h in enumerate(holes) if h >= r]
if not fits:
placed.append((r, None))
continue
if strategy == "first":
i = fits[0]
elif strategy == "best":
i = min(fits, key=lambda j: holes[j])
else:
i = max(fits, key=lambda j: holes[j])
holes[i] -= r
placed.append((r, i))
return placed, holes
holes = [100, 500, 200, 300, 600]
requests = [212, 417, 112, 426]
for s in ("first", "best", "worst"):
placed, left = place(holes, requests, s)
failed = [r for r, i in placed if i is None]
print(s, "failed=%s free=%d largest=%d" % (failed, sum(left), max(left)))The output is:
first failed=[426] free=959 largest=300
best failed=[] free=533 largest=174
worst failed=[426] free=959 largest=300With first fit and worst fit, the 426 KB request fails. The free memory totals 959 KB, which is more than 426 KB, but the largest hole is only 300 KB. This is external fragmentation. Best fit placed every request here, but that depends on the input. Best fit tends to leave many tiny, useless holes, and worst fit tends to break up the large holes that big requests need. Neither strategy is guaranteed to win on every sequence.
A rule of thumb from simulations is the 50-percent rule. With first fit, if N blocks are allocated, about 0.5N more blocks are lost to fragmentation. That is roughly one-third of memory unusable.
Remedies
Compaction
Compaction shifts the allocated blocks toward one end of memory so that all the holes merge into one large hole. In the example above, after first fit, compaction would produce a single 959 KB hole, and the 426 KB request would succeed.
Compaction has costs and conditions:
- It needs processes to be relocatable at run time. That means address binding must be delayed until execution time, so moving a process only requires updating its base register. With compile-time or load-time binding, a moved process would hold wrong addresses.
- Copying memory takes CPU time, and the moved processes cannot run meanwhile.
- A process with pending I/O into a buffer cannot easily be moved, because a device may be writing to the old physical address. The system must delay the move or do the I/O through kernel buffers.
- The cheapest plan is the one that moves the fewest bytes. Finding it is a separate optimization problem.
Non-contiguous allocation
The other cure is to stop requiring contiguity. Paging lets a process occupy any free frames, so a request fails only if the total number of free frames is too small. This removes external fragmentation completely, at the price of some internal fragmentation in the last page. Segmentation also allows non-contiguous layout of a process, but each segment is variable-sized and contiguous. So pure segmentation still suffers external fragmentation. Segmented paging combines the two schemes to avoid it.
Common Errors
- Counting the free memory in a failed variable-partition request as "internal". It is external, because it lies outside any allocated block.
- Using floor instead of ceiling when computing the number of pages. A process of 10,000 bytes with 4,096-byte pages needs 3 pages, not 2.
- Forgetting that a request is placed in a hole that stays in its original position. After an allocation, the leftover remains where the hole was, and first fit scans from the start of memory again.
- Assuming compaction is always possible. It requires execution-time address binding.
- Expecting best fit to minimize fragmentation in every case. It minimizes the leftover of each single allocation, not the overall waste.
Exercise
A system uses 4 KB (4,096-byte) pages. A process is 10,000 bytes long. How many pages does it need, and how many bytes of internal fragmentation result?
Show answer
Pages = ceil(10,000 / 4,096) = ceil(2.44) = 3. Allocated memory = 3 × 4,096 = 12,288 bytes. Internal fragmentation = 12,288 − 10,000 = 2,288 bytes, all in the last page.
Exercise
Free holes in address order are 10 KB, 4 KB, 20 KB, 18 KB, 7 KB and 9 KB. Requests of 12 KB, 10 KB and 9 KB arrive in that order. Which hole size does the 9 KB request receive under first fit, best fit and worst fit?
Show answer
First fit: the 12 KB request takes the 20 KB hole (8 KB left), the 10 KB request takes the 10 KB hole, and the 9 KB request takes the 18 KB hole. Best fit: 12 KB takes the 18 KB hole (6 KB left), 10 KB takes the 10 KB hole, and 9 KB takes the 9 KB hole. Worst fit: 12 KB takes the 20 KB hole (8 KB left), 10 KB takes the 18 KB hole (8 KB left), and 9 KB takes the 10 KB hole, leaving 1 KB.
Exercise
The average process size is 512 KB (2^19 bytes) and each page-table entry is 4 bytes. What page size minimizes the sum of page-table overhead and internal fragmentation, and what is that minimum total?
Show answer
p = sqrt(2·s·e) = sqrt(2 × 2^19 × 4) = sqrt(2^22) = 2^11 = 2,048 bytes. Page-table overhead = s·e / p = 2^19 × 4 / 2^11 = 1,024 bytes. Internal fragmentation = p / 2 = 1,024 bytes. The minimum total is 2,048 bytes.