Memory Pages

πŸ“‹ Quick Reference

AspectDetails
DefinitionFixed-size blocks of virtual memory (typically 4KB)
PurposeEfficient memory management, virtual memory
Page SizeUsually 4KB (can be 2MB, 1GB for large pages)
Virtual MemoryAllows programs to use more memory than physically available
Page FaultWhen accessed page is not in RAM (needs to be loaded from disk)
TLBTranslation Lookaside Buffer (caches page translations)
BenefitsMemory protection, efficient allocation, enables virtual memory

TL;DR: Pages are fixed-size blocks (4KB) that divide virtual memory. They enable virtual memory, memory protection, and efficient memory management through paging.


Clear Definition

A page is a fixed-size block of virtual memory used by the operating system for memory management. The OS divides a process's virtual address space into pages, typically 4KB in size, and maps these pages to physical memory frames or disk storage.

Think of pages as "chunks" of memory that the OS can move between RAM and disk transparently, allowing programs to use more memory than physically available.

πŸ’‘ Key Insight: Pages enable virtual memoryβ€”the illusion that each process has its own large, contiguous memory space. In reality, pages are scattered across physical RAM and disk, managed transparently by the OS.


Core Concepts

Virtual Memory and Paging

Virtual Memory:

  • Each process sees a large, contiguous address space (e.g., 4GB on 32-bit systems)
  • Physical memory is limited and shared among processes
  • OS maps virtual addresses to physical addresses using page tables

How Paging Works:

  1. Process requests memory at virtual address
  2. OS divides address space into pages (e.g., 4KB each)
  3. Page table maps virtual page β†’ physical frame (or disk)
  4. If page in RAM: Access directly
  5. If page on disk: Page fault β†’ load from disk to RAM

Page Table Structure:

Virtual Address: [Page Number | Offset]
     β”‚
     β–Ό
Page Table: Page 0 β†’ Frame 5
           Page 1 β†’ Frame 12
           Page 2 β†’ Disk (not in RAM)
           ...
     β”‚
     β–Ό
Physical Address: [Frame Number | Offset]

Page Faults

What is a Page Fault?

  • Occurs when process accesses a page not currently in physical RAM
  • Types:
    1. Minor: Page in RAM but not in TLB (fast, ~10-100 cycles)
    2. Major: Page on disk, needs to be loaded (slow, ~1-10ms)

Page Fault Handling:

  1. CPU generates page fault exception
  2. OS interrupt handler runs
  3. OS checks if page is valid
  4. If valid: Load page from disk to RAM
  5. Update page table
  6. Resume process execution

Performance Impact:

  • Minor page fault: Negligible (~nanoseconds)
  • Major page fault: Significant (~milliseconds, 100,000x slower than RAM access)

Translation Lookaside Buffer (TLB)

What is TLB?

  • Hardware cache for page table entries
  • Stores recent virtual-to-physical address translations
  • Very fast (L1 cache speed, ~1-3 cycles)

Why Important?

  • Without TLB: Every memory access requires page table lookup (slow)
  • With TLB: Most accesses hit TLB (fast)
  • TLB miss: Need to walk page table (slower)

TLB Characteristics:

  • Small (typically 64-512 entries)
  • Associative cache (set-associative or fully associative)
  • Per-CPU (each CPU core has its own TLB)

Use Cases

