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.
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.
Real-world analogy: Hotel room numbers
Live address-translation hardware view
Follow this address as it travels from the CPU through the MMU into RAM.
CPU
The running process requests an address in its own logical address space.
MMU
Checks the limit, then applies the relocation register.
RAM
The physical cell is accessed only after a successful MMU check.
CPU logical address space
0 … 999The marker represents logical address 346.
RAM physical memory
base 14000 … 14999The 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
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.
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.
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.
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 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
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.
RAM · main memory
Fast working area used by processes that are ready to execute.
Medium-term scheduler
Disk · swap space
Slower backing store for temporarily suspended processes.
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.
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
P1 must find one hole large enough for all four of its memory blocks.
Key idea: translation is simple, but P1 cannot be placed if only scattered free blocks are available.
Non-contiguous allocation
P1’s pieces can occupy any available frames; a page table records their locations.
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.
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).
limit base
| |
CPU --> [ logical ] --yes--> ( + ) --> physical --> RAM
| < limit ?
no
|
trap: addressing error / protection violationOnly 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.
A process is loaded at base 5000 with limit 1200. The CPU generates logical addresses 340 and 1500. Find the physical addresses.
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...