Paging vs. Segmentation
Paging and segmentation compared by address mapping and memory layout.
Paging and segmentation are two ways an operating system can translate a program’s addresses into physical memory. They solve the same broad problem, but they organise memory very differently.
Paging
Paging divides virtual memory into fixed-size pages and physical memory into fixed-size frames. Because every page and frame has the same size, the operating system can place a page into any free frame.
When the CPU generates a virtual address, the address is split into two parts:
- a page number
- an offset within that page
The page number indexes into a page table to find the frame number. The frame number plus the offset gives the physical address. A translation lookaside buffer, or TLB, caches recent translations so the CPU does not need a full page table lookup on every memory access.
The big advantage of paging is that it eliminates external fragmentation. Free memory does not need to be one large contiguous block. Any free frame can be used. Paging also makes virtual memory practical because the operating system can move pages between RAM and disk independently.
Its main cost is internal fragmentation. If the page size is 4 KB and a process only uses 100 bytes of the last page, the rest of that page is wasted. Very large page tables are another cost, which is why modern systems use multi-level page tables, huge pages for some workloads, and TLB optimisations.
Segmentation
Segmentation divides memory according to logical program structure rather than fixed-size blocks. A process might have separate segments for code, heap, stack, and shared libraries. Each segment has a variable size.
A virtual address under segmentation contains:
- a segment number
- an offset within that segment
The segment table stores metadata for each segment, typically a base address and a limit. The hardware checks that the offset stays within the segment limit, then adds the offset to the base to form the physical address.
The advantage is that segmentation matches how programmers think about memory. Different segments can have different permissions and can grow independently. Sharing a code segment between processes is also conceptually straightforward.
The downside is external fragmentation. Because segments have different sizes, free memory becomes chopped into holes over time. The system may have enough total free memory but still fail to place a large segment contiguously. Compaction can reduce this, but moving live segments is expensive.
How they compare in practice
Paging is better for efficient allocation and virtual memory at scale. Segmentation is better at expressing logical boundaries and protection rules. Historically, some architectures combined both: segmentation for logical regions and paging for physical placement.
In modern general-purpose operating systems, paging does most of the real work. On x86-64, for example, segmentation is mostly flattened for normal application code, while paging remains central to protection, isolation, copy-on-write, and demand paging.
The useful distinction is this: paging cares about fixed-size storage units, while segmentation cares about programmer-visible regions. One optimises physical allocation. The other reflects logical structure. Understanding both helps explain why modern virtual memory looks the way it does.