Paging vs. Segmentation: How Operating Systems Manage Memory
Every running process needs memory, and the operating system has to decide how to hand it out. Two classical strategies exist: segmentation, which divides memory along the logical structure of a program, and paging, which divides it into fixed-size blocks. Modern Linux, Windows, and macOS all use paging in practice. This article explains both approaches, why paging won, and where you actually notice the difference as a developer.
Segmentation: Memory That Mirrors Your Program
In a segmented memory model, a process's address space is split into variable-sized segments that correspond to program structure: a code segment, a data segment, a stack segment, and so on. Each segment is described by a base address (where it starts in physical memory) and a limit (its size), and a segment selector identifies the segment being accessed.
The x86 architecture was designed around this idea — registers like CS, DS, and SS point to code, data, and stack segments, and the CPU checks every access against the segment's limit. That check is the original hardware-level memory protection mechanism: one process could not reach outside its segments because the CPU would raise a fault.
The fundamental weakness is external fragmentation. Because segments are variable-sized, allocating and freeing them over time leaves small holes between segments that are individually too small to be used, even when the total free memory is large. Fixing it requires compaction — moving segments around in physical memory — and every pointer into a moved segment breaks unless the OS updates it.
Paging: Fixed-Size Blocks and a Page Table
Paging ignores program structure entirely. Virtual memory is cut into fixed-size blocks called pages — 4 KB on most Linux and x86-64 systems, with 2 MB "huge pages" available on servers — and physical memory into equally sized frames. A per-process page table maps every virtual page to a physical frame.
This eliminates external fragmentation by construction: any page fits into any free frame, so memory never gets riddled with unusable gaps. The costs are:
- Internal fragmentation — a process whose heap is 4 KB plus one byte occupies two full pages, wasting most of the second one.
- Translation overhead — every memory access must go through the page table. CPUs mitigate this with a Translation Lookaside Buffer (TLB), a hardware cache of recent virtual-to-physical translations. A process whose working set exceeds the TLB's capacity spends significant time walking page tables.
- Memory for page tables — a multi-level page table is small per process, but thousands of processes add up; Linux splits the address space so that shared kernel tables are not duplicated.
What x86 Actually Does Today
On modern 64-bit systems, addresses are translated through multiple levels of page tables, and segments still exist in the architecture but are configured "flat" — base 0, no limit — so they effectively do nothing. Linux uses the FS segment register for thread-local storage, which is about the only place segments matter in practice. The segment-based scheme above lives on mainly in textbooks, 80386 era tutorials, and languages like early C runtimes that referenced segment registers directly.
Why Paging Won
- No external fragmentation, so memory utilization stays high without expensive compaction.
- Demand paging and swapping become possible: individual pages can be swapped out to disk or mapped lazily, because a page is a fixed, pageable unit. A segment is a whole program component — you cannot easily page out "half a stack segment".
- Copy-on-write —
fork()on Linux shares parent and child memory by mapping the same frames read-only and copying only on write. That is why forking is cheap. Segmentation has no equivalent mechanism. - Sharing — shared libraries map the same physical frames into many processes' page tables. The OS shares memory in page-granularity units, not program-shaped segments.
Where You Notice This as a Developer
/proc/<pid>/smapsshows every mapped region of a process, including per-page reference counts;smemreports proportional memory accounting across shared mappings.- The JVM maps its heap with
mmap. The difference betweenRSSandVSZinpsoutput is paging and lazy allocation in action — virtual memory is reserved up front, but physical frames are only touched when pages are actually accessed. - Databases such as PostgreSQL and MySQL document huge pages because a big buffer pool means a working set of many 4 KB pages, and the TLB can only hold so many translations. Fewer, larger pages mean fewer TLB misses — a direct consequence of how paging works.
free -mshowing "buffers/cache" is the page cache: files cached as pages, reclaimed on demand.
Comparison Table
| Feature | Paging | Segmentation |
|---|---|---|
| Memory division | Fixed-size pages (e.g., 4 KB) | Variable-size segments |
| Fragmentation | Internal (wasted space inside a page) | External (unusable gaps between segments) |
| Addressing | Page number + offset, via page table | Segment number + offset, via segment table |
| Protection granularity | Per page | Per segment (code/data/stack) |
| Supports demand paging / swapping | Yes | Not practically |
| Used by modern OS kernels | Yes (Linux, Windows, macOS) | No (flat model + paging) |
Summary
Paging wins because it trades a little wasted space per page for a memory model that has no external fragmentation, supports demand paging, copy-on-write, and cheap sharing — properties a modern multitasking kernel needs. Segmentation's only real advantages, logical organization and fine-grained protection, are now handled by paging plus per-region access flags. When you are debugging a process's memory usage on Linux, you are almost always debugging paging.