0%

Virtual Memory

Introduction

You open a web browser, a text editor, and a game concurrently. Together, these applications consume 10 GB of RAM.

Since you only have 8 GB of physical RAM, the system cannot keep all the data in RAM at once.
So OS uses a technique to do that:
Use the disk to temporarily store less-used data and free up RAM for more important data. Like 2GB of data is stored in the disk and 8GB of data is stored in the RAM.
When an application needs more RAM to process or generate more data, add new features, or respond to user actions, it can request additional RAM from the operating system. If the physical RAM is insufficient, the OS manages memory by swapping less-used data to disk to free up RAM for the application.

Definition

Virtual memory is a memory management technique that allows an application to use more memory than what is physically available on the system. It does this by using disk space as an extension of RAM.

What to do when the CPU needs to access the less-used data that is not in the memory since you have removed it?
The OS load the corresponding data from disk to the memory

Why this called Virtual Memory?

Virtual Memory is for the Program to say.
The memory space is numbered, that is, each small block of memory has its own address.
There are two types of addresses:Virtual Address and Physical Address
From this 10GB Program’s perspective,it sees a table of numbers that is 10GB in size., And When it needs to access data, it uses virtual addresses within that 10GB space.

Senario

So, when an instruction in your game says to go to address 1000 to get the health-value data of this user, will the CPU go to address 1000 to get it?What if it does go to address 1000,what will it get? No,it will not get the health-value data of this user, So, how does the CPU know where is the right place to get the data? This is where the MMU comes in.

1
MOV REG,1000  this instruction loads a value from the virtual address 1000 into a register

this 1000 is virtual-address.The cpu will parse this instruction
After that the parsing-result will be sent to MMU

MMU

MMU is an address translator
It converts virtual addresses into physical addresses

So after the MMU conversion, the CPU uses this physical address to go to the right place to access the memory (read memory‘s data or write data to the memory).

      +---------------------+
      |         CPU         |
      |                     |
      |   MOV REG, 1000    |
      +----------+----------+
                 |
                 | (虚拟地址: 1000)
                 v
      +---------------------+
      |         MMU         |
      |  (地址转换器)       |
      +----------+----------+
                 |
                 | (物理地址转换)
                 v
      +---------------------+
      |     物理地址        |
      |                     |
      |   (读/写数据)      |
      +----------+----------+

How MMU works

  1. The CPU sends the virtual address to the MMU for translation. “hey give me the actual address of this virtual address”

    The virtual address is divided into page number and offset.

  2. The MMU checks the page table to find the corresponding physical address. How? It looks up the page table to see if the virtual address is mapped to a physical address.

    If the entry exists, the physical address is retrieved.

  3. The MMU sends the physical address to the memory controller.The controller is a part of the CPU that manages memory access.

    The controller accesses the physical memory (RAM) to read or write data.

  4. Page Fault Handling (if needed)

If the virtual address isn’t mapped which means a page fault occurs, the OS handles it by performing the following steps:

  • Check if there are any free frames in memory.
  • If there are free frames, allocate one for the page.
  • If there are no free frames, select a victim page to evict from memory.
  • The OS retrieves the required data from disk storage. Load the required page from disk into the selected frame.
  • Update the page table with the new mapping.
  • Resume the interrupted instruction that caused the page fault.Which is Returns to step 3 to complete the address translation.

page-table

A page table is a data structure used by the MMU to translate virtual addresses to physical addresses.

It contains a list of page table entries (PTEs) that map virtual pages to physical frames. Each PTE includes:

  • 有效/无效位:whether the page is currently loaded in memory
  • 物理地址:指向内存中的物理页框
  • 其他控制位:例如读/写权限、用户/内核权限等
    so on

Informative

  1. 每个进程都可以看到0x1000->但是他们的0x1000经过不同的方式映射到具体的物理内存->映射的方式是MMU使用页表->不同的进程被操作系统维护不同的页表->不同的页表对0x1000的映射规则不同
  2. 每个进程都认为它从0x0000开始,到0x1000、0x2000等等,依次排列着它的代码、数据、堆栈等。每个进程不需要关心其他进程的内存布局,它只会关注自己那一块虚拟地址空间。