Paging

Paging is an aspect of virtual memory addressing
whereby relatively inactive pages can be temporarily removed from physical memory if necessary.
If these pages have been modified, they must be saved to a temporary storage area on disk,
called a paging file or swap space.
The operation of writing one inactive page, or a cluster of inactive memory pages to disk is called a page out,
and the corresponding operation of reading them in again later when one of the pages is referenced is called a page in.
Paging is effective because programs typically only use a small proportion of their virtual memory pages actively at any one a time.
The set of pages in active use by a process is called its working set.

Some physical memory is reserved for the operating system itself, and for its data structures.
This is called wired memory, because it is not subject to paging.
The rest of physical memory is managed via the paging mechanism, and is called the page pool.
Whenever a virtual memory page that is not in physical memory is referenced,
a page is allocated from the page pool’s free list
and mapped to the required virtual memory address.
Pages are returned to the free list when the memory has been unmapped or freed.
Pages can be reclaimed from the free list if they are referenced again before the physical memory page has been reused.

If the number of pages on the free list falls below a certain threshold,
then the operating system begins to scan for inactive pages to page out.
Pages are regarded as inactive if they have not been referenced for a certain amount of time.
Inactive pages are moved to the end of the free list, but if they have been modified then their contents must first be paged out to disk.
Paging stops as soon as the number of free pages rises back above the threshold.
If the number of pages on the free list continues to fall,
then the operating system begins to scan increasingly rapidly and thus regards pages as inactive more quickly.
The scan rate is a measure of the number of pages scanned per second.
It is a good indicator of memory pressure.

Under extreme memory pressure it is possible for the majority of physical memory to remain very active,
so that the operating system searches in vain for sufficient inactive pages.
In this case, some operating systems will select low-priority processes and deactivate them entirely
to ensure that inactive pages will be able to be found.
Other operating systems will proactively swap such processes out of physical memory entirely.

Under most operating systems, it is possible to tune many aspects of the paging algorithms.
However, such tuning is often complex, and it is normally much more effective to either increase the available physical memory,
or to reduce the demand for virtual memory.