4.2.24 · D1Operating Systems

Foundations — Fragmentation — internal vs external, compaction

1,736 words8 min readBack to topic

Before you can read the parent note, you must own every symbol it throws at you. This page builds each one from absolute zero — plain words, then a picture, then why the topic cannot live without it.


0. The stage: what "memory" actually is

Figure — Fragmentation — internal vs external, compaction

Look at the figure: memory is one straight line of numbered boxes. Everything in this topic is about cutting this line into pieces and handing pieces out. The topic needs this picture because both kinds of fragmentation are just "which boxes are wasted" — and you can only point at a wasted box if you can see the ruler.


1. The symbols the parent uses

The parent note fires off , , , , , and a "ratio" formula. Here is each one, in the order that lets the next one make sense.

— the size a process needs

Why the topic needs it: fragmentation is always given minus needed, and is the "needed".

— the block the OS hands out in

Figure — Fragmentation — internal vs external, compaction

In the figure, the ruler is pre-scored into equal blocks of length . A program needing bytes must take whole blocks only — it cannot stop half-way through a block. That "whole blocks only" rule is the entire cause of internal fragmentation, so must exist before we can even name that waste.

— how many blocks that forces

To turn " bytes" into " blocks" we need one more symbol.

— the ceiling (round UP)

Figure — Fragmentation — internal vs external, compaction

The figure shows , : two blocks reach (not enough), so the ceiling jumps us to a third block. The red tail is the boxes in the third block that nobody uses — the internal waste made visible.

— the remainder / leftover

Why the topic needs it: the wasted tail in the last block is — the empty part of the final block. Here , matching the parent's answer. The average-case claim ("last block half full → waste ") is just the statement that this leftover is equally likely to be anywhere in , so the empty tail averages .


2. The "holes" symbols for external fragmentation

External fragmentation needs no ceiling — it needs the idea of holes and a ratio.

Figure — Fragmentation — internal vs external, compaction

The figure shows holes of K, K, K. The total free is their sum K, but the largest single hole is only K — so a K request fails even though enough total memory exists. That gap between "total free" and "largest usable" is external fragmentation.


3. The one prerequisite behind compaction

Why the topic needs it: compaction slides all used blocks to one end to merge holes into one big block. You can only slide a program if its addresses can be re-bound — hence compaction requires run-time binding. Without it, programs are "nailed down" and cannot be shuffled. See also Virtual Memory and Segmentation for where run-time binding shows up.


4. How it all feeds the topic

Byte-addressed memory line

Address

Block size B

Requested size S

Ceiling of S over B = N blocks

Internal fragmentation N times B minus S

Mod leftover S mod B

Holes free between used

External fragmentation ratio

Run-time address binding

Relocatable programs

Compaction merges holes

Fragmentation topic 4.2.24

Read top-down: the memory line and block size give you and thus internal waste; the memory line plus scattered holes give you the external ratio; run-time binding unlocks compaction. All three streams pour into the parent topic the Fragmentation note.


Equipment checklist

Test yourself — cover the right side, say the answer aloud.

What is a byte and what is physical memory?
A byte is one addressed box holding a small number; physical memory is a long numbered row of these boxes starting at address 0.
What does mean in the fragmentation formulas?
The number of bytes a process actually needs (requests).
What does mean, and why can't the OS give a fraction of it?
The fixed block size the OS allocates in; hardware/schemes only deal in whole blocks, so no fractions.
What does do and why is it (not ordinary rounding) the right tool for ?
Rounds up to the next whole number; even one leftover byte forces a whole extra block, so you must never round down.
Compute for , .
blocks.
What does give, and what is ?
The leftover after removing whole blocks; .
What is a "hole" in memory?
A run of contiguous free boxes sitting between used regions.
State the external fragmentation ratio and its value for holes 40K,30K,50K.
.
Why does compaction require run-time address binding?
Because sliding a program to a new location changes its addresses; only relocatable (run-time bound) code survives the move.