To know what is thrashing, you must first be aware of swapping and page fault. So lets start with those concepts:
Page Fault and Swapping :- A page fault occurs when the memory access requested (from the virtual address space) does not map to something that is in RAM. A page must then be sent from RAM to swap, so that the requested new page can be brought from swap to RAM. This results in 2 disk I/Os. Now you might know that disk I/Os are very slow as compared to memory access.
If a process does not have “enough” pages, the page fault rate is very high. This leads to:
– low CPU utilization
– operating system spends most of its time swapping to disk.
THRASHING :- If it happens that your system has to swap pages at such a higher rate that major chunk of CPU time is spent in swapping then this state is known as thrashing. So effectively during thrashing, the CPU spends less time in some actual productive work and more time in swapping.
Memory thrashing is a problem which arises when the memory is located more than the physical memory and it is not available.
Now the effects of thrashing and also the extent to which thrashing occurs will be decided by the type of page replacement policy.
Global Page Replacement: The paging algorithm is applied to all the pages of the memory regardless of which process “owns” them. A page fault in one process may cause a replacement from any process in memory. Thus, the size of a partition may vary randomly.
2. Local Page Replacement: The memory is divided into partitions of a predetermined size for each process and the paging algorithm is applied independently for each region. A process can only use pages in its partition.
What happens after Thrashing starts?
If global page replacement is used, situations worsens very quickly. CPU thinks that CPU utilization is decreasing, so it tries to increase the degree of multiprogramming. Hence bringing more processes inside memory, which in effect increases the thrashing and brings down CPU utilization further down. The CPU notices that utilization is going further down, so it increases the degree of multiprogramming further and the cycle continues.
The solution can be local page replacement where a process can only be allocated pages in its own region in memory. If the swaps of a process increase also, the overall CPU utilization does not decrease much. If other transactions have enough page frames in the partitions they occupy, they will continue to be processed efficiently.
But local page replacement has its own disadvantages which you can now explore further.
This can be illustrated by an example.
Suppose we have a RAM of 2 GB in a system.
Here we are allocating dynamically memory of 1 MB by using malloc function. This loop is continued till 2048 times, i.e each time 1 MB of memory is allocated. But this memory is allocated till the RAM size completes , 2 GB after that if we want more memory then it is virtually allocated (some memory area virtually uses hard-disk) and at this time when we want to access those files or memory the process of page-in and page-out occurs between virtual memory and RAM.This is because all manipulation of files is done in RAM.
So RAM will continuously page-in and page-out to this virtual memory created in hard disk. This will cause memory fault at some time when the resources are not available.
NOTE: in above example if we remove this line
Then the problem of memory thrashing not occurs.
Please note here if we don’t write anything on the memory allocated to the RAM, then the system will allocate memory continously actually the virtual memory, but if we write anything on that memory then upto the physical memory i.e. upto 2gb the memory problem not occurs but after this memory is allocated virtually in hard drive but physically not present then memory faulty or memory thrashing occurs.
This memory is logically created but physically is not present.
i.e. Suppose if you want to start a process of 3 GB and memory is of 2 GB. Here the system will allocate logical memory of 3GB even if it is not available but physical memory is not,it is only 2 GB.
PAGE-IN and PAGE-OUT – other name SWAP-IN or SWAP-OUT. -> RAM works in pages, it will load a page from and in the memory.