Good reference: https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/9_VirtualMemory.html

For understanding virtual memory, start with this: every address generated by a user program is a virtual address. The OS is just providing an illusion to each process, specifically that it has its own large and private memory; with some hardware help, the OS will turn these pretend virtual addresses into real physical addresses, and thus be able to locate the desired information.

From 3 easy pieces of Operating system

every address from a user program is virtual

Why do we need that?

Mostly ease of use: the OS will give each program the view that it has a large contiguous address space to put its code and data into; thus, as a programmer, you never have to worry about things like “where should I store this variable?” because the virtual address space of the program is large and has lots of room for that sort of thing.

image

image

Without virtual memory, a program couldn’t run if it was larger than the available physical RAM. Virtual memory solves this by loading only the necessary parts of the program into RAM, keeping the rest on the hard drive. When a different part of the program is needed, the operating system swaps it into RAM, moving “older,” unused parts out to the disk.

Figure shows the general layout of virtual memory, which can be much larger than physical memory: image

Virtual address space, which is the programmers logical view of process memory storage. The actual physical layout is controlled by the process’s page table.

image

Example C Program and address space image

Virtual memory also allows the sharing of files and memory by multiple processes, with several benefits:

image

Demand paging

image

The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at once.

Rather they are swapped in only when the process needs them. (on demand) This is termed a lazy swapper, although a pager is a more accurate term.