Page Replacement & Thrashing

the dirty bit

Imagine a library where every book has a tiny sticker that flips to red the first moment someone writes a note in the margin. When the librarian wants to return a book to the warehouse, that sticker tells her everything: if it is still grey (untouched) she can just shelve it, because the warehouse copy is identical; if it is red she must first photocopy the marked-up version back to the warehouse so the changes are not lost. The dirty bit is exactly that sticker for a page in memory.

Concretely, each page-table entry carries a single bit, the modify or dirty bit. The hardware sets it to 1 automatically the first time any byte of that page is written. The OS clears it to 0 when it loads or saves the page. Now when that page is chosen as a victim, the OS reads the bit: if it is 0 the page is clean, the copy already in swap space (or in the original file) is still correct, and the page can be discarded with no disk write at all; if it is 1 the page is dirty, so the OS must write the page back to the backing store before reusing the frame. Read-only pages and never-modified code pages stay clean forever, which makes them very cheap to evict.

Why it matters: the dirty bit roughly halves the cost of replacing clean pages — and most code and read-only data is clean. It is why algorithms like enhanced second-chance prefer clean victims, and why a page of program instructions can be thrown out instantly while a page of your edited document cannot. Do not confuse the dirty bit (was this page written?) with the reference bit (was this page recently touched at all, read or write?) — they answer different questions and drive different decisions.

A page of program code has its dirty bit at 0 (code is never written), so evicting it costs nothing — just reuse the frame. A page holding the document you are typing has its dirty bit at 1, so before eviction the OS must write that page out to swap, an extra disk operation.

Clean page: discard for free. Dirty page: must be written back first.

The dirty bit is set by hardware on the first write and is about cost, not recency. A frequently-read but never-written page is dirty-bit 0 (cheap to evict) even if it is heavily used — recency is the reference bit's job, not the dirty bit's.

Also called
modify bitmodified bit修改位元M 位元