Unit 3 · Module 1

Memory Management

Memory management is the part of the OS that decides where a process should live in RAM, keeps that placement safe, and moves things around when memory runs tight — all without the program ever noticing. Before a program can run, it has to live somewhere in RAM, and the OS must translate between logical and physical addresses, protect each process, and manage swapping, allocation strategies, fragmentation, compaction, and relocation.

Logical address, physical address and translation

A running program never sees real memory. It works with logical (virtual) addresses starting at 0; the hardware converts each one into a physical address — the actual wire-level location in RAM.

Logical address — Address generated by the CPU while executing the process; also called virtual address. Range 0 … (size − 1).
Physical address — Address seen by the memory unit; the real location loaded into the memory address register.
Address translation — Work done by the MMU: physical = base (relocation register) + logical, after checking logical < limit.
Logical vs physical address space — The set of all logical addresses of a process vs the set of all physical addresses it maps onto.
Memory relocation — Adjusting the addresses of a program so it can run correctly from any physical location. Static = at load time, dynamic = at run time by the MMU.
Protection — The limit register makes sure a process cannot read or write outside its own space; a violation traps to the OS.

Real-world analogy: Hotel room numbers

You book “room 3 on my floor” (logical). Reception adds your floor's starting number (base) to give the real room 1403 (physical). If you ask for room 40 on a floor that only has 20 rooms (limit), security stops you — that is protection.
Experiment: Logical → Physical address visualizer
Step 0 / 4

Live address-translation hardware view

Follow this address as it travels from the CPU through the MMU into RAM.

Valid address
UNIT 01

CPU

The running process requests an address in its own logical address space.

LA 346
UNIT 02

MMU

Checks the limit, then applies the relocation register.

14000 + 346 = 14346
UNIT 03

RAM

The physical cell is accessed only after a successful MMU check.

PA 14346

CPU logical address space

0 … 999

The marker represents logical address 346.

RAM physical memory

base 14000 … 14999

The matching RAM cell is 14346.

CPU

generates address

Logical address = 346

inside the process address space (0 … limit-1)

MMU (Memory Management Unit)

346 < 1000 ✓ then 14000 + 346

Physical address = 14346

base 14000 + offset 346

RAM

Cell 14346 is read / written

  1. Step 1 · CPU generates the address

    The running program refers to a location inside its own address space. This number is the logical (virtual) address.

  2. Step 2 · MMU checks the limit register

    Protection check: the logical address must be smaller than the limit. Otherwise the process is trying to touch memory it does not own.

  3. Step 3 · MMU adds the relocation (base) register

    Physical address = base + logical address. This is dynamic relocation — the program does not need to know where it was actually loaded.

  4. Step 4 · RAM is accessed

    The memory controller reads/writes the real cell at the physical address and returns the data to the CPU.

Swapping

Swapping is the mechanism the OS uses when there is simply not enough RAM for every process that wants to run. It temporarily moves an idle or low-priority process out of RAM and onto the backing store (swap space on disk), freeing memory for other processes. Later, when the original process needs to run again, the OS brings it back into RAM. Swap-out frees memory; swap-in restores the process so it can continue from where it stopped (its PCB keeps the saved context).

Real-world analogy: Desk and drawer

Your desk (RAM) holds only a few files. When you need a new file, you put a file you are not using into the drawer (disk) and take the new one out. Nothing is lost — but walking to the drawer takes time.
Experiment: Swapping simulator (RAM ⇅ swap space)

RAM in use

600/ 800 KB

Available space

200KB free

Backing store

2processes swapped out

Live swapping workspace

Select a process to transfer it between fast RAM and slower swap space.

75% RAM occupied

RAM · main memory

Fast working area used by processes that are ready to execute.

800 KB
P1
P2
P3
FREE
0 KB600 KB occupied800 KB

Medium-term scheduler

Disk · swap space

Slower backing store for temporarily suspended processes.

BACKING STORE
Swap queue2 waiting
P4 · 300 KBP5 · 120 KB

Why swapping? RAM is small and expensive. By temporarily moving a blocked or low-priority process to the swap area on disk, the medium-term scheduler frees space so other processes can be loaded — raising the degree of multiprogramming. The cost is swap time: disk transfers are thousands of times slower than RAM.

Memory allocation: contiguous vs non-contiguous

Contiguous allocation

Each process occupies a single unbroken run of memory. Simple and fast (only base + limit needed), but suffers from internal fragmentation (fixed partitioning) or external fragmentation (dynamic partitioning).

Non-contiguous allocation

A process is split into pieces that can sit anywhere in RAM — fixed-size pages (paging) or variable-size segments(segmentation). Removes external fragmentation, needs a page/segment table.

Contiguous allocation

one continuous run

P1 must find one hole large enough for all four of its memory blocks.

OS
P1
P1
P1
P1
Free
P2
P2
P1Base = frame 1 · Limit = 4 blocks → base + offset locates every P1 address.

Key idea: translation is simple, but P1 cannot be placed if only scattered free blocks are available.

Non-contiguous allocation

pages map to frames

P1’s pieces can occupy any available frames; a page table records their locations.

OS
P1-0
P2-0
P1-1
Free
P2-1
P1-2
P3
P1 page table
page 0 → frame 1page 1 → frame 3page 2 → frame 6

Key idea: scattered free frames can still be used, avoiding external fragmentation at the cost of table lookups.

Fixed partitioning divides memory into a set number of partitions at boot time; dynamic partitioning creates a partition exactly as big as the process when it arrives. Try both in the allocation lab.

Experiment: Fragmentation lab & memory compaction

Click a coloured block to deallocate that process. Free total: 440 KB · largest hole: 200 KB · holes: 4

Internal fragmentation

Memory wasted inside an allocated partition, because the partition is bigger than the process. A 200 KB partition holding a 150 KB job wastes 50 KB that nobody else can use. Typical of fixed partitioning and paging (last page).

External fragmentation

Enough total free memory exists, but it is split into scattered holes, so no single hole is large enough. Typical of dynamic partitioning. Cured by compaction or by non-contiguous allocation (paging).

Relocation and protection in hardware
         limit            base
           |                 |
CPU --> [ logical ] --yes--> ( + ) --> physical --> RAM
           |  <  limit ?
           no
           |
        trap: addressing error / protection violation

Only the operating system, running in kernel mode, may load the base and limit registers. That single rule is what stops one process from corrupting another or the kernel itself.

Practice problem

A process is loaded at base 5000 with limit 1200. The CPU generates logical addresses 340 and 1500. Find the physical addresses.

Quick quiz

1. Who converts a logical address into a physical address?

2. 50 KB is free in total but split into holes of 20 KB and 30 KB. A 40 KB job fails. This is:

3. Swapping is mainly performed by which scheduler?

4. Compaction is only possible when...

Next: allocate real jobs with First / Best / Worst Fit.

Unit 3
Memory allocation algorithms