Operating Systems Module 6: Memory Lesson 6.6 15 min

Page replacement

When RAM is full and a page must come in, which page goes out? FIFO, LRU, the unreachable OPT and Clock, and the surprise of Belady’s anomaly.

Memory is full, and a program needs a page that is not there (Lesson 6.5). The OS has to put the page in a frame, so some other page must leave. Which one?

If it picks a page that is needed again a moment later, there will be another fault straight away, and another disk read. If it picks a page that is never needed again, the system loses nothing. The art of page replacement is to guess which pages are not needed soon.

The reference string

To compare policies, we need a fixed sequence of page numbers: the pages a program uses, in order. This is called a reference string. We use this one throughout the lesson, and 3 frames of memory:

1 2 3 4 1 2 5 1 2 3 4 5

A reference is a hit if the page is already in a frame and a fault if it is not. With a fault and a free frame, the page is loaded. With a fault and no free frame, a victim is chosen and replaced. The best policy is the one with the fewest faults.

FIFO: first in, first out

The simplest rule: evict the page that has been in memory the longest. Keep a queue of pages in order of arrival and take the one at the front. It needs almost no bookkeeping. But age is a poor guess for usefulness: a page that has been there a long time may be one the program uses constantly.

Below is FIFO on the string, drawn as a grid: one column per reference, one row per frame. The second pane runs OPT (explained below) on exactly the same string. Step through the left one and predict the victims.

FIFO with 3 frames

  1. Page 1: fault Page 1 is not in memory: a page fault. Frame 1 is free, so the page goes there and nothing is evicted.
    Reference
    1 of 12: page 1
    Result
    page fault
    Evicted
    nothing
    Frames
    1 – –
    Faults so far
    1
    Hits so far
    0
  2. Page 2: fault Page 2 is not in memory: a page fault. Frame 2 is free, so the page goes there and nothing is evicted.
    Reference
    2 of 12: page 2
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 –
    Faults so far
    2
    Hits so far
    0
  3. Page 3: fault Page 3 is not in memory: a page fault. Frame 3 is free, so the page goes there and nothing is evicted.
    Reference
    3 of 12: page 3
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 3
    Faults so far
    3
    Hits so far
    0
  4. Page 4: fault, evict 1 Page 4 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 1, loaded at reference 1.
    Reference
    4 of 12: page 4
    Result
    page fault
    Evicted
    page 1
    Frames
    4 2 3
    Faults so far
    4
    Hits so far
    0
  5. Page 1: fault, evict 2 Page 1 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 2, loaded at reference 2.
    Reference
    5 of 12: page 1
    Result
    page fault
    Evicted
    page 2
    Frames
    4 1 3
    Faults so far
    5
    Hits so far
    0
  6. Page 2: fault, evict 3 Page 2 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 3, loaded at reference 3.
    Reference
    6 of 12: page 2
    Result
    page fault
    Evicted
    page 3
    Frames
    4 1 2
    Faults so far
    6
    Hits so far
    0
  7. Page 5: fault, evict 4 Page 5 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 4, loaded at reference 4.
    Reference
    7 of 12: page 5
    Result
    page fault
    Evicted
    page 4
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    0
  8. Page 1: hit Page 1 is already in frame 2. No fault, and nothing is evicted.
    Reference
    8 of 12: page 1
    Result
    hit
    Evicted
    nothing
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    1
  9. Page 2: hit Page 2 is already in frame 3. No fault, and nothing is evicted.
    Reference
    9 of 12: page 2
    Result
    hit
    Evicted
    nothing
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    2
  10. Page 3: fault, evict 1 Page 3 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 1, loaded at reference 5.
    Reference
    10 of 12: page 3
    Result
    page fault
    Evicted
    page 1
    Frames
    5 3 2
    Faults so far
    8
    Hits so far
    2
  11. Page 4: fault, evict 2 Page 4 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 2, loaded at reference 6.
    Reference
    11 of 12: page 4
    Result
    page fault
    Evicted
    page 2
    Frames
    5 3 4
    Faults so far
    9
    Hits so far
    2
  12. Page 5: hit Page 5 is already in frame 1. No fault, and nothing is evicted.
    Reference
    12 of 12: page 5
    Result
    hit
    Evicted
    nothing
    Frames
    5 3 4
    Faults so far
    9
    Hits so far
    3

FIFO had 9 faults, and OPT only 7. You can see that FIFO evicted pages that were used again soon, and so faulted again soon.

OPT: the best possible, and impossible

