Memory Management: Paging and Segmentation For students.
Memory management is how the operating system gives every running program its own private view of memory while sharing one physical RAM chip. The two classic techniques are paging and segmentation. In paging, memory is chopped into equal-sized blocks called pages (for example 4 KB each); in segmentation, a program is divided into logical pieces called segments, such as code, data, and stack.
This matters because without memory management, programs would overwrite each other's data and crash the system, and the CPU could only run programs smaller than RAM. Paging and segmentation give each process protection and the illusion of a large, continuous address space — the foundation that lets your computer run a browser, a game, and a music player at the same time.
Key Concepts
- Logical vs physical address: the address a program uses versus the real location in RAM; the memory management unit (MMU) translates between them.
- Page: a fixed-size block of a process's logical memory. Page frame: a fixed-size block of physical RAM.
- Page table: a per-process table mapping each page number to the frame holding it.
- Offset: a byte's position inside its page; the offset is identical in the page and the frame.
- TLB (translation lookaside buffer): a small, fast cache of recent page-table lookups.
- Internal fragmentation: wasted space inside the last page of a process, because fixed-size pages rarely divide a program exactly.
- Segment: a logical unit like code or stack, with its own length. Segment table: maps each segment number to a base address and a limit.
- External fragmentation: wasted gaps left between variable-sized segments in RAM.
Formulas
- Address split: with a page size of 2^k bytes, an n-bit logical address splits into the top (n − k) bits = page number and the bottom k bits = offset.
- Physical address = frame_number × page_size + offset.
- Segmentation: physical address = segment_base + offset, valid only if offset < segment_limit — otherwise the hardware raises a fault.
- Example: with a 32-bit address and 4 KB pages (k = 12), the page number is bits 31…12 and the offset is bits 11…0.
Worked Example
A CPU uses 32-bit addresses and 4 KB pages. A program reads logical address 0x00003F80.
- 4 KB = 2^12, so the low 12 bits are the offset and the rest is the page number. Offset = 0xF80 = 15×256 + 8×16 = 3840 + 128 = 3968. Page number = 0x00003 = 3.
- The page table says page 3 lives in frame 7.
- Physical address = 7 × 4096 + 3968 = 28672 + 3968 = 32640 = 0x7F80.
The MMU fetches byte 32640 of RAM, and the program never learns where its data really sits.
Common Misconceptions
- "Paging removes all fragmentation." It removes external fragmentation, but internal fragmentation remains — on average, half a page is wasted at the tail of each process.
- "Segments and pages are the same thing." Pages are fixed-size hardware blocks; segments are variable-size logical units chosen by the programmer or compiler.
- "The page table is stored in the CPU." Page tables live in RAM; the CPU only caches recent entries in the TLB.
Real-World Applications
- Every modern OS — Linux, Windows, macOS — uses paging for process isolation.
- Segmentation ideas live on in how programs organize code, data, and stack regions.
- Embedded systems use the MMU to protect firmware from buggy applications.
- Hypervisors use nested paging to isolate virtual machines from each other.
Paging splits memory into equal pages and translates addresses with a page table; segmentation splits programs into logical pieces with base-plus-limit checks. Together — paging for protection and efficiency, segmentation for logical structure — they make multitasking safe, and the same model sits inside every modern processor.
Related blogs
- Processes and Threads: Complete Beginner's Guide Guide.
- CPU Scheduling Algorithms: Complete Beginner's Guide
- Process Synchronization and Semaphores With easy examples.
- Deadlocks: Detection, Prevention and Avoidance For students.
- Virtual Memory and Page Replacement With easy examples.
- File Systems: Complete Beginner's Guide With easy examples.