Virtual Memory in Operating System
Virtual memory is a mechanism used to manage memory using hardware and software. It is a part of the secondary storage that gives the user the illusion that it is a part of the main memory. It helps in running multiple applications with low main memory and increases the degree of multiprogramming in systems. It is commonly implemented using demand paging.
What is Virtual Memory in OS?
Imagine the physical memory of your computer in the front section of your wardrobe. You keep the clothes that you would like to wear in front so that they are handy. Now, in the above scenario, consider yourself to be the operating system and the backside of your wardrobe to be the hard disk. The clothes at the back can also be accessed when needed although they may take a little more time. To make it easy to search, you may keep a note of where you have placed what clothing items. Such a mapping mechanism is called a page table.
Virtual memory is a part of the system's secondary memory that acts and gives us a feel as if it is a part of the main memory. Virtual memory allows a system to execute heavier applications or multiple applications simultaneously without exhausting the RAM (Random Access Memory). In particular, the system can behave as if its total RAM resources were equal to the whole amount of physical RAM plus the complete amount of virtual RAM.
When running multiple heavy applications at once, the system's RAM may get overloaded. To mitigate this issue, some data stored in RAM that isn't being actively used can be temporarily relocated to virtual memory. This frees up RAM space, which may then be utilized to store data that the system will need to access.
A system can continue to run smoothly with significantly less physical RAM than it would normally require. This can be done by exchanging data between RAM and virtual memory based on its need.
History of Virtual Memory in OS
Early computers had two types of storage namely primary and secondary. Back in those times, storage was highly expensive, and thus expanding it would cause trouble. When programs grew and required more memory than there was present, the system ran out of memory. When this happened, they were forced to use the hard drive's memory. However, since access to this was slow, they thought that it would be good to have some kind of fast cheap memory that would help in solving this problem.
The first virtual memory was developed in 1959. It was the criticism that the concept of virtual memory of that time faced that led to building the virtual memory that exists now. By the late 1970s, the idea of virtual memory was properly developed and imbibed into commercial computers.
However, it was not until 1985, that Intel introduced virtual memory, which marked the beginning of virtual memory in personal computers. That's how virtual memory has its place in our daily lives.
How Virtual Memory Works?
Let's understand the working of virtual memory using the example shown below.
Assume that an operating system uses 500 MB of RAM to hold all of the running processes. However, there is now only 10 MB of actual capacity accessible on the RAM. The operating system will then allocate 490 MB of virtual memory and manage it with an application called the Virtual Memory Manager (VMM). As a result, the VMM will generate a 490 MB file on the hard disc to contain the extra RAM that is necessary. The OS will now proceed to address memory, even if only 10 MB of space is available because it considers 500 MB of actual memory saved in RAM. It is the VMM's responsibility to handle 500 MB of memory, even if only 10 MB is available.
Let's consider the wardrobe analogy that we spoke about earlier. Imagine that you are scrolling through an e-commerce platform and like some clothes. However, since you don't have space in your wardrobe, you save it in your wishlist and decide to buy them only when needed. As compared to this process, demand paging is a process that keeps pages of a process that are infrequently used in secondary memory, and pulls them only when required to satisfy the demand.
As a result, when a context switch happens, the OS begins executing the new program after loading the first page and only retrieves the application's referenced pages. If the software addresses a page that is not available in the main memory because it was swapped, the processor deems it as an invalid memory reference.
In the above image, when Program A finishes executing, it swaps out the memory that was in use. Program B then swaps in the memory that was required by it to fulfill the timely demand. This is a typical example of demand paging.
A page frame is used to structure physical memory. A page frame's size is a power of two bytes and varies between platforms.
The CPU accesses the processes through their logical addresses while the main memory recognizes the physical address only. The Memory Management Unit eases this process by converting the page number (logical address) to the frame number (physical address). The offset is the same in both.
The page table maps the page number to its frame number. The image above shows how the frame number and frame offset together help us to access the required word.
A page table is a logical data structure that is used by a virtual memory to record the mapping between virtual and physical addresses. The programs performed by the accessing process use virtual addresses, whereas the hardware, notably the random-access memory (RAM) subsystem, uses physical addresses. The page table is an important part of virtual address translation, which is required to access data in memory.
As shown in the figure above, let us assume that the physical address space, logical address space, and page size are M, L, and P words respectively. Then, the physical address, logical address, and page offset can be given as:
Physical Address = m bits = M Logical Address = l bits = L page offset = p bits = P
We get the frame numbers by subtracting the page offset from the address, i.e. the frame number for physical and logical address will be m-p and l-p respectively.
Since the page table consists of the processes, the number of entries in the page table is equal to the number of pages in the process. Thus, the page table size would be given as bytes.
Swap In and Swap Out
When the primary memory (RAM) is insufficient to store data required by several applications, we use a method known as swap out to transfer certain programs from RAM to the hard drive.
Similarly, when RAM becomes available, we swap in the applications from hard disk to RAM. We may manage many processes inside the same RAM by using swaps. Swapping aids in the creation of virtual memory and is cost-effective.
In the above diagram, process 1 is seen to swap in the applications from hard disk to RAM when they are required, further, when there is a lack of available memory, process 2 swaps out transferring certain programs from RAM to hard disk.
Advantages of Virtual Memory in OS
- Increases the degree of multiprogramming: You can run many applications at once without buying more memory RAMs.
- Data Sharing: Data that is common can be shared between memory.
- Avoids Relocation: The code in the physical memory can be accessed whenever required without relocation of the code.
- Increases memory: Users can run larger applications in systems that cannot support further RAM. Thus, it can run applications that are larger than the physical memory.
- Increases effective use of CPU: Since more processes can be maintained in the memory, the CPU is more effectively used. Each page is stored on the disk while it's in use, and further removed.
Disadvantages of Virtual Memory in OS
- Makes the system slower: Swapping takes time affecting the system's speed and making it slower. This happens as it takes more time to switch between applications.
- Lesser Hard Disk Space: Virtual Memory takes up storage space that could otherwise be used for long-term data storage.
- Reduces System Stability: It negatively affects the overall performance of a system.
- Virtual memory is a part of the system's secondary memory that acts and gives us an illusion of being the main memory. When your computer's physical memory is full, virtual memory is required.
- Although the concept of virtual memories was discovered in 1959, it was not until 1970 that virtual memories were introduced in commercial computers. In 1985, Intel introduced it to the world of personal computers.
- Virtual memory in OS works mainly by transferring processes between the computer's RAM and hard disk depending on the requirements. A Memory Management unit (MMU) facilitates this purpose.
- Page Frames are used to structure physical memory.
- Page tables are used to record the mapping between virtual and physical memory.
- A demand paging mechanism is similar to a paging system with swapping in that processes and pages are loaded only on demand, rather than in preparation.
- When there isn't enough space in the main memory to store the applications, certain applications can be swapped out of the RAM to the hard disk.
- When a certain application needs to be run and is in the hard disk currently, it can be swapped into the RAM.
- It increases the degree of multiprogramming, effective CPU Usage, and effective memory that can be used, and eases data sharing without the need for relocation.
- Virtual Memory can make a system slower, reducing hard disk space and system stability.