Virtual memory in Ventura and Sonoma

Purpose and design

macOS (Ventura, Sonoma) uses a Mach-derived virtual memory system to give each process (Mach task) a large, protected 64-bit address space. This provides strong memory isolation and stability, preventing processes from interfering with each other. Physical RAM is treated as a cache for the virtual address space; the system can transparently page out less-used data to disk (swap) when needed, allowing the OS to run programs requiring more memory than the available physical RAM. Virtual memory also enables efficient memory sharing (e.g., mapping libraries or files into multiple processes) without duplicating data in physical memory.

Internal architecture and operation

Internally, the XNU kernel’s Mach VM subsystem represents memory regions as VM objects (anonymous memory or file-backed objects) while a machine-specific pmap module manages actual hardware mappings like page tables and TLBs.

Each Mach task has a VM map (address space) composed of entries that map either private (anonymous) pages or file-backed pages with certain protections. When an executable (Mach-O binary) is launched, its sections are mapped into the new task’s address space rather than eagerly loaded. For example, shared libraries are mapped via submaps such that the read-only code segment (__TEXT) is shared across processes, whereas each process gets a private copy-on-write instance of the library’s writable __DATA segment. Memory is paged in on demand: if a referenced virtual page is not in RAM, the VM subsystem locates the backing memory object (such as the binary file for code or a swap file for anonymous memory) and faults the page into physical memory, possibly evicting an inactive page to disk to make room.

VM features: compression, COW, and UBC

macOS has features to optimize memory usage. One such feature is compressed memory, introduced in OS X Mavericks: as available RAM fills up, the kernel automatically compresses the least-recently-used pages (typically to about half their original size) and frees the memory for active processes. This in-memory compression is highly efficient—using a fast algorithm (WKdm) so that compressing and decompressing data is quicker than paging to disk—and it greatly reduces reliance on slow disk swap. The VM system also relies heavily on copy-on-write (COW) semantics. When a process forks or when multiple mappings refer to the same pages, those pages are initially shared; a private copy is created only if a write occurs. Mach’s VM implements COW via shadow objects that clone pages on demand, avoiding upfront duplication. Additionally, macOS employs a Unified Buffer Cache (UBC) in its BSD kernel layer to unify file I/O caching with the VM page cache. File-backed memory (e.g., memory-mapped files or regular file reads) uses the same underlying cache of physical pages as the rest of virtual memory, eliminating double-caching. This UBC design allows the file system and VM subsystem to share buffers, reducing redundant copies and disk access; data from files is stored in memory once and can be accessed via file reads or memory mappings equally, improving efficiency.

Sections on compression, COW, and UBC will eventually be created in more depth if they have not already been made.

Reading