Introduction
Sometimes you might open too many chrome tabs in a system that consumes a lot of RAM which eventually leads to slowing down the performance of the Operating system. Although, it will not crash the whole OS. The reason behind it is that whenever the OS RAM gets full, it transfers some data into secondary memory, which helps the OS prevent a sudden crash. Virtual Memory is a storage allocation scheme that allows secondary memory to be addressed as if it were the main memory. Yet, this process will make the system slow so to avoid this situation it is recommended to upgrade RAM as well as Solid-state drive as it performs better than Hard drives.
Source:memegenerator
In this article, we will learn more about virtual memory and its uses.
Also See, FCFS Scheduling Algorithm Multiprogramming vs Multitasking
Virtual Memory
Virtual memory enables a computer to compensate for physical memory shortages by temporarily transferring data from random access memory (RAM) to disc storage. Mapping memory chunks to disc files allows a computer to treat secondary memory as if it were the main memory.
Source: wikipedia
The main objective of virtual memory is to increase the storage capacity of primary memory. Most PCs nowadays have at least 8 GB (gigabytes) of RAM. However, this is not always enough to run multiple programs at the same time. This is where virtual memory enters the picture. Virtual memory frees up RAM by transferring data that hasn't been used in a while to a storage device like a hard drive or solid-state drive (SSD).
Virtual memory was created at a time when physical memory, also known as RAM, was prohibitively expensive. Since computers have a limited amount of RAM, memory will eventually run out if multiple programs are running at the same time. A virtual memory system emulates RAM by using a section of the hard drive. A system can use virtual memory to load larger or multiple programs at the same time, allowing each one to operate as if it had more space without having to buy more RAM.
How Virtual Memory works?
Most computers today have like 4 to 8 GB of RAM available. Although that amount of RAM is enough to do daily tasks a user expects, if a user opens up a lot of programs at the same time then even that amount of RAM is insufficient to run all of those programs at the same time.
For example, if a user opens up a number of programs more than the available amount of RAM can handle. At that time, If there was no such thing as virtual memory, your computer would have to notify you, "I'm out of RAM." "You are unable to load any additional applications. Please abandon another application before loading a new one."
This undoubtedly will irritate you.
Source: WDRfree
With virtual memory, the computer can scan RAM for areas that haven't been used in a while and copy them to the hard disk. This frees up RAM for the new application to load. 😍
Since this copying occurs automatically, you are unaware of it, and it gives your computer the illusion of having unlimited RAM space despite the fact that it only has a limited amount of RAM installed. It also has a nice economic benefit because hard disc space is much cheaper than RAM chips.
The read/write speed of HDD is much slower than RAM, and the technology of a hard drive is not designed to access small chunks of data at a time. You will notice a significant performance drop if your system is forced to rely too heavily on virtual memory. The key is to have enough RAM to handle everything you tend to work on at the same time; then, the only time you "feel" the slowness of virtual memory is when you switch tasks. When this is the case, virtual memory is ideal.
The area of the hard disk that stores the RAM picture is known as a page file. It holds RAM pages on the hard disk, and the OS moves data back and forth between the RAM and the page file.
You can also read about the Multilevel Feedback Queue Scheduling and Open Source Operating System.
Demand Paging
Virtual memory can be implemented by Demand Paging or Demand Segmentation. Demand paging works on the principle that when a process is swapped in, its pages are not swapped in all at once. Instead, they are only used when the process requires them (On-demand) or a page fault occurs.
In the page table, pages are not moved into memory but are designated as invalid. The rest of the table is empty when an entry is invalid. Pages loaded into memory are marked as valid, along with information about where the swapped-out page can be found.
Suppose a program addresses a page not present in the main memory because it was swapped out recently. In that case, the processor recognizes the invalid memory reference as a page fault and switches control from the program to the operating system, which requests the page back into memory.
Advantages
- Since only a portion of each process's page is loaded into memory, there is room for additional processes. This increases processor utilization because more processes can be in the ready state at the same time
- Because each process uses less primary memory, multiprogramming levels can be increased.
- Demand paging enables the execution of a process that consumes more memory than the main memory. The operating system loads the required pages of a process into the main memory.
Disadvantages
- Compared to simple paged management solutions, the number of tables and the amount of processing overhead for managing page interrupts are higher.
Page Replacement
Only a few pages of a process are loaded into memory at first, as discussed in Demand paging. This allows us to store more processes in memory at once. What happens, though, when a process wants more pages and there isn't enough free RAM to accommodate them?
To address this issue, take some steps:
- Place the process in the wait queue until any other process has completed its execution, freeing up frames.
- Alternatively, to release frames, eliminate some other process from memory.
- Alternatively, discover any pages that aren't being used at the moment and move them to the disc to free up frames. This is known as Page replacement, and it is the most widely used strategy.
Thrashing
Thrashing refers to a process that spends more time paging than executing. To put it another way, the process doesn't have enough frames to hold all of the pages it needs to run, so it swaps pages in and out often to keep running. Occasionally, pages that will be needed shortly must be swapped out.
When the CPU utilization is low, the process scheduling mechanism loads numerous processes into memory simultaneously, allocating a limited number of frames to each process to improve the degree of multiprogramming. As the memory fills up, the process begins to take a long time to swap in the required pages, resulting in low CPU utilization because most processes are waiting for pages. As a result, the long-term scheduler loads more processes to increase CPU use; as a result, the entire system eventually comes to a halt.
Causes of Thrashing
- High degree of multiprogramming:
If the number of processes in memory continues to grow, the number of frames assigned to each process will shrink. As a result, each process will have fewer frames available. As a result, page faults will become more frequent, more CPU time will be wasted switching in and out of pages, and utilization will continue to decline.
2. Lacks of Frames:
When a process has fewer frames, fewer pages can be stored in memory, necessitating more frequent shifting in and out. This could result in thrashing. As a result, each process must be given a sufficient number of frames to avoid thrashing.
Recovery for Trashing
- You can prevent the system from thrashing by ordering the long-term scheduler not to put the processes into memory after the threshold.
- There are several techniques by which the thrashing can be avoided like working-set model, page fault frequency.