Thrash is a term used in computer science to describe the poor performance of a virtual memory system when the same pages are loaded repeatedly owing to a shortage of main memory to store them in secondary memory.
Thrashing in OS (Operating System) happens when a computer's virtual memory resources are overutilized, resulting in a persistent state of paging and page faults, which inhibits most application-level activity. It causes the computer's performance to decline or collapse. The scenario can last indefinitely unless the user stops certain running apps or active processes to free up extra virtual memory resources.
What is Thrashing in OS (Operating System)?
Thrashing in OS is a common phenomenon in computer systems. It occurs when the system spends more time swapping pages than actually performing useful tasks. It is caused by a high number of page faults, which happens when the system needs to retrieve a page from the disk because it is not present in memory. Thrashing in operating system is a phenomenon in computing that occurs when virtual memory is employed. It occurs when a computer's virtual memory rapidly exchanges data for data on the hard drive, to the exclusion of most application-level operations. As main memory is depleted, more pages must be swapped into and out of virtual memory.
To know more about thrashing in OS, first, we need to know about page faults and swapping.
- Page fault: A page fault is a type of interrupt that occurs when a program attempts to access a page of memory that is not currently mapped to physical memory, leading to a disk I/O operation.
- Swapping: Swapping results in a high rate of hard drive access. Thrashing can last for a long time if the underlying problem is not addressed. Thrashing in operating system has the ability to cause the computer's hard disk to fail completely.
Thus, thrashing in operating system is sometimes referred to as disk thrashing.
After visualizing the Graphical representation,
- The underlying idea is that if a process is given too few frames, there would be too many and too frequent page faults. As a result, the CPU would perform no useful work, and CPU usage would plummet drastically.
- The long-term scheduler would then attempt to enhance CPU usage by loading additional processes into memory, increasing the degree of multiprogramming. Unfortunately, this would result in even lower CPU utilization, creating a chain reaction of larger page faults followed by a rise in the degree of multiprogramming, a phenomenon known as Thrashing.
Causes of Thrashing in Operating System
- Thrashing in the operating system has an impact on the performance of the operating system's execution.
- Thrashing causes serious performance issues in the operating system.
- We can also argue that as soon as the memory is full, the process begins to take a long time to swap in the needed pages. Because most programs are waiting for pages, CPU utilization drops once again.
- The high degree of multiprogramming and a shortage of frames are two of the most common reasons for thrashing in the operating system.
Algorithms during Thrashing
In order to deal with page faults, the operating system uses either the local frames replacement algorithm or the global frames replacement algorithm to try to bring in enough pages in the main memory. Let's explore how various replacement strategies affect Thrashing.
1. Global Page Replacement
The Global Page replacement has the ability to bring any page, and once Thrashing in the operating system is detected, it attempts to bring more pages. As a result of this, no process can acquire enough frames, and the thrashing in the operating system will get worse. To conclude, when Thrashing in operating system occurs, the global page replacement technique is ineffective.
2. Local Page Replacement
Unlike the Global Page Replacement, the Local Page Replacement will choose pages that belong to that process. As a result, there is a potential that the Thrashing in operating system will be reduced. As previously been demonstrated, there are several drawbacks to Local Page replacement. As a result, local page replacement is merely a different option than global page replacement.
How to Overcome Thrashing?
Thrashing has an adverse effect on hard disk health and system performance. As a result, some steps must be taken to avoid it. The following strategies can be used to solve the thrashing problem:
- Upgrade the RAM size - As inadequate memory might result in disk thrashing, one approach is to upgrade the RAM. With additional memory, your computer can perform tasks more simply and without having to work as hard. It is, in general, the best long-term answer.
- Replace programs - In order to overcome thrashing, what we can do is we can remove heavy memory-intensive programs with relatively less memory-intensive alternatives.
- Reduce the number of active applications - If you have too many apps running in the background, your system resources will be depleted quickly. Furthermore, the remaining system resource is sluggish, which might cause Thrashing. As a result, when you close an application, certain resources are released, allowing you to prevent Thrashing in operating system to some extent.
- Alter the size of the swap file - There is a probability of occurrence of thrashing also when the swap file is not properly configured in the operating system. We can define swapping as when a page fault occurs. The operating system attempts to retrieve that page from secondary memory and swap it with one of the pages in RAM.
Techniques to Prevent Thrashing
The Local Page replacement is superior to the Global Page replacement, although it has several drawbacks and is not always useful. As a result, here are some more strategies for dealing with Thrashing.
1. Working-Set Model
A process goes from locality to locality when it runs, while a locality is a set of pages that are actively being used together. The working set window is defined by the parameter Δ(theta) in this model. The working set is the collection of pages in the most current Δ page references. The working set contains pages that are currently in use.
If Δ is too small, the entire locality will not be covered. On the other hand, if Δ is too large, it may overlap numerous localities. The size of the working set is thus the essential feature.
We calculate the working-set size, WSSi, for each process in the system if there are m processes.
D = ΣWSSi, where D is the overall demand for frames. Each process actively makes use of the pages in its working set. As a result, WSSi frames are required by process i. If the overall demand exceeds the total amount of possible frames (D > m), thrashing will occur because certain processes will run out of frames.
After choosing, the operating system monitors each process's working set and allocates enough frames to that working set to provide it with its working-set size. Another process can be started if there are enough spare frames. The OS selects a process to suspend if the sum of the working-set sizes exceeds the total number of available frames.
The series of numbers in the following diagram represents the pages required by a process. If the working set size is set to nine, the following pages are displayed at times t1 and t2:
How do we compute Working Sets?
Peter D.Denning introduced the working set parameter as T, which means that the working set is made up of all pages that have been referenced in the recent T seconds. The clock method may be extended to retain an idle time for each page. The working set contains pages with idle times shorter than T.
2. Page Fault Frequency
This is a straightforward model. We must act depending on the frequency/rate of page faults and assign frames to each process accordingly. For this page-fault rate, we specified an upper bound (UB) and a lower bound (LB). We compare the page fault rate(R) of each process to the stated upper bound(UB) and lower bound(LB).
If R > UB, we can deduce that a process requires additional frames to keep this rate under control. To avoid thrashing, we'll need to dedicate extra frames to it. If there aren't any frames available, the process can be paused until a sufficient number of frames becomes available. The allotted frames should be assigned to another high-paging process once this process has been suspended.
We have more than enough frames for a process if R < LB, and some of them can be given to other processes. We can maintain a balance between frame needs and frame allocation by using the R, UB, and LB.
3. Locality Model
The locality model in thrashing is based on the observation that when a system is thrashing, a large proportion of memory accesses are likely to be concentrated in a few pages of memory that are in high demand. By identifying and prioritizing these pages, it may be possible to reduce the number of page faults and alleviate the thrashing condition. This is because if the system can keep these high-demand pages in physical memory, it will need to page them in and out less frequently, reducing the number of disk I/O operations and improving system performance. The locality model can be applied using techniques such as page replacement algorithms that aim to keep the most frequently accessed pages in physical memory.
Symptoms of Thrashing in OS and How to Detect it?
Thrashing might occur in OS when the system is overloaded with excessive paging, which results in decreased performance of the OS. Here are the different ways to detect Thrashing in OS.
- Maximum cores of CPU being used, and little or no activity is done.
- Thrashing leads to the swapping of pages between the main memory and disk, so disk activity increases rapidly.
- Frequent page faults can also be a reason for thrashing in OS.
Effects on System Performance and User Experience
Thrashing in OS has various impacts on system performance and user experience.
- It decreases the overall performance of the system due to excessive cores of the CPU being used.
- Due to the swapping of pages between memory and disk, the response time for user interaction gets increased, which reduces the user experience.
- Thrashing increases application loading time, which reduces the efficiency of the system.
- Disk activity is increased rapidly, which reduces the performance of systems. Due to slow performance and increased loading time of the system and applications, the user might get frustrated, which depicts a bad user experience.
Thrashing in OS vs. Swapping
Let us note the differences between thrashing and swapping in OS in the table below.
|Here, the CPU spends most of its time swapping pages in and out of the main memory. It results in a decrease in the system's performance.||It is a technique that includes moving an entire process or a part of it from the main memory to the secondary memory and vice versa.|
|It is caused by overcommitment of the physical memory where the system system tries to manage many processes with only limited RAM.||It is done by the OS to manage the memory efficiently and mainly to free up the space in RAM when not required by the process.|
|It degrades the system's performance.||It improves the overall performance of the system.|
|To resolve it, the system can reduce the number of running processes or can increase the physical memory(RAM).||It can be controlled by many memory management techniques to optimize the memory usage.|
|If not taken care of, it can lead to delays and crashes.||It prevents memory exhaustion leading to efficient memory usage.|
Frequently Asked Questions
What is thrashing in operating systems?
Thrashing occurs when page faults and switching occur at a higher frequency, requiring the operating system to spend more time exchanging these pages. Thrashing slows down an operating system as the OS is not doing any productive work during thrashing.
How is thrashing detected?
Because of thrashing, the CPU utilization is going to be reduced or negligible. Thrashing can be detected by comparing the level of CPU use to the level of multiprogramming.
What is an example of thrashing?
A common example of thrashing is when a system with limited RAM runs many memory intensive programs simultaneously. This can lead the system to a state of thrashing because the system constantly swaps data between RAM and the Hard Drive, making the processing slower.
How do you limit the effect of thrashing?
We can use several methods like optimizing the memory usage of programs, using virtual memory, assigning higher priority to critical processes, increasing the RAM of the system, improving the cache mechanism etc. to limit the effect of thrashing.
We investigated the causes of Thrashing in OS (Operating System) and talked about several ways to overcome it. And then, we also conversed about some other techniques to handle thrashing in the operating system. Hope you learned something from this article. But learning should never stop, so to better understand the Operating system, you can go through the other articles on our platform.
Do upvote our blog to help other ninjas grow.