Virtual Memory

Virtual memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of main memory.

Loads the program’s code and data on demand:

  • Upon reads, load memory from disk
  • When memory is unused, write it to disk

The fundamental idea...

Not all of the memory used by a process needs to be loaded in main memory. We can lazy load it.

Analogy with Cache

There is a lot of parallels between this and Cache design. However, their differing historical roots have led to the use of different terminology.

At the crux of it, you need to understand Paging and Page Tables.

Considering how much memory modern computers have, is virtual memory really needed?

Yes, because when you open lots of applications (processes), they will each take up RAM, even if they are in the background. That would eat up so much memory without virtual memory. https://community.spiceworks.com/t/virtual-memory-advantages-and-disadvantages/877755

The two comparisons

Real memory: backed by RAM chips. Virtual memory: RAM and swap space.

Two motivations:

  1. Allow efficient and safe sharing of memory among several programs, such as for the memory needed by multiple virtual machines for Cloud computing (main reason today)
  2. Allow a single-user program to exceed the size of primary memory.
    • Basically gives you more memory, or at least some illusion of it

Terminology:

  • page = A virtual memory block
    • So basically instead of calling these Blocks, we call them pages, but they are the same thing
  • page fault = virtual memory miss, i.e. trying to access a page not present in the main memory

With virtual memory, the processor produces a virtual address, which is translated by a combination of hardware and software to a physical address, which in turn can be used to access main memory. This process is called address mapping/translation, where we use a Page Table.

The virtual memory address is broken into two parts:

  1. Virtual Page Number (VPN) Page
  2. Page Offset (PO)

The physical memory address is also broken into two parts:

  1. Physical Page Number (PPN) Frame
  2. Page Offset (PO)

The page offset is determined by the page size - 4KB page size = 4096B = bytes 12 bits for page offset

The illusion of larger amount of virtual memory is created by having a larger number of virtual pages than physical pages.

The Page Table contains the address translations in a virtual memory system.

  • Typically stored in memory
  • Typically indexed by the virtual page number
  • each entry in the table contains the physical page number for that virtual page if the page is currently in memory.

Each program has its own page table.

Policies for Virtual Memory Management:

From ECE222

Calculations

Example 1: Assume a physical address size of 28 bits, virtual address size of 32 bits, and page size is 1KB (or 1024B).

  • page size of , so page offset = 10 bits.
  • VPN = 32 - 10 = 22 bits
  • PPN = 28 - 10 = 18 bits