What is Virtual memory?
Virtual memory is the ‘memory management’ component of operating system, which creates the illusion to users of a very large (main) memory. Typically when RAM runs low, ‘memory manager’ move some data from RAM to a special location in disk and frees RAM. The freed memory can be used to serve to serve another request.
Virtual address & Physical address:
VM is typically implemented using a combination of hardware and software and it maps memory addresses used by a program a.k.a Virtual addresses into Physical addresses in computer memory (RAM).
Why we need VM?
I see 2 main advantages in using VM:
- It allows users to use more memory for a program than the real memory of program.
- In a multi-programming environment, different programs run in isolation and secured access to only its memory is guaranteed by VM
How VM works?
If your computer runs out of physical memory, it writes some of the unused data to a special location in disk (called as swap space) and frees that memory. When the swapped part is requested again, they are brought back to main memory again from disk (swap space).
Another important aspect is to do with multiprogramming. When process context switch happens, we need to ensure proper usage of memory so that there are no security compromises. To achieve this the physical memory and disk are split into small units of addressable memory called as pages. Pages of data are brought in and out physical memory and every process has its own mapping of the allocated physical page to ensure righteous usage. This mapping is typically called as Page Table.
Virtual Address and Physical Address:
All the addresses used by programmer (which we see in program) are virtual address and all these addresses together form the address space for the program.
An addressable unit in physical memory is called physical address. The page table usually has the mapping of virtual to physical addresses.
Page replacement algorithms:
The logic of which page to be evicted, when there is a need of physical memory page is called Page Replacement Algorithm. There are various algorithms used by OS (LRU, FIFO, OPT). we can discuss them in some other posts.