Thrashing, Locality of Reference and Cache Memory in Operating System
These three topics look separate at first, but they are actually connected by one important idea: keep frequently needed data close to the CPU and avoid expensive memory transfers.
Locality of Reference explains why a program repeatedly uses a small region of memory. Cache Memory takes advantage of that behavior. Virtual-memory systems also depend on locality. When a process does not have enough frames for the pages it is actively using, page faults can become extremely frequent and the system can enter Thrashing.
Locality of Reference means programs tend to access recently used or nearby memory locations repeatedly.
Cache Memory is a small and very fast memory located close to the CPU that stores recently or frequently needed data and instructions.
Thrashing happens in virtual memory when excessive page faults cause the system to spend too much time moving pages between RAM and storage instead of performing useful computation.
The Big Connection: Locality, Cache and Thrashing
Modern computer systems contain several levels of memory. The fastest memory is small and expensive. Larger memory is slower.
The complete memory system therefore works efficiently only when the computer keeps the most useful data in the faster levels.
Good Locality → Useful data stays close → More Cache Hits and fewer unnecessary page transfers
Poor memory availability for active pages → More Page Faults → Possible Thrashing
Memory Hierarchy Before Understanding Cache and Thrashing
Computer memory is arranged in levels. Faster memory is generally smaller, while larger memory is usually slower.
What Is Locality of Reference?
Programs usually do not access random memory locations equally. During a short period, they tend to repeatedly access a relatively small region of memory.
Locality of Reference is the tendency of a program to repeatedly access recently used memory locations or locations close to recently accessed memory.
This property is extremely important because both cache systems and virtual memory can use it to keep useful data closer to the processor.
Visual Example of Locality
Imagine a process with many memory blocks, but only a small area is being accessed repeatedly.
The darker active region represents memory locations used repeatedly. Keeping such frequently accessed data in faster memory can improve performance.
Types of Locality of Reference
Example: instructions inside a loop.
Example: nearby elements of an array.
Example: reading an array from beginning to end.
Temporal Locality Example
Consider this repeated sequence:
A, B and C are repeatedly reused. This is temporal locality.
Spatial Locality Example
Suppose a program accesses array elements:
These values are stored near each other. Fetching a block containing nearby values allows future references to benefit from the data already brought closer to the CPU.
Why Is Locality Important?
What Is Cache Memory?
The CPU is much faster than main memory. If the processor had to wait for RAM on every memory access, a lot of CPU time would be wasted.
Cache Memory is a small and very fast memory located close to the CPU that stores copies of frequently or recently used instructions and data.
The processor checks the cache before going to slower memory levels.
How Cache Memory Works
Cache Hit and Cache Miss
Cache Hit
The requested data is already available in the cache.
Cache Miss
The requested data is not present in the cache.
Cache Hit Ratio, Miss Ratio and Average Memory Access Time
Cache Performance Solved Example
Given
Step 1: Find Miss Rate
Step 2: Apply AMAT Formula
Average Memory Access Time = 3.5 ns
L1, L2 and L3 Cache
| Cache Level | General Position | Typical Characteristic | Purpose |
|---|---|---|---|
| L1 | Closest to CPU core | Smallest and fastest of common cache levels | Serve very frequent accesses quickly |
| L2 | Near or within CPU core hierarchy | Larger than L1, usually slower than L1 | Back up L1 misses |
| L3 | Often shared by multiple cores | Larger but generally slower than L1 and L2 | Reduce accesses to RAM |
Basic Cache Mapping Techniques
When a memory block enters cache, the system needs a rule for deciding where that block can be stored.
Main Types of Cache Misses
Write-Through and Write-Back Cache
Write-Through
A write updates the cache and also propagates the change to the lower memory level according to the system design.
Write-Back
A write updates the cached copy first. The modified block is written back later when required.
Does the Operating System Directly Control CPU Cache?
Not in the same way that it manages virtual-memory pages.
Modern CPU caches are largely controlled by processor hardware. Cache hits, cache-line fills and many replacement decisions happen automatically in hardware.
CPU Cache = Mostly hardware-managed
Virtual Memory Pages = Hardware and Operating System cooperate closely
The operating system can still influence cache behavior indirectly through scheduling, memory placement, page mapping, process migration and architecture-specific cache or coherency operations.
CPU Cache vs Operating System Page Cache
The word "cache" can refer to more than one thing.
What Is Thrashing in Operating System?
Virtual memory works well when the pages a process actively needs remain in RAM most of the time.
Problems begin when a process or group of processes receives too few frames. Pages are removed, then quickly needed again, causing another page fault.
Thrashing is a condition in which excessive page faults cause the operating system to spend a large amount of time moving pages between main memory and secondary storage instead of performing useful execution.
How Thrashing Starts Step by Step
Main Causes of Thrashing
Symptoms of Thrashing
- Very high page-fault activity.
- Heavy storage input/output caused by paging.
- Low useful CPU progress.
- Processes frequently wait for pages.
- System responsiveness becomes poor.
- Adding more active processes can make performance worse.
Working Set and Thrashing
A process usually has a group of pages that it actively uses during a recent period of execution.
The Working Set is the collection of pages a process has been actively referencing during a recent execution window.
If enough of those pages stay in RAM, the process can continue with relatively few page faults.
If too many working-set pages are missing, the process repeatedly faults and performance drops.
In this example, Pages 1, 2 and 3 represent the currently active region.
Working Set and Locality Are Closely Related
A working set is useful because program execution usually shows locality.
During one phase, a process may execute one function and repeatedly access a specific group of data pages.
Later, the program can move to another phase and its working set can change.
Page Fault Frequency Method
Another way to control memory pressure is to observe how frequently page faults occur.
PFF = Number of Page Faults / Number of Page References
How Page Fault Frequency Can Guide Frame Allocation
Fault Rate Too High
The process may need more physical frames.
Fault Rate Very Low
The process may have more frames than it currently needs.
Degree of Multiprogramming and Thrashing
Multiprogramming means keeping several processes active so that the CPU can work on another process when one process waits.
Increasing multiprogramming can improve resource use, but only up to a point.
Example
Suppose physical memory can comfortably hold the active working sets of four processes.
If many more memory-intensive processes are added without enough RAM, each process may receive fewer frames.
Page faults rise, storage traffic rises, and useful progress may fall.
How to Prevent or Reduce Thrashing
Local and Global Page Replacement During Memory Pressure
Local Replacement
A process selects a victim from its own allocated frames.
This can limit how directly one process affects another process's resident pages.
Global Replacement
A replacement decision can draw from a broader pool of frames, depending on the system policy.
This provides flexibility but can allow one workload's behavior to influence another workload more strongly.
Simple Thrashing Example
Suppose a process actively needs these pages:
But it receives only two physical frames.
The process moves repeatedly among P1, P2, P3 and P4. Two pages cannot hold the complete four-page active set.
Cache Miss vs Page Fault
| Basis | Cache Miss | Page Fault |
|---|---|---|
| Meaning | Requested data is not in the current cache level | Required virtual page is not currently resident in physical memory |
| Handled Mainly By | Processor hardware and cache hierarchy | Hardware detects condition, operating system handles fault |
| Next Level | Lower cache level or RAM | RAM must obtain page data from its backing source when necessary |
| Typical Cost | Usually much smaller than a page fault | Can be extremely expensive because storage I/O may be involved |
| Relation to Locality | Good locality improves hit rate | Good locality helps active pages stay useful in RAM |
Cache Memory vs Virtual Memory vs Thrashing
| Basis | Cache Memory | Virtual Memory | Thrashing |
|---|---|---|---|
| Type | Fast memory mechanism | Memory-management abstraction | Performance problem |
| Main Goal | Reduce average data-access time | Provide flexible virtual address spaces and page management | Not a goal; it is an undesirable condition |
| Main Location | Near or inside processor hierarchy | Uses page tables, RAM and backing storage | Occurs when paging activity becomes excessive |
| Main Event | Cache Hit / Cache Miss | Page translation / Page Fault | Very frequent page faults |
| Relation to Locality | Strongly benefits from locality | Demand paging benefits from locality | Poor frame availability for the active locality can cause it |
Cache AMAT Calculator
Use this tool to calculate Average Memory Access Time.
Page Fault Frequency Calculator
This calculator finds the observed page-fault ratio for a sequence of memory references.
Common Student Confusions
Is Cache Memory the Same as RAM?
No. Cache is much smaller and faster and sits closer to the CPU. RAM is larger main memory.
Is Cache Memory the Same as Virtual Memory?
No. CPU cache reduces access latency. Virtual memory provides address translation and allows pages to move between RAM and backing storage when needed.
Is a Cache Miss the Same as a Page Fault?
No. A cache miss usually means a lower cache level or RAM must be checked. A page fault means the required virtual page is not currently resident in physical memory.
Does Every High Page Fault Rate Mean Thrashing?
Not automatically. Thrashing refers to sustained excessive paging activity that seriously reduces useful system progress.
Does Good Locality Guarantee No Thrashing?
No. Good locality helps, but if a process still receives too few frames for its active working set, page faults can remain high.
Does the Operating System Manage L1 Cache Like It Manages Pages?
No. CPU caches are mainly hardware-managed, while virtual-memory page management requires much more direct operating-system involvement.
Is More Multiprogramming Always Better?
No. If memory pressure becomes too high, adding more processes can increase paging and reduce performance.
Locality, Cache and Thrashing in One View
Cache is a performance mechanism.
Thrashing is a virtual-memory performance failure condition.
One-Minute Revision
Thrashing, Locality and Cache Memory Exam Preparation
2-Mark Locality Definition
Locality of Reference is the tendency of a program to repeatedly access recently used memory locations or memory locations close to recently accessed locations.
2-Mark Cache Definition
Cache Memory is a small and high-speed memory located close to the CPU that stores frequently or recently used instructions and data.
2-Mark Thrashing Definition
Thrashing is a condition in which excessive page faults cause a system to spend more time moving pages between RAM and storage than performing useful process execution.
Important Formulas
For a 5-Mark Question
- Define Locality of Reference.
- Explain Temporal and Spatial Locality.
- Define Cache Memory.
- Explain Cache Hit and Cache Miss.
- Define Thrashing and list its causes.
For a 10-Mark Question
- Memory hierarchy.
- Meaning of Locality of Reference.
- Temporal Locality.
- Spatial Locality.
- Sequential Locality.
- Working Set.
- Cache Memory.
- Cache Hit and Miss.
- Hit Ratio and Miss Ratio.
- Average Memory Access Time.
- L1, L2 and L3 Cache.
- Cache Mapping.
- Cache Miss Types.
- Thrashing.
- Causes and Symptoms of Thrashing.
- Page Fault Frequency.
- Working Set Model.
- Thrashing Prevention.
Frequently Asked Questions
What is Thrashing in Operating System?
Thrashing is a condition where excessive page faults cause a system to spend a large amount of time transferring pages between RAM and backing storage instead of performing useful computation.
What is Locality of Reference?
Locality of Reference is the tendency of a program to repeatedly access recently used memory locations or nearby locations.
What is Temporal Locality?
Temporal Locality means that a memory location used recently is likely to be used again soon.
What is Spatial Locality?
Spatial Locality means that memory locations near a recently accessed location are likely to be accessed soon.
What is Cache Memory?
Cache Memory is a small and very fast memory near the CPU that stores frequently or recently used instructions and data.
What is a Cache Hit?
A Cache Hit occurs when the requested data is found in the cache.
What is a Cache Miss?
A Cache Miss occurs when the requested data is not found in the cache and a lower memory level must be accessed.
What is the Working Set of a Process?
The Working Set is the group of virtual-memory pages actively referenced by a process during a recent execution period.
What is Page Fault Frequency?
Page Fault Frequency is the ratio of page faults to page references during an observed interval.
How Can Thrashing Be Reduced?
Thrashing can be reduced by allocating enough frames for active pages, using working-set or page-fault-frequency control, and reducing the degree of multiprogramming when memory pressure becomes too high.
Is Cache Memory the Same as Virtual Memory?
No. Cache Memory reduces access latency by keeping frequently used data close to the CPU, while Virtual Memory provides address translation and manages virtual pages across RAM and backing storage.
What is the Difference Between a Cache Miss and a Page Fault?
A Cache Miss means data is not present in the current cache level, while a Page Fault means the required virtual-memory page is not currently resident in physical RAM.
Conclusion
Locality of Reference explains one of the most important behaviors of real programs: programs repeatedly access a relatively small area of memory during a period of execution.
Cache Memory takes advantage of this behavior by keeping recently or frequently used data close to the CPU.
A high cache hit rate can significantly reduce average memory-access time.
Virtual memory also benefits from locality. If the actively used pages of a process remain in RAM, execution can continue with relatively few page faults.
But if too many active pages compete for too few physical frames, the operating system may continuously load and remove pages.
This excessive paging condition is called Thrashing.
Working-set analysis, Page Fault Frequency monitoring, suitable frame allocation and control of multiprogramming can help reduce the risk.
Temporal Locality = Use Again Soon
Spatial Locality = Use Nearby Location Soon
Cache Hit = Data Found in Cache
Cache Miss = Go to Lower Memory Level
Working Set = Active Pages
Too Few Frames + High Page Faults = Thrashing
Good Locality = Better Cache and Virtual-Memory Performance
