Why Linux Uses Virtual Memory and How It Works
This article explains the need for virtual memory in Linux, describes its implementation mechanisms such as swap space and lazy allocation, and details the typical layout of the kernel and user address spaces, including high‑memory handling and zone mapping.
Why Virtual Memory Is Needed
Processes execute code and data from RAM because it is orders of magnitude faster than secondary storage. Physical RAM, however, is limited and must be shared among many concurrent processes. Linux solves this by giving each process a 3 GB private user address space and a shared 1 GB kernel space, implemented through a virtual‑memory system that provides each process with its own page tables. Virtual memory also eliminates external fragmentation and the requirement for physically contiguous memory.
Implementation Mechanism
When physical RAM is exhausted, Linux swaps out pages to a dedicated swap area on disk. During a context switch, pages that are not needed immediately may be moved to swap, which incurs I/O latency; this is why spinlocks (which avoid sleeping) can be more efficient than semaphores in high‑contention paths.
Memory allocation in Linux follows a lazy‑allocation model:
The kernel reserves only a virtual (linear) address range when a program calls malloc or mmap.
Physical pages are allocated on the first access to each page, triggering a page‑fault.
If a page is never touched, no RAM is consumed.
On a page‑fault, the kernel allocates a physical frame, optionally reads the page from swap, and maps it into the process’s address space.
This mechanism improves locality, reduces RAM pressure, and increases overall system throughput.
Typical Layout of the 32‑bit Linux Virtual Address Space
The 4 GB linear address space (0x00000000‑0xFFFFFFFF) is divided as follows:
Kernel space (0xC0000000‑0xFFFFFFFF): 1 GB shared by all processes, accessible only in supervisor mode.
User space (0x00000000‑0xBFFFFFFF): 3 GB private to each process.
Address‑Space Details
Linear address space : the full 4 GB virtual range.
Kernel logical address space : from PAGE_OFFSET (≈3 GB) up to high_memory, providing a 1:1 mapping to low physical RAM.
High‑memory linear address space : addresses above high_memory used for temporary mappings of non‑contiguous high‑memory pages.
Special Kernel Regions
VMALLOC_START‑VMALLOC_END : a contiguous virtual region used by vmalloc() for allocations that are physically non‑contiguous.
PKMAP_BASE‑LAST_PKMAP : region used by kmap() for permanent high‑memory mappings (typically 1024 pages).
FIXADDR_START‑… : region used by kmap_atomic() for short‑lived high‑memory mappings.
User‑Space Memory Usage and Zones
When the kernel allocates pages for a user process, it prefers the ZONE_HIGHMEM zone for pages that are expected to be used infrequently. If ZONE_HIGHMEM is exhausted, allocation falls back to ZONE_NORMAL. Functions such as kmalloc() and vmalloc() internally decide which zone to draw from based on size and alignment requirements.
Physical Memory Layout on x86 (32‑bit)
Physical RAM is divided into three zones:
ZONE_DMA : memory that can be accessed by legacy DMA devices.
ZONE_NORMAL : low memory below 896 MB, permanently mapped into the kernel’s linear address space.
ZONE_HIGHMEM : memory above 896 MB, not permanently mapped; accessed via vmalloc(), kmap(), or kmap_atomic().
Low memory is mapped with a fixed offset using the macros:
#define __pa(x) ((unsigned long)(x) - PAGE_OFFSET) // virtual → physical
#define __va(x) ((void *)((unsigned long)(x) + PAGE_OFFSET)) // physical → virtualHigh memory cannot be converted with these macros; instead the kernel creates temporary page tables on demand.
Mapping Between Logical, Linear, and Physical Addresses
On x86, a logical address (segment:offset) is first converted to a linear (virtual) address using segment descriptors. The linear address is then translated to a physical address via the page‑table hierarchy. Typically the high 12 bits of a 32‑bit address form the segment base, while the low 20 bits serve as the offset within that segment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
