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 under high memory pressure (a decent amount of main memory is used, the sensitivity can be adjusted with Swappiness), 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
- Also known as Swapping
- The process of swapping memory page between Main Memory and Swap Space
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)
- 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.)
- 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.)
- 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.)
- 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.)
- 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.)
- 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.
Swappiness
- A value between
0
and100
that controls the tendency of the kernel to move processes out of physical memory and onto the Swap Space
Tool
Check the swappiness value with
cat /proc/sys/vm/swappiness
.Edit
vm.swappiness=10
in/etc/sysctl.conf
to change the swappiness.
ZRAM
- ZRAM is a Kernel Module that compresses unused data in Main Memory and moves it back to the Main Memory. We basically have Swap Space inside the main memory
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
- A feature of Linux Kernel that intercepts memory pages that are about to be swapped out to the Swap Space, compress them, and stores them temporarily in Main Memory instead of immediately writing them to the swap space
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.