Memory Management
Memory management is how the operating system and processor organise RAM for running programs. It allocates memory, tracks its use and protects one process from another.
What the OS manages
When a program runs, it needs memory for its instructions and data. The OS:
Gives processes memory to use.
Tracks which areas are in use.
Protects processes from unauthorised access to each other’s memory.
Reclaims memory when a process ends.
Uses virtual memory to give each process its own address space.
Virtual and physical addresses
A process uses virtual addresses. These do not directly identify the physical location in RAM.
The processor’s memory management unit (MMU) translates virtual addresses into physical addresses. The OS sets up the mapping and protection rules; the MMU applies them when the program accesses memory.
This means processes can use similar-looking address ranges while their data remains mapped to different physical memory.
Pages and page tables
Virtual memory is commonly divided into fixed-size blocks called pages. RAM is divided into corresponding blocks called frames.
A page table records how a process’s virtual pages map to physical frames, or whether a page is currently elsewhere. The MMU uses this information to translate addresses.
A translation lookaside buffer (TLB) is a small, fast cache of recent address translations. If the translation is in the TLB, the processor can avoid consulting the page table in RAM.
Demand paging and page faults
With demand paging, a page is loaded into RAM when it is needed.
If a process tries to access a page that is not currently in RAM, the processor raises a page fault. The OS then handles it. It may load the page from storage, update the mapping and resume the process.
Page faults are a normal part of virtual memory, but they take much longer to handle than a RAM access. If the system is constantly moving pages between RAM and storage, performance can drop sharply.
Protection and isolation
Memory protection helps stop a process from:
Reading another process’s private data.
Overwriting another process’s memory.
Accessing memory reserved for the kernel without permission.
If a process makes an invalid or unauthorised access, the hardware reports an exception and the OS decides how to handle it.
What happens when a process ends?
The OS releases the memory allocated to the process so it can be used by other programs. It also removes or updates the process’s memory mappings.
Key terms to remember
Memory management · virtual address · physical address · MMU · page · frame · page table · TLB · demand paging · page fault · memory protection
Check your understanding
What does the MMU do?
What is the difference between a page and a frame?
What happens during a page fault?
Why does the processor use a TLB?
How does memory protection help isolate processes?
Answers:
It translates virtual addresses into physical addresses and applies memory-access rules.
A page is a block in virtual memory; a frame is a corresponding block in physical RAM.
The OS handles the missing page, often loading it into RAM, then allows the process to continue if the access is valid.
It caches recent address translations so they can be found faster.
It restricts processes from accessing memory they are not allowed to use.