Memory Management
How the OS carves physical memory for processes: allocation, protection, sharing.
Level: intermediate
What is it?
Memory management tracks physical memory, allocates it to kernel and user workloads, maps process virtual addresses to physical locations, shares selected pages, and enforces access permissions. Modern systems commonly combine paging with virtual address spaces, page tables, and demand allocation.
Why does it matter?
Applications benefit from each process having a simple, isolated address space even when physical RAM is fragmented and shared. Memory-management policies also affect performance through locality, page faults, overcommit, and swapping.
Mental model
A virtual address is a name, not a physical location. The MMU consults page-table mappings and permissions to translate that name; the OS changes those mappings when pages are allocated, shared, swapped, or protected.
How it works
- Virtual memory separates process-visible addresses from physical frames.
- Page tables map virtual pages to physical frames and record access permissions.
- A page fault transfers control to the OS when a mapping is absent or an access violates permissions.
- Demand paging delays physical allocation or loading until a page is first touched.
- Copy-on-write lets processes initially share pages and copy a page only when a writer modifies it.
Steps
- A process loads or accesses a virtual address.
- The MMU checks the page-table entry and permissions during translation.
- If the mapping is present and allowed, the access proceeds to the physical frame.
- If absent, the kernel decides whether to allocate/load a page or report an invalid access.
Real-world usage
- Diagnosing page faults, memory pressure, and out-of-memory termination.
- Explaining process isolation, memory-mapped files, and shared libraries.
- Estimating the cost of random memory access versus sequential locality.
Common mistakes
- Assuming virtual memory means unlimited RAM; storage-backed pages are much slower than RAM.
- Treating all page faults as bugs; demand paging can cause normal first-touch faults.
- Ignoring permissions while focusing only on address translation.
Interview questions
What is the difference between a virtual page and a physical frame?
A virtual page is part of a process's address space; a physical frame is a fixed-size unit of physical RAM to which that page may be mapped.
What is copy-on-write?
Multiple processes share a page until one tries to modify it; then the OS creates a private copy for that writer when required.