Why Pages Exist

  1. Virtual Memory: Allow processes to use more memory than available

    • Example: 10 processes, each thinks it has 4GB, but only 16GB RAM total
    • OS swaps pages to disk when RAM is full
  2. Memory Protection: Isolate processes from each other

    • Each process has its own page table
    • Process A can't access Process B's pages
    • Example: Browser can't access password manager's memory
  3. Efficient Allocation: Allocate memory in fixed-size chunks

    • Easier than variable-size allocation
    • Reduces fragmentation
    • Example: Allocate 4KB page instead of tracking every byte
  4. Memory Sharing: Share pages between processes

    • Copy-on-write for forked processes
    • Shared libraries (same code pages shared)
    • Example: Multiple processes use same libc code pages
  5. Demand Paging: Load pages only when needed

    • Program starts faster (doesn't load everything)
    • Example: Large program, only code being executed is loaded

Advantages & Disadvantages

Advantages

βœ… Virtual Memory: Programs can use more memory than physically available

  • Enables running large programs on limited RAM
  • Example: 8GB RAM system can run programs totaling 32GB virtual memory

βœ… Memory Protection: Processes isolated from each other

  • One process crash doesn't affect others
  • Security: Can't access other process's memory

βœ… Efficient Memory Management: Fixed-size allocation

  • Easier to manage than variable-size
  • Reduces external fragmentation

βœ… Memory Sharing: Share pages between processes

  • Shared libraries save memory
  • Copy-on-write for forked processes

βœ… Demand Paging: Load only what's needed

  • Faster program startup
  • Better memory utilization

Disadvantages

❌ Page Fault Overhead: Major page faults are slow

  • Disk access is 100,000x slower than RAM
  • Can cause noticeable delays (thrashing)

❌ Memory Overhead: Page tables consume memory

  • Each process needs page table
  • Large address spaces = large page tables
  • Example: 4GB address space, 4KB pages = 1M page table entries

❌ TLB Misses: Page table walks are slow

  • TLB miss requires memory access to page table
  • Can be mitigated with larger TLBs, better algorithms

❌ Fragmentation: Internal fragmentation possible

  • Process might not use full page
  • Example: Process needs 1 byte, gets 4KB page

❌ Complexity: More complex than direct physical addressing

  • Page table management
  • Swapping algorithms

Best Practices

  1. Understand Your Application's Memory Access Patterns

    • Sequential access: Better locality, fewer page faults
    • Random access: More page faults, consider larger pages
  2. Use Large Pages When Appropriate

    • 2MB or 1GB pages for large datasets
    • Reduces TLB misses
    • Example: Database buffer pools, scientific computing
  3. Monitor Page Fault Rates

    • High page fault rate = memory pressure
    • Use tools: vmstat, sar, perf
    • Example: >1000 page faults/sec might indicate problem
  4. Optimize Data Structures for Locality

    • Keep related data together
    • Improves cache and page locality
    • Example: Array of structs vs struct of arrays
  5. Consider Memory-Mapped Files

    • OS handles paging automatically
    • Efficient for large files
    • Example: Database systems, large data processing
  6. Avoid Memory Thrashing

    • Too many processes competing for RAM
    • Constant swapping to disk
    • Solution: Reduce memory usage, add RAM, limit concurrent processes

Common Pitfalls

⚠️ Common Mistake: Ignoring page fault rates

  • Problem: High page fault rate = poor performance
  • Solution: Monitor with vmstat, optimize memory usage

⚠️ Common Mistake: Not understanding virtual vs physical memory

  • Problem: Confusion about memory usage
  • Solution: Virtual memory can exceed physical (with swapping)

⚠️ Common Mistake: Assuming all memory access is equal

  • Problem: Page faults are much slower
  • Solution: Optimize for locality, use large pages for big datasets

⚠️ Common Mistake: Not considering TLB size

  • Problem: TLB misses hurt performance
  • Solution: Use larger pages, optimize access patterns

Interview Tips

🎯 Interview Focus: Understanding memory management fundamentals

Common Questions:

  1. "What is virtual memory? How does it work?"

    • Answer: Illusion of large memory through paging. OS maps virtual pages to physical frames or disk. Enables running programs larger than RAM.
  2. "What is a page fault?"

    • Answer: Occurs when accessing page not in RAM. Minor (TLB miss) or major (page on disk). Major faults are slow (disk access).
  3. "How does the OS decide which page to evict?"

    • Answer: Page replacement algorithms (LRU, FIFO, Clock). Goal: Evict pages unlikely to be used soon.
  4. "What is the TLB? Why is it important?"

    • Answer: Cache for page table entries. Without it, every memory access needs page table lookup. TLB makes most accesses fast.
  5. "How would you optimize an application with high page fault rate?"

    • Answer: Reduce memory usage, improve locality, use larger pages, add RAM, optimize data structures.

Red Flags to Avoid:

  • Confusing virtual and physical memory
  • Not understanding page fault implications
  • Ignoring TLB importance

  • Caching (Step 4): TLB is a cache; similar principles apply
  • Database Systems (Step 2): Use pages for buffer pools
  • Operating Systems: Fundamental OS concept

Visual Aids

Virtual Memory Mapping

Process Virtual Address Space (4GB)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Page 0 (0-4KB)      β†’ Frame 5  β”‚
β”‚ Page 1 (4-8KB)      β†’ Frame 12 β”‚
β”‚ Page 2 (8-12KB)     β†’ Disk     β”‚
β”‚ Page 3 (12-16KB)    β†’ Frame 3  β”‚
β”‚ ...                             β”‚
β”‚ Page N (4GB-4KB)    β†’ Frame 8  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
Physical RAM (16GB)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Frame 0  β”‚ Frame 1  β”‚ Frame 2  β”‚
β”‚ Frame 3  β”‚ Frame 4  β”‚ Frame 5  β”‚ ← Page 0
β”‚ ...                             β”‚
β”‚ Frame 12 β”‚ ← Page 1             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
Disk (Swap Space)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Page 2 (swapped out)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Page Fault Flow

CPU Access Virtual Address
         β”‚
         β–Ό
    Page in TLB?
    β”œβ”€ Yes β†’ Access Physical Memory βœ…
    └─ No
         β”‚
         β–Ό
    Page in RAM?
    β”œβ”€ Yes β†’ Update TLB β†’ Access βœ…
    └─ No (Page Fault!)
         β”‚
         β–Ό
    Load Page from Disk
         β”‚
         β–Ό
    Evict Another Page (if RAM full)
         β”‚
         β–Ό
    Load Page to RAM
         β”‚
         β–Ό
    Update Page Table
         β”‚
         β–Ό
    Update TLB
         β”‚
         β–Ό
    Resume Process Execution βœ…

Back to: Step 1 Index | Main Index