4.2.24 · HinglishOperating Systems

Fragmentation — internal vs external, compaction

1,769 words8 min readRead in English

4.2.24 · Coding › Operating Systems


WHY does fragmentation even happen?


Internal Fragmentation

HOW to compute it — from first principles

Ek process ko bytes chahiye. Memory ko size (ek page/frame/partition) ke fixed blocks mein diya jaata hai. Kitne blocks chahiye:

Total memory di gayi = . Waste woh sab hai jo diya gaya minus woh jo chahiye tha:


External Fragmentation

HOW it builds up

Variable-size allocation mein, bahut saare allocate/free cycles ke baad free list Swiss cheese jaisi lagti hai:

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

100K ki request fail hoti hai even though free hai.


Figure — Fragmentation — internal vs external, compaction


Compaction — the cure for EXTERNAL fragmentation


Steel-manned mistakes


Quick comparison

Internal External
Waste kahaan hai allocated block ke andar blocks ke beech
Kis wajah se fixed-size allocation variable-size allocation
Scheme paging, fixed partitions segmentation, dynamic partitions
Cure chhote blocks (limited) compaction, paging

Recall Feynman: ek 12-saal-ke bachche ko explain karo

Socho tumhara toy box sirf fixed-size bags mein toys fit karta hai. Agar tumhara toy chhota hai lekin bag bada hai, toh bag mein bachi jagah waste hai — yeh internal waste hai, bag ke andar. Ab socho tumhare paas toys ke beech free shelf gaps hain: bahut saare chhote gaps mile karke kaafi jagah bante hain, lekin koi bhi ek bade toy ke liye itna wide nahi. Yeh external waste hai. Compaction matlab saare toys ko ek taraf push karna taaki gaps mil karke ek badi free space ban jayein — ab bada toy fit ho jaata hai!


Flashcards

What is internal fragmentation?
Ek process ko allocate ki gayi lekin unused memory, wasted kyunki allocation fixed-size blocks mein hoti hai; waste ek allocated block ke andar hoti hai.
What is external fragmentation?
Total free memory ek request ke liye enough hai lekin non-contiguous holes mein split hai, koi bhi individually itna bada nahi.
Formula for internal fragmentation of one allocation (size S, block B)?
.
Average internal waste per process relative to block size B?
About (last block average pe half-full).
External fragmentation ratio formula?
.
What does compaction do and which fragmentation does it cure?
Allocated blocks ko saath slide karta hai taaki free holes ek contiguous block mein merge ho jayein; sirf external fragmentation cure karta hai.
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; yeh external ko kuch internal fragmentation se trade karta hai.
Why are smaller blocks not always better?
Less internal waste lekin bade page tables aur zyada management overhead — ek trade-off hai.
Does compaction fix internal fragmentation?
Nahi — internal waste block ke saath move hoti hai; sirf external gaps merge hote hain.

Connections

  • Paging — fixed frames external fragmentation khatam karte hain, internal cause karte hain.
  • Segmentation — variable segments, external fragmentation ka zyada chance.
  • Dynamic Memory Allocation — first-fit / best-fit / worst-fit external fragmentation affect karte hain.
  • Memory Allocation Strategies
  • Virtual Memory — page faults vs fragmentation.
  • Address Binding — compile/load/run-time binding compaction enable karta hai.

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