6 Memory Management and Virtual Memory
Learn how operating systems allocate and protect memory, translate addresses, and use paging, segmentation, and virtual memory to manage processes and mapped files.
Why matters
RAM is limited and shared by the operating system and running programs. tracks which parts are in use, allocates and reclaims memory, translates program addresses into physical locations, and enforces protection so that one process cannot freely access another process’s memory.
The operating system and memory-management unit (MMU) work together to give each process an . A process generally uses virtual addresses, which the MMU translates into physical addresses in RAM. This abstraction lets programs use memory without knowing where their data resides physically, while also supporting protection and controlled sharing.
Address spaces and allocation
A process’s commonly has regions for program code, global data, dynamically allocated memory (often called the ), and the used for function calls and local variables. The exact layout depends on the operating system and architecture. Threads in one process typically share its , whereas separate processes normally have separate address spaces.
Memory allocation takes place at two levels:
A program requests memory from a runtime allocator, for example by calling
mallocin C. The allocator manages requested blocks and may obtain larger regions from the operating system.The operating system manages virtual-memory regions and physical page frames, supplying, mapping, and reclaiming memory as needed.
Allocators balance speed with efficient memory use. With variable-sized blocks, free space can become fragmented. occurs when free memory is split into pieces too small for a request; is unused space inside an allocated block.
and address translation
divides spaces into fixed-size pages and physical memory into same-sized page frames. A records which physical frame, if any, holds each virtual page, along with information such as access permissions. Page tables are commonly hierarchical, avoiding the need for one large, flat table for every possible .
A is interpreted as a virtual page number and an offset within that page. For example, with 4 KiB pages, address 0x12345 has virtual page number 0x12 and offset 0x345. If the maps page 0x12 to physical frame 0x2A, the resulting is 0x2A345.
The memory-management unit caches recent translations in a . A TLB hit provides a translation quickly. On a miss, the processor consults the page tables and may cache the result. Page-table entries can also enforce permissions, including read, write, or execute access.
avoids in physical-memory allocation because any available frame can hold a page. However, the last page of a region may contain unused space.
divides an into variable-sized logical regions, such as code, data, and . In a simple model, a segment’s base identifies where it begins in physical memory, and its limit specifies its size. A contains a segment identifier and an offset; translation adds the offset to the base, provided the offset is within the segment’s limit.
Segments correspond to the sizes of logical program regions, but allocating variable-sized regions in physical memory can create . instead uses fixed-size units, which makes physical allocation simpler. Some architectures combine and ; many modern systems use a largely flat model and rely primarily on for address translation.
and page faults
allows a process’s to be larger than the portion of it currently resident in RAM. The operating system can leave some pages unmapped in physical memory until they are needed, or back mappings with files.
When a process accesses a valid page that is not currently resident, the processor raises a . The operating system handles it, often by bringing the page into RAM, updating the , and restarting the instruction. If RAM is full, the operating system may reclaim a page, writing modified contents to backing storage when necessary. An invalid access or one that violates permissions instead results in an error or process signal.
is not simply extra RAM on disk: disk is much slower than RAM, and a process’s may include regions that are not backed by swap space. It also provides isolation, flexible mapping, and sharing. For example, two processes can map the same read-only program-file pages while retaining separate private data. File-backed mappings are one way an operating system connects a file to a process’s space.
Accessing a mapped file
Suppose a program maps part of a file into its . It reads bytes using ordinary memory addresses. If the needed file page is not in RAM, the first access can cause a ; the operating system loads the relevant data and establishes the mapping.
Later accesses can use the in-memory page, and the may speed up address translation. The program interacts with the mapped region through virtual addresses rather than directly managing physical disk or RAM locations.
How the mechanisms fit together
allocates and reclaims memory, translates addresses, and protects processes. Each process uses an that is mapped to physical memory through hardware and operating-system structures. divides memory into fixed-size pages and frames, while organizes it into variable-sized logical regions. lets the operating system map only needed pages into RAM, support file-backed regions and sharing, and handle nonresident pages through page faults.