Unit 3 · Module 2

Memory Allocation Algorithms

Memory allocation is how the OS assigns portions of RAM to processes and reclaims them when processes finish. It must decide where each process goes and how much space it gets. Explore contiguous allocation in action: fixed vs dynamic partitioning and the classic First Fit, Best Fit and Worst Fit strategies, with an animated scan of every block.

Contiguous vs non-contiguous memory allocation

Memory allocation is the method the OS uses to assign RAM to processes and reclaim it when they finish. The key question is whether a process must occupy one consecutive region or may be split across separate free regions.

Contiguous allocation

One continuous region

Together

A process receives one continuous run of adjacent memory addresses. It is simple and fast to access, but finding a large enough run can cause external fragmentation.

Fixed partitioningDynamic partitioning

Non-contiguous allocation

Scattered memory pieces

Split

A process is divided into smaller pieces that can occupy separate memory locations. This uses scattered free space efficiently, but needs mapping tables and address translation.

PagingSegmentation
Physical memory · 24 unitsP1: 4 · P2: 3 units
0
1
2
3
4
5
6
7
P1
P1
P1
P1
12
13
14
15
P2
P2
P2
19
20
21
22
23
P1 data (4 units)P2 data (3 units)free unit

What to notice: P1 uses 8–11 and P2 uses 16–18. Each process stays together, so each needs one hole large enough for its entire size.Use the buttons to remove either process and watch its units return to the free pool.

Contiguous allocation recap

Fixed partitioning — live example

100K free
200K free
300K free
150K free
P1: 100 KBP2: 200 KBP3: 300 KBP4: 150 KB

Press Place Job A to see how a 120 KB job is allocated under fixed partitioning and where fragmentation appears.

First Fit — Allocate the first hole from the start of the list that is big enough. Fastest search.
Best Fit — Allocate the smallest hole that is big enough. Saves large holes but creates tiny leftovers.
Worst Fit — Allocate the largest hole. The leftover is big enough to be reused — in theory.
Allocation failure — If no single hole is large enough, the job waits — even when the total free memory would be sufficient.

Real-world analogy: Parking a car in a row of bays

First Fit = park in the first bay you see that fits. Best Fit = walk the whole row and squeeze into the tightest bay. Worst Fit = take the roomiest bay so the leftover space can still hold a bike.
Experiment: First / Best / Worst Fit step-by-step simulator

First Fit: Scan from the top and take the first block big enough.

Ready — press Next step
Physical Memory1225 KB total · 5 blocks

B1

50 KB

50 KB

50K free

B2

300 KB

300 KB

300K free

B3

75 KB

75 KB

75K free

B4

700 KB

700 KB

700K free

B5

100 KB

100 KB

100K free

AllocatedFree spaceInternal frag.ScanningChosen
  1. Step 1 · What is happening?

    The OS takes the next job from the queue and scans the free block list using First Fit.

  2. Step 2 · Why this decision?

    Scan from the top and take the first block big enough.

  3. Step 3 · What changes in memory?

    The chosen partition is marked busy; the leftover space inside it is internal fragmentation and cannot be reused.

  4. Step 4 · Resulting state

    Free sizes update, totals for used memory and fragmentation are recalculated, and the next job is considered.

Experiment: Side-by-side comparison of the three algorithms

First Fit

  • J1 (20) → block 1 (50 KB)
  • J2 (200) → block 2 (300 KB)
  • J3 (50) → block 3 (75 KB)
  • J4 (150) → block 4 (700 KB)

Internal fragmentation: 705 KB

External fragmentation: 100 KB

Unallocated jobs: 0

Best Fit

  • J1 (20) → block 1 (50 KB)
  • J2 (200) → block 2 (300 KB)
  • J3 (50) → block 3 (75 KB)
  • J4 (150) → block 4 (700 KB)

Internal fragmentation: 705 KB

External fragmentation: 100 KB

Unallocated jobs: 0

Worst Fit

  • J1 (20) → block 4 (700 KB)
  • J2 (200) → block 2 (300 KB)
  • J3 (50) → block 5 (100 KB)
  • J4 (150) → not allocated

Internal fragmentation: 830 KB

External fragmentation: 125 KB

Unallocated jobs: 1

Same blocks, same jobs — only the choice rule changes. Notice how Worst Fit destroys the biggest block early, while Best Fit leaves many tiny unusable holes.

Practice problem

Memory partitions: 50 KB, 300 KB, 75 KB, 700 KB, 100 KB. Jobs: J1 = 20 KB, J2 = 200 KB, J3 = 50 KB, J4 = 150 KB. Show the First Fit allocation.

Quick quiz

1. Which algorithm always scans the whole free list before deciding?

2. Internal fragmentation happens mainly in…

3. Total free memory is 300 KB but the largest hole is 90 KB. A 120 KB job arrives. What happens?

Next: remove external fragmentation completely with paging.

Unit 3
Paging lab