What is the best any policy could do? OPT (also called Belady’s optimal algorithm): evict the page whose next use is the furthest in the future, or one that is never used again. It can be shown that no policy has fewer faults. But OPT needs to know the future: the operating system cannot see which pages a program will use next. So OPT cannot be built. It is useful as a yardstick: a measure of how close a real policy gets to the best that could be done. (The simulator can use it because the whole reference string is known in advance.)

In the grid you can see OPT’s reasons in the chips under it: each page in a frame, with the reference number where it is next used, or “never used again”.

LRU: use the recent past to guess the future

Programs tend to reuse what they used recently (this is the same fact that makes the TLB work, Lesson 6.4). So the past can stand in for the future: LRU (least recently used) evicts the page that was used longest ago. It is a good guess and it is the most common model of what real systems try to do.

LRU with 3 frames

  1. Page 1: fault Page 1 is not in memory: a page fault. Frame 1 is free, so the page goes there and nothing is evicted.
    Reference
    1 of 12: page 1
    Result
    page fault
    Evicted
    nothing
    Frames
    1 – –
    Faults so far
    1
    Hits so far
    0
  2. Page 2: fault Page 2 is not in memory: a page fault. Frame 2 is free, so the page goes there and nothing is evicted.
    Reference
    2 of 12: page 2
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 –
    Faults so far
    2
    Hits so far
    0
  3. Page 3: fault Page 3 is not in memory: a page fault. Frame 3 is free, so the page goes there and nothing is evicted.
    Reference
    3 of 12: page 3
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 3
    Faults so far
    3
    Hits so far
    0
  4. Page 4: fault, evict 1 Page 4 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 1, last used at reference 1 (page 2 at reference 2, page 3 at reference 3 are more recent).
    Reference
    4 of 12: page 4
    Result
    page fault
    Evicted
    page 1
    Frames
    4 2 3
    Faults so far
    4
    Hits so far
    0
  5. Page 1: fault, evict 2 Page 1 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 2, last used at reference 2 (page 4 at reference 4, page 3 at reference 3 are more recent).
    Reference
    5 of 12: page 1
    Result
    page fault
    Evicted
    page 2
    Frames
    4 1 3
    Faults so far
    5
    Hits so far
    0
  6. Page 2: fault, evict 3 Page 2 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 3, last used at reference 3 (page 4 at reference 4, page 1 at reference 5 are more recent).
    Reference
    6 of 12: page 2
    Result
    page fault
    Evicted
    page 3
    Frames
    4 1 2
    Faults so far
    6
    Hits so far
    0
  7. Page 5: fault, evict 4 Page 5 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 4, last used at reference 4 (page 1 at reference 5, page 2 at reference 6 are more recent).
    Reference
    7 of 12: page 5
    Result
    page fault
    Evicted
    page 4
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    0
  8. Page 1: hit Page 1 is already in frame 2. No fault, and nothing is evicted. Its last use is now reference 8.
    Reference
    8 of 12: page 1
    Result
    hit
    Evicted
    nothing
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    1
  9. Page 2: hit Page 2 is already in frame 3. No fault, and nothing is evicted. Its last use is now reference 9.
    Reference
    9 of 12: page 2
    Result
    hit
    Evicted
    nothing
    Frames
    5 1 2
    Faults so far
    7
    Hits so far
    2
  10. Page 3: fault, evict 5 Page 3 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 5, last used at reference 7 (page 1 at reference 8, page 2 at reference 9 are more recent).
    Reference
    10 of 12: page 3
    Result
    page fault
    Evicted
    page 5
    Frames
    3 1 2
    Faults so far
    8
    Hits so far
    2
  11. Page 4: fault, evict 1 Page 4 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 1, last used at reference 8 (page 3 at reference 10, page 2 at reference 9 are more recent).
    Reference
    11 of 12: page 4
    Result
    page fault
    Evicted
    page 1
    Frames
    3 4 2
    Faults so far
    9
    Hits so far
    2
  12. Page 5: fault, evict 2 Page 5 is not in memory, and every frame is full. LRU evicts the page that was used longest ago: page 2, last used at reference 9 (page 3 at reference 10, page 4 at reference 11 are more recent).
    Reference
    12 of 12: page 5
    Result
    page fault
    Evicted
    page 2
    Frames
    3 4 5
    Faults so far
    10
    Hits so far
    2

