Abstract


  • A swap space is an area on a storage device (usually a Hard Disk or Flash Memory) that is used as part of Virtual Memory. When the physical Main Memory of a computer is fully utilised, the Kernel may transfer some data from the main memory to the swap space to free up physical memory for other tasks. This allows the system to continue running applications and processes even when the main memory is insufficient

Check for swap space usage on Linux

free -h

Paging

Why do we need paging?

You will never need to perform paging if you have enough main memory.

Paging eliminates crashes caused by running out of main memory.

Paging Mechanism

Page Table Entry Indicates Page is in Secondary Storage (takes about clock cycle)

  1. CPU Generates a Page Fault Interrupt: The CPU encounters a page table entry that indicates the requested data resides in secondary storage (not main memory), triggering a page fault interrupt. (takes about clock cycles.)
  2. Interrupt Handler Initiates Page Replacement: The corresponding interrupt handler is invoked and selects a victim memory page in main memory to be replaced. (takes about clock cycles.)
  3. Dirty Page Handling: If the victim page has been modified (is “dirty”), the interrupt handler writes it back to secondary storage before proceeding. (takes about clock cycles.)
  4. Page Fetch from Secondary Storage: The interrupt handler reads the required page from secondary storage and loads it into the vacated space in main memory. (takes about clock cycles.)
  5. Page Table Update: The page table entry is updated to reflect the new physical location of the fetched page in main memory. (takes about clock cycles.)
  6. Resume Execution: The interrupt handler returns control to the instruction that originally caused the page fault, allowing execution to continue seamlessly. (takes about clock cycles.)

Significant performance hit

In the time it takes to handle one page fault, a modern CPU can execute 80 million clock cycles.

Page faults are the SLOWEST possible thing that can happen to a computer (except for human interaction). This is why buying more memory may make your computer faster.

Some system don't page

iOS kills your program if you use too much memory.

OS X 10.9 uses ZSwap, which compresses the program first and then resorts to paging if necessary.

ZRAM


Important

ZRAM focuses on optimising main memory usage by compressing pages directly in main memory. Zram allocates a dedicated area in main memory for its compressed memory storage. You can check the status of your ZRAM using sudo swapon --show.

More memory!

When data is compressed, it typically occupies one-quarter of its original size, freeing the remaining three-quarters for other applications to use.

Speed

Paging is much faster since everything happens inside the main memory.

Attention

CPU has to work harder to compress and decompress the data. This also increases power consumption.

ZSwap


Important

Zswap focuses on improving the efficiency of Swap Space.

Better swapping performance

ZSwap reduces the amount of data written to slower swap space, which can improve performance by reducing slow IO Operation.

References