4.4 PAGE REPLACEMENT ALGORITHMS
When a page fault occurs, the operating system has to choose a page to re-
move from memory to make room for the page that has to be brought in. If the
page to be removed has been modified while in memory, it must be rewritten to
the disk to bring the disk copy up to date. If, however, the page has not been
changed (e.g., it contains program text), the disk copy is already up to date, so no
rewrite is needed. The page to be read in just overwrites the page being evicted.
While it would be possible to pick a random page to evict at each page fault,
system performance is much better if a page that is not heavily used is chosen. If
a heavily used page is removed, it will probably have to be brought back in
quickly, resulting in extra overhead. Much work has been done on the subject of
page replacement algorithms, both theoretical and experimental. Below we will
describe some of the most important algorithms.
It is worth noting that the problem of ‘‘page replacement’’ occurs in other
areas of computer design as well. For example, most computers have one or more
memory caches consisting of recently used 32-byte or 64-byte memory blocks.
When the cache is full, some block has to be chosen for removal. This problem is
precisely the same as page replacement except on a shorter time scale (it has to be
done in a few nanoseconds, not milliseconds as with page replacement). The rea-
son for the shorter time scale is that cache block misses are satisfied from main
memory, which has no seek time and no rotational latency.
A second example is in a Web server. The server can keep a certain number
of heavily used Web pages in its memory cache. However, when the memory
cache is full and a new page is referenced, a decision has to be made which Web
page to evict. The considerations are similar to pages of virtual memory, except
for the fact that the Web pages are never modified in the cache, so there is always
a fresh copy on disk. In a virtual memory system, pages in main memory may be
either clean or dirty.
4.4.1 The Optimal Page Replacement Algorithm
The best possible page replacement algorithm is easy to describe but impossi-
ble to implement. It goes like this. At the moment that a page fault occurs, some
set of pages is in memory. One of these pages will be referenced on the very next
instruction (the page containing that instruction). Other pages may not be refer-
enced until 10, 100, or perhaps 1000 instructions later. Each page can be labeled
with the number of instructions that will be executed before that page is first
The optimal page algorithm simply says that the page with the highest label
should be removed. If one page will not be used for 8 million instructions and
another page will not be used for 6 million instructions, removing the former
pushes the page fault that will fetch it back as far into the future as possible.