Demand Paging

demand Paging Illustration

Demand Paging is a method of virtual memory management where pages are loaded into physical memory only when they are requested by a running program. This approach contrasts with pre-paging systems that attempt to predict which pages will be needed and load them in advance.

How It Works

When a process is started, the operating system creates its page table but does not immediately load any pages into physical memory. Instead, all pages are initially marked as "not in memory." When the CPU tries to access a page that isn't in memory:

  • A page fault is triggered
  • The operating system suspends the process
  • The required page is located in secondary storage (usually the hard disk)
  • The page is loaded into an available frame in physical memory
  • The page table is updated to reflect the new page location
  • The process is resumed from the point of interruption
  • This "load on demand" mechanism ensures that only needed pages occupy valuable physical memory, optimizing resource utilization.

    Advantages

    • Efficient memory usage as only needed pages are loaded
    • Lower initial loading time for programs
    • More processes can run simultaneously with limited physical memory
    • Support for programs larger than physical memory
    • Reduced I/O and storage transfers
    • Better system responsiveness for multiple users

    Disadvantages

    • Performance overhead due to page fault handling
    • Increased hardware complexity (requires MMU)
    • Potential for thrashing when physical memory is over-committed
    • Additional complexity in system design
    • Unpredictable program execution times due to page faults

    Performance Considerations

    The effectiveness of demand paging depends on several factors:
    • Locality of reference - Programs that exhibit high locality perform better
    • Page size - Balancing fragmentation against transfer overhead
    • Page replacement algorithm - Critical for deciding which pages to remove
    • Frame allocation policy - How frames are distributed among processes
    • Working set management - Identifying actively used pages to prevent thrashing