4.2.24Operating Systems

Fragmentation — internal vs external, compaction

1,880 words9 min readdifficulty · medium2 backlinks

WHY does fragmentation even happen?


Internal Fragmentation

HOW to compute it — from first principles

A process needs SS bytes. Memory is handed out in fixed blocks of size BB (a page/frame/partition). The number of blocks needed is:

N=SBN = \left\lceil \frac{S}{B} \right\rceil

Total memory given = NBN \cdot B. The waste is everything given minus everything needed:

Internal=NBS=SBBS\boxed{\text{Internal} = N\cdot B - S = \left\lceil \tfrac{S}{B}\right\rceil B - S}


External Fragmentation

HOW it builds up

With variable-size allocation, after many allocate/free cycles the free list looks like Swiss cheese:

[ Used ][ free 40K ][ Used ][ free 30K ][ Used ][ free 50K ]

A request for 100K fails even though 40+30+50=120K40+30+50 = 120\text{K} is free.


Figure — Fragmentation — internal vs external, compaction


Compaction — the cure for EXTERNAL fragmentation


Steel-manned mistakes


Quick comparison

Internal External
Where waste is inside allocated block between blocks
Caused by fixed-size allocation variable-size allocation
Scheme paging, fixed partitions segmentation, dynamic partitions
Cured by smaller blocks (limited) compaction, paging

Recall Feynman: explain to a 12-year-old

Imagine your toy box only fits toys in fixed-size bags. If your toy is small but the bag is big, the empty space in the bag is wasted — that's internal waste, inside the bag. Now imagine instead you have free shelf gaps between toys: lots of little gaps add up to plenty of space, but none is wide enough for a big toy. That's external waste. Compaction is pushing all the toys to one side so the gaps join into one big free space — now the big toy fits!


Flashcards

What is internal fragmentation?
Memory allocated to a process but unused, wasted because allocation is in fixed-size blocks; the waste lies inside an allocated block.
What is external fragmentation?
Total free memory is enough for a request but it's split into non-contiguous holes, none individually large enough.
Formula for internal fragmentation of one allocation (size S, block B)?
S/BBS\lceil S/B\rceil \cdot B - S.
Average internal waste per process relative to block size B?
About B/2B/2 (last block half-full on average).
External fragmentation ratio formula?
1largest free blocktotal free memory1 - \dfrac{\text{largest free block}}{\text{total free memory}}.
What does compaction do and which fragmentation does it cure?
Slides allocated blocks together so free holes merge into one contiguous block; cures external fragmentation only.
What hardware/binding requirement does compaction need?
Relocatable code with run-time address binding (base/relocation register).
Which scheme eliminates external fragmentation, and what does it trade for?
Paging — fixed-size frames; it trades external for some internal fragmentation.
Why are smaller blocks not always better?
Less internal waste but larger page tables and more management overhead — a trade-off.
Does compaction fix internal fragmentation?
No — internal waste moves with the block; only external gaps are merged.

Connections

Concept Map

choose fixed

choose variable

unused tail causes

scattered holes cause

needs

gives

averages to

reduces

but adds page-table overhead

shifts blocks to fix

trade-off

trade-off

OS allocates in units

Fixed-size units

Variable-size units

Internal fragmentation

External fragmentation

Blocks N = ceil S over B

Internal = N*B - S

Avg waste approx B/2

Smaller blocks

Compaction

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, memory ek limited resource hai aur OS use chunks me deta hai. Do tarah ki fragmentation hoti hai. Internal fragmentation tab hoti hai jab block fixed-size ka ho — jaise paging me frame fixed hota hai. Agar process ko 5000 byte chahiye aur block 2048 byte ka hai, to 3 block dene padenge (6144 byte), aur 1144 byte block ke andar waste ho jaate hain. Ye waste process ke paas hai par use nahi ho raha — isliye "internal".

External fragmentation tab hoti hai jab allocation variable-size ho (segmentation ya dynamic partition). Process aate-jaate rehte hain, aur beech-beech me chhote-chhote free holes ban jaate hain. Total free memory kaafi hoti hai (maan lo 120K) par koi single hole itna bada nahi ki request (100K) fit ho — gaps scattered hote hain. Cinema me 50 khaali seats par 6 logon ka family ek saath nahi baith pa raha — wahi feeling.

Compaction iska ilaaj hai: saare allocated blocks ko ek taraf khisko do, taaki saare free holes mil ke ek bada contiguous block ban jaaye — ab 100K wali request pass ho jaayegi. Par yaad rakho: compaction sirf external fix karta hai, internal nahi (kyunki internal waste block ke andar hota hai, block ke saath hi move ho jaata hai). Compaction mehnga hai (memory copy karni padti, processes ruk jaate), isliye OS often paging use karta hai jo external fragmentation hi nahi banne deta — bas thoda internal cost deta hai. Yahi 80/20 funda hai: "Internal = andar chhupa waste, External = total enough par koi single piece bada nahi."

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections