A note on Thrashing

Anshika Bhargava
3 min readOct 24, 2020

--

Image from pexels.com

So, it happened that one of my friends had asked me to explain thrashing while preparing for interviews, and I could not resist penning it down while explaining it. :p

Here it goes.

According to Wikipedia,

In computer science, thrashing occurs when a computer’s virtual memory resources are overused, leading to a constant state of paging and page faults, inhibiting most application-level processing. This causes the performance of the computer to degrade or collapse.

But why exactly does this happens ? What causes this sudden series of page faults ? Here is a small attempt to explain this intriguing concept of OS.

For this, let us take a step back and touch upon the notion of virtual memory.

We know that for our process to run, we should have it in the memory. Of course, we can put the entire process in the memory(if it fits), but that, as you would have guessed, would not be a wise solution. We have principal of locality in our favour here.

Again quoting our very own Wikipedia,

In computer science, locality of reference, also known as the principle of locality, is the tendency of a processor to access the same set of memory locations repetitively over a short period of time

So, we can infer that at any given time, a process will require only few pages to be in memory for its execution, and these sets of pages will change overtime. It will be these pages that will be present in the main memory, and will be swapped out in case any other page is required. The advantages of this approach are two folds :

  1. The degree of multiprogramming will be higher as we can have some pages of different processes in the memory.
  2. We will be able to execute those processes also whose size is greater than the size of memory.

Now, when the CPU wants to access instruction/data, it starts looking for the page which is supposed to contain that information in the memory. If the CPU finds the page in there, it is considered as a PAGE HIT. In this case, the CPU goes ahead and reads that information from the page. If the CPU does not finds the page in memory, it is termed as a PAGE FAULT, the page would then have to be brought into the memory from HD.

In the case of Page Fault, a page which is brought in from the HD, will replace one of the pages in the memory. This is called Page Replacement. The technique of Local Page Replacement, in which a page of the same process is replaced, is generally followed as it does not disturbs the execution of other processes and also prevents the starvation of lower priority processes.

Let us say that a process requires at least 5 pages to be in memory for its execution, but it has been allocated only 4 frames in the memory. In this scenario, when the CPU tries to access the fifth page which is not present in the memory, it encounters a PAGE FAULT. So, it tries to brings this page in memory, replacing one of the pages. Since that replaced page was also required, CPU again encounters a PAGE FAULT. And this goes on and on and on. And this my friend, is thrashing.

--

--

Anshika Bhargava
Anshika Bhargava

Written by Anshika Bhargava

Software Engineer at Google | I try to learn and blog

Responses (1)