Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 4.2: Contiguous memory allocation, fragmentation and placement algorithms

Placing processes in variable-size holes with First, Best and Worst Fit, the internal and external fragmentation that results, and compaction.

Module 4 1.5 hours Theory CO4
← Before
4.1 Address binding
Logical vs physical
You are here
4.2 Contiguous memory allocation, fragmentation and placement algorithms
Module 4 · Memory and File Management
Next →
4.3 Paging & TLB
Non-contiguous allocation

Learning objectives

By the end of this session you should be able to:

  • Describe fixed and variable partition allocation.
  • Apply First, Best and Worst Fit to a list of holes and requests.
  • Distinguish internal and external fragmentation and identify each in an example.
  • State the 50-percent rule and explain compaction and its requirement.

Lecture notes

1. Contiguous allocation

In contiguous allocation each process occupies a single contiguous block of memory. Memory is divided into two parts — the OS (usually low memory, with the interrupt vector) and user processes. Protection uses the relocation and limit registers of Session 4.1.

Fixed partitions

Memory is split into fixed-size partitions; each holds one process. Simple, but the degree of multiprogramming is bounded by the number of partitions, and space inside a partition is wasted. (IBM OS/360 MFT.)

Variable partitions

The OS keeps a table of free holes. A new process gets a hole large enough; the remainder becomes a smaller hole. When a process ends its block is freed and merged with adjacent holes.

2. Placement algorithms — dynamic storage allocation

Given a request of size n and a list of free holes, which hole should be used?

First Fit

Allocate the first hole that is big enough. Stop searching as soon as one is found — fast.

Best Fit

Allocate the smallest hole that is big enough. Must search the whole list (unless sorted). Leaves the smallest leftover.

Worst Fit

Allocate the largest hole. Must search the whole list. Leaves the largest leftover, hoping it stays useful.

Worked example (Silberschatz) — holes 100, 500, 200, 300, 600 KB; processes 212, 417, 112, 426 KB in order
ProcessFirst FitBest FitWorst Fit
212500 → 288 left300 → 88 left600 → 388 left
417600 → 183 left500 → 83 left500 → 83 left
112288 → 176 left200 → 88 left388 → 276 left
426must wait600 → 174 leftmust wait

Here only Best Fit places all four. In simulations, first fit and best fit both beat worst fit in time and storage use; first fit is usually faster.

Simulator — placement algorithmsSwitch the strategy to compare. Step to see each allocation and the leftover hole.

3. Fragmentation

External fragmentation

Enough total free memory exists, but it is split into non-contiguous holes, none large enough. In the example above, First Fit has 100 + 176 + 200 + 300 + 183 = 959 KB free but cannot place 426 KB.

Internal fragmentation

Memory allocated to a process is slightly larger than requested (blocks come in fixed units); the unused part inside the block is wasted. E.g. a 18 462-byte request in a hole of 18 464 bytes: the 2 bytes are not worth tracking.

The 50-percent rule

Statistical analysis of first fit shows that for N allocated blocks, about 0.5 N blocks are lost to fragmentation — one-third of memory may be unusable.

Solutions to external fragmentation

  • Compaction — shuffle memory contents to place all free memory together in one large block. Possible only with execution-time (dynamic) relocation; expensive (copy all of memory).
  • Non-contiguous allocation — let a process's memory be split into pieces placed anywhere: paging (Session 4.3) and segmentation (Session 4.4). This is the real cure.
OSP1hole 100P2hole 150P3hole 200

450 KB free in three holes; a 300 KB request fails.

Move P2 and P3 down, updating their relocation registers.

OSP1P2P3hole 450

One 450 KB hole — the request fits.

VideoBytes

First, Best and Worst Fit

Video coming soon

First, Best and Worst Fit

Recorded by Dr. Krithika L.B. Slot m4-fits

Fragmentation and compaction

Video coming soon

Fragmentation and compaction

Recorded by Dr. Krithika L.B. Slot m4-fragmentation

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Holes 50, 200, 70, 115, 15 KB; request 60 KB. Which hole does each strategy choose?

First fit: 200. Best fit: 70. Worst fit: 200.

Is the 2-byte leftover in a fixed-size block internal or external fragmentation?

Internal — it is inside an allocated block.

Why is compaction impossible with load-time binding?

Addresses were fixed when the process was loaded; moving it would break every address. Compaction needs execution-time relocation.

Which strategy usually performs worst?

Worst fit, in both speed and storage utilisation.

Key takeaways

  • Variable partitions allocate holes; First Fit (fast), Best Fit (smallest leftover), Worst Fit (largest leftover).
  • External fragmentation = scattered free holes; internal = waste inside allocated blocks.
  • Compaction needs dynamic relocation; paging and segmentation avoid the problem.

Where this leads

The real cure for external fragmentation is to stop requiring contiguity. Paging cuts both memory and processes into fixed-size pieces.

Continue to 4.3 — Paging with a TLB →