1. Why Is Page Replacement Needed?
Modern operating systems usually use virtual memory mechanisms to provide each process with an independent virtual address space.
When a program accesses a virtual address, the MMU maps the virtual page to a physical page frame through the page table. Because physical memory capacity is limited, the system cannot always keep all pages required by all processes in RAM.
For example, a server has 16 GB of physical memory and simultaneously runs:
- Database service, using 6 GB.
- Java application, using 5 GB.
- Cache service, using 4 GB.
- Operating system and other services, using 2 GB.
The memory requirements of these workloads already exceed the physical memory capacity.
The operating system must decide:
- Which pages should remain in physical memory?
- Which pages are temporarily unused and can be reclaimed?
- Which pages need to be written to swap space?
- How can a page be restored when the process accesses the swapped-out page again?
- If memory remains insufficient, how can the system avoid a collapse in performance?
These are the core problems that the page replacement mechanism must solve.
1.1 What Problems Does Page Replacement Solve?
Page replacement is the mechanism by which the operating system, when physical memory is insufficient and space needs to be freed for new pages, selects one or more existing pages to reclaim or swap out.
It mainly solves the following problems:
| Problem | Symptom | Role of Page Replacement |
|---|---|---|
| Insufficient physical memory | Not enough free page frames | Reclaim or swap out pages |
| Different page access frequencies | Some pages are accessed frequently, some are not used for a long time | Prefer keeping active pages |
| Memory resource contention | Multiple processes compete for RAM | Coordinate memory among processes |
| File cache growth | File cache occupies a large amount of memory | Reclaim cache pages that can be rebuilt |
| Anonymous memory growth | Anonymous pages such as heap and stack occupy memory | Swap out to Swap when necessary |
| Memory access performance degradation | Frequent page faults and disk I/O | Reduce unnecessary page swapping in and out |
Note: page replacement is not equivalent to simply writing pages to disk.
If a page is a clean file-mapped page, the operating system can usually discard its physical copy directly and read it from the original file later when needed.
If a page is a modified anonymous page, its contents usually need to be saved to Swap first before the corresponding physical page frame can be safely reclaimed.
1.2 Where Page Replacement Fits in Memory Management
Page replacement is part of virtual memory management and is closely related to page faults, page tables, physical memory allocation, and page reclamation.
flowchart TD
A["Application accesses virtual address"] --> B["MMU queries address mapping"]
B --> C{"Is the page resident?"}
C -->|Yes| D["Access physical memory"]
C -->|No| E["Trigger page fault"]
E --> F["Operating system handles page fault"]
F --> G{"Is there a free page frame?"}
G -->|Yes| H["Allocate free page frame"]
G -->|No| I["Start page reclamation"]
I --> J["Select reclaimable page"]
J --> K["Write back page if necessary"]
K --> L["Release physical page frame"]
L --> H
H --> M["Load target page"]
M --> N["Update page table"]
N --> O["Resume execution"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style C fill:#FFF1CC,stroke:#E5C77A,color:#755719
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style F fill:#F1F3F5,stroke:#B9C1CA,color:#475569
style G fill:#FFF1CC,stroke:#E5C77A,color:#755719
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style K fill:#FFF1CC,stroke:#E5C77A,color:#755719
style L fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style M fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style N fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style O fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
2. Fundamentals of Page Replacement
Before understanding page replacement algorithms, one must first grasp concepts such as pages, page frames, page faults, and dirty pages.
2.1 Page and Page Frame
The operating system uses paging mechanisms to manage virtual memory and physical memory.
- Page: a page in the virtual address space.
- Page Frame: a page frame in physical memory.
Assume the system uses 4 KB pages:
| Concept | Meaning | Size |
|---|---|---|
| Virtual page Page 0 | One page in the virtual address space | 4 KB |
| Virtual page Page 1 | One page in the virtual address space | 4 KB |
| Physical page frame Frame 0 | A block of storage in RAM | 4 KB |
| Physical page frame Frame 1 | A block of storage in RAM | 4 KB |
Virtual pages do not need to be mapped contiguously to physical page frames.
For example:
flowchart TD
A["Virtual page Page 0"] --> B["Physical page frame Frame 3"]
C["Virtual page Page 1"] --> D["Physical page frame Frame 1"]
E["Virtual page Page 2"] --> F["Physical page frame Frame 5"]
G["Virtual page Page 3"] --> H["Physical page frame Frame 0"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#FFF1CC,stroke:#E5C77A,color:#755719
style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style G fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
2.2 What Is a Page Fault?
A page fault is an exception triggered when the CPU accesses a virtual address and the page mapping or the access conditions are not satisfied.
Common cases include:
- The page has not yet been allocated physical memory.
- The page has already been swapped out to Swap.
- The data corresponding to a file mapping has not yet been loaded.
- The process accesses a copy-on-write protected page.
- The page permissions do not allow the current operation.
- The virtual address has no legal mapping at all.
A page fault does not necessarily mean the program has hit an error.
If the access is legal, the operating system can complete handling it by allocating a page, reading data from a file, or restoring data from Swap.
2.3 What Is a Dirty Page?
A dirty page is a page that has been modified but whose modifications have not yet been written back to its backing store.
For example:
- A file-mapped page has been modified but has not yet been written back to the file.
- An anonymous page has been modified by a process, and its contents need to be saved via Swap.
Whether a page is clean directly affects the cost of reclaiming it.
| Page type | Modified? | Typical reclaim method |
|---|---|---|
| Clean file page | No | Can be discarded directly and read in again later |
| Dirty file page | Yes | Usually needs to be written back to the file |
| Clean anonymous page | Unmodified, with a valid backing copy already available | Can be reclaimed depending on the specific implementation |
| Dirty anonymous page | Yes | Written to Swap when necessary |
Here, clean and dirty are relative to the page's backing store. In practice, the kernel also needs to consider states such as whether the page is shared, whether it is currently being written back, and whether it is locked.
2.4 What Is the Difference Between Page Replacement and Page Reclaim?
These two concepts are often used interchangeably, but they are not entirely the same.
Page Replacement:
This usually emphasizes choosing an existing page as a victim page to make room for a new page when a new page is needed.
Page Reclaim:
This emphasizes the entire process by which the operating system reclaims usable physical memory, including steps such as scanning pages, determining page state, writing back dirty pages, and freeing pages.
Memory reclaim in modern Linux is far more complex than classic page replacement algorithms; it also involves factors such as the page cache, anonymous pages, working sets, memory control groups, and NUMA.
3. The Basic Workflow of Page Replacement
When a process accesses a legal page that is not in physical memory, the operating system needs to handle a page fault.
If the system has free page frames, it can allocate one directly.
If there are no free page frames, it needs to reclaim other pages.
3.1 The Complete Steps of Page Replacement
- The CPU accesses a virtual address.
- The MMU checks the address mapping.
- It finds that the target page is not in physical memory.
- The CPU triggers a page fault.
- The operating system checks whether the access is legal.
- It checks whether any free page frames are available.
- If there are no free page frames, it selects candidate pages for reclaim.
- If the victim page has modifications that must be preserved, it writes them back first.
- It frees the physical page frame.
- It loads the target page into the free page frame.
- It updates the page table and the relevant memory management state.
- It flushes the TLB if necessary.
- It re-executes the instruction that triggered the page fault.
flowchart TD
A["Page fault occurs"] --> B["Check whether access is valid"]
B --> C{"Is the access valid?"}
C -->|No| D["Report access error"]
C -->|Yes| E["Find a free page frame"]
E --> F{"Is there a free page frame?"}
F -->|Yes| G["Allocate page frame directly"]
F -->|No| H["Scan candidate pages"]
H --> I["Select victim page"]
I --> J{"Does it need writeback?"}
J -->|Yes| K["Write to file or Swap"]
J -->|No| L["Reclaim page"]
K --> L
L --> G
G --> M["Read target page"]
M --> N["Set up page table mapping"]
N --> O["Flush TLB if necessary"]
O --> P["Re-execute the original instruction"]
style A fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#FFF1CC,stroke:#E5C77A,color:#755719
style D fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#FFF1CC,stroke:#E5C77A,color:#755719
style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style J fill:#FFF1CC,stroke:#E5C77A,color:#755719
style K fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style L fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style M fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style N fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style O fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style P fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
3.2 Why Does Page Replacement Have a Cost?
Page replacement is not free.
It can incur the following overheads:
| Operation | Cost Source |
|---|---|
| Scanning candidate pages | CPU time |
| Checking page status | Kernel data structure access |
| Writing back dirty file pages | File system and disk I/O |
| Swapping out anonymous pages | Swap writes |
| Loading the target page | File or Swap reads |
| Updating page tables | Kernel memory management overhead |
| TLB invalidation | Address translation cache invalidation |
| Re-executing the instruction | Exception handling and scheduling overhead |
Storage I/O deserves particular attention.
If every page replacement has to wait for disk reads and writes, the program's response time may increase significantly.
Therefore, a good page replacement strategy should not only reduce the number of page faults but also minimize the cost of page reclaiming and writeback.
4. Evaluation Metrics for Page Replacement Algorithms
When evaluating a page replacement algorithm, you cannot look only at the number of page faults.
You also need to consider memory access patterns, whether pages have been modified, algorithm implementation cost, and overall system throughput.
4.1 Page Fault Rate
The page fault rate indicates the proportion of memory accesses that cause page faults.
For example:
- Total memory accesses: 100,000.
- Page faults: 1,000.
Then:
The lower the page fault rate, the more closely the set of resident pages usually matches the program's access needs.
However, different types of page faults have different costs. The performance impact of a Minor Fault and a Major Fault can differ greatly.
4.2 Number of Page Replacements
The number of page replacements indicates how many times the system reclaims other pages to free up physical page frames.
Frequent replacement usually means:
- Insufficient available memory.
- The working set is too large.
- Severe memory contention among multiple processes.
- Poor locality of page access.
4.3 Number of Page Writebacks
If a page being replaced is dirty, the operating system usually needs to write it back to backing storage.
Too many writebacks can cause:
- Increased disk I/O.
- Higher application latency.
- Increased write pressure on storage devices.
- Lower page reclaim efficiency.
Therefore, with the same page fault rate, prioritizing clean pages for reclaiming often helps reduce reclaim cost.
4.4 Average Memory Access Time
A simplified model can be used to understand the impact of page faults on performance:
Where:
- EAT: average effective access time.
- p: page fault probability.
- MA: normal memory access time.
- PF: average page fault handling time.
Assume:
- Normal memory access time is 100 ns.
- Page fault handling time is 10 ms.
- Page fault probability is 0.0001.
Then:
Although the page fault probability is only 1 in 10,000, the average access time has increased significantly.
This model is a simplified analysis; actual page fault handling is also affected by factors such as page source, cache state, storage device, and concurrent I/O.
5. FIFO Page Replacement Algorithm
FIFO stands for First In First Out; it is a first-in, first-out algorithm.
Its core idea is:
The page that entered memory earliest is evicted first.
FIFO does not care whether a page has been accessed recently, nor does it care about the page's importance; it only records the order in which pages entered memory.
5.1 How FIFO Works
Assume physical memory has only 3 page frames, and the page access sequence is:
1, 2, 3, 1, 4, 2, 5
Initially, memory is empty.
| Accessed page | Memory state | Page fault? | Evicted page |
|---|---|---|---|
| 1 | 1 | Yes | None |
| 2 | 1, 2 | Yes | None |
| 3 | 1, 2, 3 | Yes | None |
| 1 | 1, 2, 3 | No | None |
| 4 | 2, 3, 4 | Yes | 1 |
| 2 | 2, 3, 4 | No | None |
| 5 | 3, 4, 5 | Yes | 2 |
The total number of page faults is 5.
5.2 FIFO Execution Process
flowchart TD
A["Page enters memory"] --> B["Record entry order"]
B --> C["Receive a new page access"]
C --> D{"Is there a free page frame?"}
D -->|Yes| E["Allocate a free page frame"]
D -->|No| F["Select the page that entered earliest"]
F --> G["Evict the old page"]
G --> H["Load the new page"]
E --> H
H --> I["Update the FIFO queue"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style D fill:#FFF1CC,stroke:#E5C77A,color:#755719
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style I fill:#E9E1F8,stroke:#B7A4D8,color:#514078
5.3 Advantages of FIFO
- The algorithm is simple.
- Low implementation cost.
- It only needs to maintain the order in which pages enter memory.
- It does not require complex access history statistics.
5.4 Disadvantages of FIFO
- It ignores page access frequency.
- It ignores whether a page has been accessed recently.
- It may evict pages that are being used frequently.
- The page fault rate does not necessarily decrease as the number of physical page frames increases.
The most important problem among these is Belady's anomaly.
5.5 Belady's Anomaly
Belady's anomaly refers to the phenomenon in which some page replacement algorithms produce more page faults after the number of physical page frames is increased.
FIFO can exhibit this behavior.
For example, consider the page access sequence:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Using the FIFO algorithm:
| Number of Frames | Number of Page Faults |
|---|---|
| 3 | 9 |
| 4 | 10 |
As can be seen, when the number of frames increases from 3 to 4, the number of page faults increases instead.
This shows that FIFO does not have the stack algorithm property.
6. OPT Page Replacement Algorithm
OPT, Optimal Page Replacement, is the optimal page replacement algorithm.
Its core idea is:
Evict the page that will not be accessed for the longest time in the future.
Theoretically, OPT can achieve the minimum number of page faults, so it is often used to evaluate the effectiveness of other page replacement algorithms.
6.1 How OPT Works
Suppose:
- Physical memory has 3 page frames.
- The page access sequence is
1, 2, 3, 4, 1, 2, 5.
When page 4 is accessed, pages 1, 2, and 3 are already in memory.
Look ahead in the access sequence:
Current memory: 1, 2, 3
Future accesses: 1, 2, 5
Page 3 will not appear again in the future, so page 3 should be evicted.
OPT makes the optimal choice based on future access patterns.
6.2 Example Execution of OPT
| Accessed Page | Memory State | Page Fault? | Evicted Page |
|---|---|---|---|
| 1 | 1 | Yes | None |
| 2 | 1, 2 | Yes | None |
| 3 | 1, 2, 3 | Yes | None |
| 4 | 1, 2, 4 | Yes | 3 |
| 1 | 1, 2, 4 | No | None |
| 2 | 1, 2, 4 | No | None |
| 5 | 1, 2, 5 | Yes | 4 |
The total number of page faults is 5.
6.3 Advantages of OPT
- Theoretically, it has the fewest page faults.
- It can serve as a performance benchmark for other algorithms.
- It helps analyze different page access patterns.
6.4 Disadvantages of OPT
The biggest problem with OPT is that it requires knowing the future page access sequence.
Real systems cannot accurately predict which pages all processes will access in the future.
Therefore, OPT is usually used for theoretical analysis and algorithm simulation, rather than being directly used for actual page replacement in general-purpose operating systems.
7. LRU Page Replacement Algorithm
LRU, Least Recently Used, is the least-recently-used algorithm.
Its core idea is:
Evict the page that has not been accessed for the longest time.
LRU assumes that a page that has recently been accessed still has a relatively high probability of being accessed again within a short time.
This idea leverages the temporal locality of program accesses.
7.1 What Is Temporal Locality?
Temporal locality (Temporal Locality) refers to:
After a piece of data or an instruction has just been accessed, the probability that it will be accessed again in the near future is relatively high.
For example:
for (int i = 0; i < 100000; i++) {
sum += data[i];
}
Variables and some instructions in the loop are accessed repeatedly, showing obvious temporal locality.
LRU preferentially retains pages that have been used recently.
7.2 How LRU Works
Suppose memory has 3 page frames, and the access sequence is:
1, 2, 3, 1, 4, 2, 5
| Accessed Page | Pages in Memory | Recency Order | Page Fault? | Evicted Page |
|---|---|---|---|---|
| 1 | 1 | 1 | Yes | None |
| 2 | 1, 2 | 2, 1 | Yes | None |
| 3 | 1, 2, 3 | 3, 2, 1 | Yes | None |
| 1 | 1, 2, 3 | 1, 3, 2 | No | None |
| 4 | 1, 3, 4 | 4, 1, 3 | Yes | 2 |
| 2 | 1, 2, 4 | 2, 4, 1 | Yes | 3 |
| 5 | 2, 4, 5 | 5, 2, 4 | Yes | 1 |
The total number of page faults is 6.
7.3 LRU Execution Process
flowchart TD
A["Received page access request"] --> B["Check whether page is resident"]
B --> C{"Does the page exist?"}
C -->|Exists| D["Update last access time"]
C -->|Does not exist| E{"Is there a free page frame?"}
E -->|Yes| F["Load new page"]
E -->|No| G["Find least recently used page"]
G --> H["Evict target page"]
H --> F
F --> I["Update access order"]
D --> I
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style C fill:#FFF1CC,stroke:#E5C77A,color:#755719
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#FFF1CC,stroke:#E5C77A,color:#755719
style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style I fill:#E9E1F8,stroke:#B7A4D8,color:#514078
7.4 Implementation Methods of LRU
LRU needs to know the order in which pages were most recently accessed.
Common implementation methods include:
Method 1: Timestamps
Record the current time each time a page is accessed.
During replacement, scan all pages and select the page with the earliest timestamp.
Advantages:
- Easy to understand and implement.
- Suitable for small-scale simulations.
Disadvantages:
- Requires maintaining time information.
- May require scanning when selecting a victim page.
- Update cost is relatively high under high-concurrency access.
Method 2: Doubly linked list plus hash table
Use a doubly linked list to maintain access order.
- Head of the list: most recently accessed page.
- Tail of the list: least recently used page.
- Hash table: quickly locate a node by page number.
When a page is accessed, move its node to the head of the list.
When replacement is needed, evict the node at the tail of the list.
With this structure, access and eviction can usually both achieve O(1) on average.
7.5 Advantages of LRU
- Can exploit temporal locality.
- Usually conforms better to program access patterns than FIFO.
- Does not require predicting future accesses.
- Has good theoretical properties.
7.6 Disadvantages of LRU
- Requires recording access history.
- Each access may require updating the data structure.
- An exact implementation incurs additional overhead.
- May perform poorly in certain sequential scan scenarios.
Especially in operating system kernels, frequently maintaining an exact LRU linked list may cause high lock contention and management overhead.
Therefore, real systems often adopt approximate LRU strategies.
8. Clock Page Replacement Algorithm
The Clock algorithm is also called the Second Chance algorithm.
It is an improvement on FIFO and uses the page reference bit to determine whether a page has been accessed recently.
Core idea:
If a page has been accessed recently, do not evict it for the time being; instead, give it one chance to remain in memory.
8.1 Data Structure of Clock
Clock usually organizes pages into a circular structure.
Each page maintains a Reference Bit:
- 0: No access has been recorded recently.
- 1: Recently accessed.
The system maintains a pointer to the current position being checked.
8.2 How Clock Works
When a page needs to be replaced:
- Check the page pointed to by the current pointer.
- If the reference bit is 0, select that page as the victim page.
- If the reference bit is 1, reset it to 0.
- Move the pointer to the next page.
- Repeat the check until a page with a reference bit of 0 is found.
flowchart TD
A["Need to reclaim physical page frames"] --> B["Check page at current pointer"]
B --> C{"Is reference bit 0?"}
C -->|Yes| D["Select current page"]
C -->|No| E["Set reference bit to 0"]
E --> F["Move pointer to next page"]
F --> B
D --> G["Reclaim target page"]
G --> H["Load new page"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style C fill:#FFF1CC,stroke:#E5C77A,color:#755719
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#F1F3F5,stroke:#B9C1CA,color:#475569
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
8.3 Advantages of Clock
- Relatively simple to implement.
- Does not need to maintain an exact access order.
- Suitable for using hardware reference bits.
- Focuses more on whether a page has been accessed recently than simple FIFO.
8.4 Disadvantages of Clock
- Can only approximate page usage.
- The reference bit cannot accurately reflect access time.
- When page access patterns are complex, the selection may not be ideal.
- The pointer may need to scan multiple pages before finding a candidate page.
8.5 Enhanced Clock
In addition to the reference bit, the enhanced Clock also considers the dirty bit.
This divides pages into four types:
| Reference bit R | Dirty bit M | Page state | Priority |
|---|---|---|---|
| 0 | 0 | Not recently accessed, clean | Reclaim first |
| 0 | 1 | Not recently accessed, dirty page | Second priority |
| 1 | 0 | Recently accessed, clean | Defer reclamation |
| 1 | 1 | Recently accessed, dirty page | More likely to be retained |
The enhanced Clock preferentially looks for pages that have not been accessed recently and are clean, in order to reduce write-back costs.
9. LFU Page Replacement Algorithm
LFU stands for Least Frequently Used, the least frequently used algorithm.
Its core idea is:
Evict the page with the fewest accesses.
Unlike LRU, which focuses on recent access time, LFU focuses on the cumulative access frequency of pages.
9.1 How LFU Works
Suppose there are three pages in physical memory:
| Page | Cumulative access count |
|---|---|
| A | 10 |
| B | 3 |
| C | 7 |
When a page needs to be replaced, LFU selects B first.
Because B has the lowest cumulative access count.
9.2 Advantages of LFU
- Can identify pages that are accessed frequently over the long term.
- Suitable for scenarios where access frequency is relatively stable.
- Can retain hot data.
9.3 Disadvantages of LFU
The biggest problem with LFU is that historical access counts may affect current decisions.
For example:
A page may have been accessed many times during program startup and later is no longer used, but its cumulative access count remains high.
It may occupy memory for a long time and prevent genuinely new hot spots from entering.
Common improvement methods:
- Periodically decay access counts.
- Use a sliding window to count frequency.
- Combine LRU and LFU.
- Introduce weights for access time and frequency.
10. Comparison of Different Page Replacement Algorithms
Common algorithms each have their own characteristics, and no single algorithm is suitable for all workloads.
| Algorithm | Core Strategy | Main Advantages | Main Disadvantages |
|---|---|---|---|
| FIFO | Evict the page that entered earliest | Simple to implement | May evict hot pages |
| OPT | Evict the page whose next access is farthest in the future | Theoretically optimal | Cannot accurately predict the future |
| LRU | Evict the least recently used page | Exploits temporal locality | Exact maintenance is costly |
| Clock | Use reference bit to approximate LRU | Low cost | Not precise enough |
| Enhanced Clock | Combine reference bit and modified bit | Balance access status and write-back cost | Still an approximation |
| LFU | Evict the page with the fewest accesses | Retain high-frequency pages | Easily affected by historical accesses |
10.1 Relationships Between Algorithms
flowchart TD
A["Page Replacement Algorithms"] --> B["Based on Entry Order"]
A --> C["Based on Access History"]
A --> D["Based on Theoretical Optimum"]
B --> E["FIFO"]
C --> F["LRU"]
C --> G["LFU"]
C --> H["Clock"]
H --> I["Enhanced Clock"]
D --> J["OPT"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style D fill:#FFF1CC,stroke:#E5C77A,color:#755719
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style I fill:#FFF1CC,stroke:#E5C77A,color:#755719
style J fill:#FFF1CC,stroke:#E5C77A,color:#755719
11. Belady's Anomaly and Stack Algorithms
Page replacement algorithms also have an important theoretical property: after increasing the number of physical page frames, is it guaranteed that the number of page faults will not increase?
Not all algorithms can guarantee this.
11.1 What Is Belady's Anomaly?
Belady's anomaly means:
For some page replacement algorithms, after the number of physical page frames is increased, the number of page faults actually increases.
FIFO is a typical example.
11.2 What Is a Stack Algorithm?
A stack algorithm is a class of page replacement algorithms that has the set inclusion property.
For the same page access sequence, if the set of resident pages when using n page frames is S_n, and the set when using n+1 page frames is S_{n+1}, then it satisfies:
In other words, increasing the number of page frames will not exclude pages that should originally have remained resident.
Algorithms with this property will not suffer from Belady's anomaly.
Typical examples:
- LRU.
- OPT.
FIFO does not have this property, so Belady's anomaly may occur.
12. Working Set Model: How Does the Operating System Determine How Much Memory a Process Needs?
The working set is an important concept in operating system memory management.
It describes the set of pages that a process has actually accessed during a recent period of time.
The working set model was proposed by Peter Denning, and its core idea is:
The memory a process currently needs is usually related to the pages it has actually used during a recent period of time.
12.1 Definition of the Working Set
Suppose:
- The current time is t.
- The working set window is Δ.
- The set of pages accessed within the most recent Δ time is W.
Then:
For example, a process has recently accessed:
1, 2, 3, 1, 4, 2, 1, 3
If the working set window covers the entire access interval, then its working set is:
{1, 2, 3, 4}
Even if page 1 is accessed multiple times, it is counted only once in the working set.
12.2 Why Is the Working Set Important?
The working set can help the operating system understand which pages a process truly needs at present.
If a process's entire working set cannot reside in physical memory, page faults may occur frequently.
If the sum of the working sets of multiple processes exceeds the available physical memory of the system, memory contention and thrashing may occur.
The following model can be used to understand this:
where:
- D: The total number of pages required by the working sets of all processes.
- W_i: The working set of the i-th process.
- n: The number of processes.
If:
then the system may be unable to simultaneously satisfy the working set requirements of all processes.
12.3 Relationship Between Working Sets and Page Replacement
flowchart TD
A["Process continuously accesses pages"] --> B["Record recent access patterns"]
B --> C["Calculate the working set"]
C --> D["Estimate the process's active memory demand"]
D --> E{"Can the working set remain resident?"}
E -->|Can| F["Keep the main active pages resident"]
E -->|Cannot| G["Frequent page faults occur"]
G --> H["Increase page reclaim pressure"]
H --> I["Analyze process concurrency and memory demand"]
I --> J["Adjust the load or add memory"]
F --> K["Continuously monitor access patterns"]
J --> K
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF1CC,stroke:#E5C77A,color:#755719
style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style G fill:#FCE8E8,stroke:#D9A1A1,color:#824444
style H fill:#FFF1CC,stroke:#E5C77A,color:#755719
style I fill:#E9E1F8,stroke:#B7A4D8,color:#514078
style J fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style K fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
12.4 Working Sets and Locality
When a program accesses memory, there are usually two important types of locality:
Temporal locality:
Data or instructions that were recently accessed may be accessed again in the future.
Spatial locality:
After an address is accessed, nearby addresses may be accessed soon afterward.
For example:
for (int i = 0; i < 100000; i++) {
array[i] = i;
}
Accessing array elements consecutively demonstrates spatial locality.
Repeated execution of the loop body demonstrates temporal locality.
The working set model leverages the locality characteristics of programs to help the system understand the currently active pages.
13. Thrashing: What Happens When Page Replacement Is Too Frequent?
Thrashing is usually translated as page thrashing or memory thrashing.
When the system has insufficient physical memory and cannot hold the active working sets of processes, pages may be continuously swapped out and then quickly accessed again.
As a result, the system spends a large amount of time handling page faults, reading pages, and reclaiming pages, rather than executing actual business logic.
13.1 Typical Symptoms of Thrashing
- The frequency of page faults increases significantly.
- Swap read/write volume continues to increase.
- Disk I/O remains busy for extended periods.
- Application response time becomes significantly longer.
- CPU utilization may decrease.
- System throughput decreases.