Virtual memory in operating systems

Virtual memory

What is virtual memory?

Memory is integral part of any execution of process. It is the place where process reads and writes data to process on. An executable instruction needs to in memory for before processor can execute it. What happens when available memory is less than that is required by process?
That’s where virtual memory comes into picture. It is a technique which is implemented by operating system to meet process memory requirements even when there is not enough physical memory available. This memory which is not real physical memory is called as virtual memory. So it is nothing but extension of real memory with the help of disk space.
Virtual memory brings in another concept called logical address space. The address which are directly mapped to physical memory are called as physical addresses. Other type of address which is actually visible to process and used by it to execute its instructions, is called as logical address space. Size of this space vary from architecture to architecture. This is not the actual physical memory allocated to the process. So, the address space which a process sees is not actual physical address space available. Separation of logical space from physical address space removes the dependency of program size on the available physical memory. Program size can be much bigger than memory available because it is not necessary to bring in entire process in memory at the same time. There are various sections in process like error handling/ exception which are rarely executed. No point in bringing all those in memory. Operating system can bring those in as and when they are required. How do these two spaces map?  To map these two, operating system implements memory map function which does the dirty work of mapping process’s logical address to physical address on to memory. 
One question still remains: What happens to extra memory which is required by process and is not available now? For example if memory required by process is 150 MB and only 80 MB is available in memory, there is 70 MB short. How do we manage that? Operating systems rely on the fact that not all instructions of process are required to be in memory at the same time in order to execute it. So, only instruction which are needed are brought into memory and rest 70 MB are allocated on disk and managed by a process called as Virtual Memory Manager (VMM). Instructions are swapped in and out when there is a need of an instruction which is on disk and not in main memory.

There are two methods in which virtual memory concept can be implemented :
1. Demand paging (showed below)
2. Segmentation (will be discussed later)

All in all, below picture explain overall virtual memory concept.
Virtual memory
Virtual memory overview
Virtual memory allows process to use much more memory than what is really available. It makes process which are very big in size (more than memory size) to be able to execute in memory. Other advantage it brings is ability to run multiple process at a time in main memory which is called as multiprocessing.
To go in details of concept, let’s understand how physical memory is organized. Physical memory is divided into chunks of a predefined size usually 4 KB. These chunks are called as pages. When a logical address is accessed by process, it physical address is found by memory map and the page in which that memory address falls is accessed. What if that address is not available in memory? In that case, operating system needs to bring that page into main memory from the disk. Another question? What if there is no space available in main memory to accommodate that new page? Then operating system which decide which page needs to be taken out from memory to make place for new page. Page replacement algorithms is another study all together. I have discussed one Least Recently Used page replacement algorithm in this post: Least Recently Used cache. Point when a page is not found in memory and needs to be brought in from disk is said to be page fault. Page fault has it’s own overhead and hit on performance. We will talk of principle of locality in later posts. 
Below figure explain the basics of paging

Paging overview
Paging overview

Once desired page is loaded into main memory, process resumes it execution. 

This swapping of pages between hard disk and main memory presents another problem called as thrashing. Thrashing is an occurrence when every instruction or most of them lead to swapping of pages from main memory to disk and vice-versa. This has tremendous impact on performance.

From the above explanation it is clear that in order to implement virtual memory, we need hardware support for paging or segmentation and operating system capable of moving pages in and out of main memory. Effective time to access is defined as
ETA = (1-p) * memory access time +  p * {swap in time + swap out time + restart overhead } 

This article explain basic concept of virtual memory. In next post we will discuss how page address translation works in order to realize it.  Please leave comment if you think there is something I missed.