On this string LRU has 10 faults: worse than FIFO’s 9. That is not a mistake in the simulator. This particular string cycles through more pages (1 2 3 4, then 1 2 5, then 3 4 5) than 3 frames can hold, a pattern in which “least recently used” is exactly the page needed next. No practical policy wins on every string; LRU is good on typical programs, not on every sequence. But there is an important difference between LRU and FIFO, as the next section shows.

Belady’s anomaly

It seems obvious that giving a program more memory can only help. For FIFO, it is not true. Here is the same string with FIFO and 4 frames instead of 3, next to the 3-frame run.

FIFO with 4 frames, against FIFO with 3

  1. Page 1: fault Page 1 is not in memory: a page fault. Frame 1 is free, so the page goes there and nothing is evicted.
    Reference
    1 of 12: page 1
    Result
    page fault
    Evicted
    nothing
    Frames
    1 – – –
    Faults so far
    1
    Hits so far
    0
  2. Page 2: fault Page 2 is not in memory: a page fault. Frame 2 is free, so the page goes there and nothing is evicted.
    Reference
    2 of 12: page 2
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 – –
    Faults so far
    2
    Hits so far
    0
  3. Page 3: fault Page 3 is not in memory: a page fault. Frame 3 is free, so the page goes there and nothing is evicted.
    Reference
    3 of 12: page 3
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 3 –
    Faults so far
    3
    Hits so far
    0
  4. Page 4: fault Page 4 is not in memory: a page fault. Frame 4 is free, so the page goes there and nothing is evicted.
    Reference
    4 of 12: page 4
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 3 4
    Faults so far
    4
    Hits so far
    0
  5. Page 1: hit Page 1 is already in frame 1. No fault, and nothing is evicted.
    Reference
    5 of 12: page 1
    Result
    hit
    Evicted
    nothing
    Frames
    1 2 3 4
    Faults so far
    4
    Hits so far
    1
  6. Page 2: hit Page 2 is already in frame 2. No fault, and nothing is evicted.
    Reference
    6 of 12: page 2
    Result
    hit
    Evicted
    nothing
    Frames
    1 2 3 4
    Faults so far
    4
    Hits so far
    2
  7. Page 5: fault, evict 1 Page 5 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 1, loaded at reference 1.
    Reference
    7 of 12: page 5
    Result
    page fault
    Evicted
    page 1
    Frames
    5 2 3 4
    Faults so far
    5
    Hits so far
    2
  8. Page 1: fault, evict 2 Page 1 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 2, loaded at reference 2.
    Reference
    8 of 12: page 1
    Result
    page fault
    Evicted
    page 2
    Frames
    5 1 3 4
    Faults so far
    6
    Hits so far
    2
  9. Page 2: fault, evict 3 Page 2 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 3, loaded at reference 3.
    Reference
    9 of 12: page 2
    Result
    page fault
    Evicted
    page 3
    Frames
    5 1 2 4
    Faults so far
    7
    Hits so far
    2
  10. Page 3: fault, evict 4 Page 3 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 4, loaded at reference 4.
    Reference
    10 of 12: page 3
    Result
    page fault
    Evicted
    page 4
    Frames
    5 1 2 3
    Faults so far
    8
    Hits so far
    2
  11. Page 4: fault, evict 5 Page 4 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 5, loaded at reference 7.
    Reference
    11 of 12: page 4
    Result
    page fault
    Evicted
    page 5
    Frames
    4 1 2 3
    Faults so far
    9
    Hits so far
    2
  12. Page 5: fault, evict 1 Page 5 is not in memory, and every frame is full. FIFO evicts the page that has been in memory longest: page 1, loaded at reference 8.
    Reference
    12 of 12: page 5
    Result
    page fault
    Evicted
    page 1
    Frames
    4 5 2 3
    Faults so far
    10
    Hits so far
    2

With 3 frames FIFO had 9 faults. With 4 frames it has 10. More memory, more faults. This is Belady’s anomaly. It happens because FIFO’s choice depends on arrival order, and an extra frame changes which page is oldest at the moment it matters, so the set of pages in memory with 4 frames is not always a superset of the set with 3.

LRU and OPT can never behave this way: with more frames, the pages they keep are always a superset of what they kept with fewer. (Try them in Try it: give LRU or OPT 4 frames on this string, and their faults go down.) Policies with that property are called stack algorithms, and it is a real reason to prefer LRU over FIFO even though FIFO won with 3 frames here.

Clock: LRU on the cheap

