The purpose of Virtual memory is to give the user the illusion the availability of a very large (main) memory for programs and data. The user assumes that the complete secondary storage space (virtual address space) is available for programs and data, although the processor can access only the main-memory space (real or physical address space).
Processor View of Memory
Processor sees physical memory – actual memory modules. The amount of physical memory available is usually less than what all the programs can access. The addresses that the processor uses for physical memory have a one to one mapping to bytes/words in physical memory.
Program View of Memory
The program sees a huge amount of memory – the complete secondary storage space. In reality, most of the memory is not accessed (region between heap and stack).
Memory Management Unit (MMU)
The operating system, maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory. This is done by an Address translation hardware in the CPU, often referred to as a memory management unit (MMU).
The benefits of virtual memory include freeing applications from having to manage a shared memory space, increased security due to memory isolation, and being able to conceptually use more memory than might be physically available, using the technique of paging.
Why Virtual Memory?
Allow efficient and safe sharing of memory among multiple programs. Compile each program into its own address space – a separate range of memory locations accessible only to this program. This translation process enforces protection of a program’s address space from other virtual machines.
Remove the programming burdens of a small, limited amount of main memory. In early days, when programmers developed large programs that did not fit into main-memory space, they would divide the programs into independent partitions known as overlays. These overlays were then brought into the main memory as and when needed for execution. Virtual memory mechanisms handle overlays in an automatic manner transparent to the user.
Simplifies loading the program for execution by providing relocation. Relocation maps the virtual addresses used by a program to different physical addresses before the addresses are used to access memory. This relocation allows us to load the program anywhere in main memory. The operating system need only find a sufficient number of pages in main memory instead of a contiguous block of memory to allocate to a program.
Cache vs Virtual Memory
Replacement on cache misses is primarily controlled by hardware, while virtual memory replacement is primarily controlled by the operating system. The longer miss penalty means it’s more important to make a good decision, so the operating system can be involved and take time deciding what to replace.
The size of the processor address determines the size of virtual memory, but the cache size is independent of the processor address size.
In addition to acting as the lower-level backing store for main memory in the hierarchy, secondary storage is also used for the file system.