Trashing in Operating System
Introduction
In an operating system, processes need memory (RAM) to run efficiently. When there are too many
processes and not enough memory, the system keeps moving pages in and out of memory very frequently.
This situation where the CPU spends more time swapping pages instead of doing actual work is called
Trashing. Trashing badly affects the system performance and slows down all processes.
Thus, trashing happens when the system is busy with paging activity instead of process execution.
What is Trashing?
- Trashing occurs when a process spends more time paging than executing.
- It usually happens when there are too many active processes in memory and not enough frames available
for them.
- The system keeps loading pages and replacing pages continuously, resulting in very few instructions being
executed between page faults.
- It leads to high CPU utilization for paging activities but low useful work.
Cause of Trashing
1. High Degree of Multiprogramming:
- Too many processes running simultaneously.
- All processes compete for limited memory, causing frequent page faults.
2. Insufficient Frames:
- Each process does not get enough frames to hold its working set.
3. Poor Page Replacement Policies:
- Bad replacement algorithms may remove useful pages, causing more page faults.
4. Increase in CPU Utilization:
- OS may add more processes thinking CPU is free, but actually, CPU is busy with paging.
Trashing in Operating System
How Trashing Works
1. A process needs a set of pages to run efficiently (working set).
2. If working set pages are not in memory, page faults occur.
3. OS will swap in required pages and swap out others.
4. This swapping repeats again and again because the process cannot keep its working set in memory.
5. CPU spends more time handling page faults and less time executing instructions.
6. As a result, system slows down heavily, even though CPU utilization may look high.
Example of Trashing
Suppose three processes A, B, and C are running.
- Each process requires 10 frames to work efficiently.
- Total frames available in the system = 20.
Thus:
- A gets 7 frames
- B gets 7 frames
- C gets 6 frames
None of the processes have enough frames. They keep replacing and swapping pages constantly.
Result:
- High page fault rate
- CPU busy swapping pages
- Low process execution
This is trashing.
Detection of Trashing
Operating System can detect trashing by observing:
- High page-fault rate
- Low CPU utilization
- Processes making little progress
- High disk I/O activity
Trashing in Operating System
Solutions to Avoid Trashing
1. Working Set Model:
- Allocate enough frames to hold the working set.
2. Page Fault Frequency (PFF) Control:
- Monitor page fault rate and adjust frames accordingly.
3. Reduce Degree of Multiprogramming:
- Suspend some processes temporarily to free memory.
4. Use Good Page Replacement Algorithms:
- LRU, LFU reduce unnecessary page replacements.
5. Load Control:
- Control the number of processes admitted to memory.
Advantages of Controlling Trashing
- Better CPU utilization
- Faster process execution
- Reduced disk I/O
- Smooth system performance
- Reduced system overhead
Disadvantages if Trashing Occurs
- Extremely slow system response
- High memory and CPU waste
- Programs take too long to complete
- Increased wear on hard drives
- Frustration for users