Exact LRU would need the OS to note the time of every memory access, or keep a list reordered on every one. The CPU does it for free only in a simpler way: it sets a use bit (also called the reference bit) in a page’s table entry whenever the page is read or written. The Clock algorithm uses that bit.

Imagine the frames in a circle with a hand pointing to one. On a fault, look at the page under the hand:

  • If its use bit is 1, the page was used recently. Clear the bit to 0 (a second chance) and move on.
  • If its use bit is 0, it has not been used since the hand last passed. Evict it.

A hit only sets the page’s bit to 1, which is one cheap operation. A page that has just been loaded counts as used, so its bit is 1. The sweep ends with a page that was not touched during a whole turn of the hand, a rough but cheap version of “not used recently”.

Clock with 3 frames

  1. Page 1: fault Page 1 is not in memory: a page fault. Frame 1 is free, so the page goes there and nothing is evicted.
    Reference
    1 of 6: page 1
    Result
    page fault
    Evicted
    nothing
    Frames
    1 – –
    Faults so far
    1
    Hits so far
    0
  2. Page 2: fault Page 2 is not in memory: a page fault. Frame 2 is free, so the page goes there and nothing is evicted.
    Reference
    2 of 6: page 2
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 –
    Faults so far
    2
    Hits so far
    0
  3. Page 3: fault Page 3 is not in memory: a page fault. Frame 3 is free, so the page goes there and nothing is evicted.
    Reference
    3 of 6: page 3
    Result
    page fault
    Evicted
    nothing
    Frames
    1 2 3
    Faults so far
    3
    Hits so far
    0
  4. Page 4: fault, evict 1 Page 4 is not in memory, and every frame is full. The clock hand sweeps, giving a second chance to page 1 and page 2 and page 3 by clearing their use bits, and stops at frame 1: page 1 has use bit 0, so it is evicted. Every page has its use bit set, because each was just loaded. The hand sweeps all three, clearing every bit, and evicts the first page it reaches.
    Reference
    4 of 6: page 4
    Result
    page fault
    Evicted
    page 1
    Frames
    4 2 3
    Faults so far
    4
    Hits so far
    0
  5. Page 2: hit Page 2 is already in frame 2. No fault, and nothing is evicted. Its use bit is set to 1. A hit changes nothing in the frames. It only sets page 2's use bit back to 1.
    Reference
    5 of 6: page 2
    Result
    hit
    Evicted
    nothing
    Frames
    4 2 3
    Faults so far
    4
    Hits so far
    1
  6. Page 5: fault, evict 3 Page 5 is not in memory, and every frame is full. The clock hand sweeps, giving a second chance to page 2 by clearing its use bit, and stops at frame 3: page 3 has use bit 0, so it is evicted.
    Reference
    6 of 6: page 5
    Result
    page fault
    Evicted
    page 3
    Frames
    4 2 5
    Faults so far
    5
    Hits so far
    1

Real systems cannot afford exact LRU either, and many use something like Clock: a use bit set by the hardware and a hand (or a pair of lists) that sweeps over the pages. The details differ between systems, but the idea is the one in this simulator.

Try it

Open Try it on any of the simulators in this lesson. Put in your own reference string, such as 1 2 3 1 2 3 1 2 3 (which fits in 3 frames, so after three faults nothing more happens) or 1 2 3 4 1 2 3 4 (which needs 4). Compare the four algorithms, and try to find a string where LRU beats FIFO, and one where it does not. Then try a string with no repeated pages: how many faults can any policy avoid?

Quick check

Quick check

  1. Why can OPT not be built into a real operating system?
  2. What is Belady's anomaly?
  3. Which information does the Clock algorithm use to approximate LRU?
  4. A FIFO queue has pages 7, 3 and 9 in it, with 7 the oldest. Page 7 was used a moment ago. Which page does FIFO evict?

Recap

  • When memory is full, a page fault needs a victim. A good choice avoids faults, a bad one causes another fault at once.
  • A reference string is the sequence of pages a program uses. Policies are compared by their faults on the same string.
  • FIFO evicts the oldest page. LRU evicts the page used longest ago. OPT evicts the page needed furthest ahead: the best possible, but it needs the future, so it is only a yardstick.
  • Belady’s anomaly: with FIFO, more frames can mean more faults (9 with 3 frames, 10 with 4 on the example). LRU and OPT never do this.
  • Clock approximates LRU with a use bit set by the hardware, and a hand that clears bits and evicts the first page with a 0 bit.

Next: what if there is not enough memory for the pages the programs really need? Then the system spends all its time paging.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion