欢迎光临
我们一直在努力

什么是虚拟内存?为什么 8GB 内存能跑几十 GB 的程序

这是“操作系统系列教程”的第十五篇。前面几篇我们讲了进程、线程、调度、同步和死锁,这些都是处理器管理的内容。从这一篇开始,我们进入操作系统另一个核心模块——内存管理。而虚拟内存,是内存管理中最重要、也最精彩的概念。

一个反直觉的现象

先来想一个奇怪的问题。

假设你的电脑只有 8GB 物理内存,但你同时打开了浏览器(占用约 2GB)、IDE(占用约 2GB)、音乐播放器(占用约 500MB)、微信(占用约 1GB)、Photoshop(占用约 3GB),再加上操作系统本身占用的 2GB。

加起来已经超过了 8GB。但电脑并没有崩溃,程序依然能正常运行。这是为什么?

再想一个更极端的例子:你在 Linux 服务器上运行一个需要 10GB 内存的程序,但服务器只有 4GB 物理内存。程序依然能跑起来,只是可能慢一点。

这看起来违背常识:物理内存不够,程序为什么还能运行?

答案就是——虚拟内存(Virtual Memory)。

虚拟内存是操作系统最精妙的设计之一。它让每个进程都以为自己拥有独立、连续、足够大的内存空间,而实际上,这些空间可能并不连续,甚至部分数据根本不在物理内存中。

这篇文章,我们就来把“虚拟内存”这件事讲清楚。

说明:本文涉及地址空间、分页、页表、缺页等概念。初学者如果遇到暂时看不懂的术语,可以先跳过细节,重点理解“虚拟内存让每个进程以为自己独占内存,实际上由操作系统在物理内存和磁盘之间调度”这一核心思想。

一、没有虚拟内存会怎样?

1.1 直接使用物理内存的问题

在早期的计算机中,程序直接使用物理内存地址。这种方式存在几个严重问题:

问题一:地址冲突

如果两个程序都使用物理地址 0x1000,就会互相覆盖。程序 A 写入的数据,可能被程序 B 覆盖,导致程序 A 崩溃。

问题二:内存不足

如果物理内存只有 4GB,而程序需要 6GB,程序就无法运行。程序员必须手动管理内存,把数据分成小块,用到时加载,不用时写回磁盘。这非常繁琐,容易出错。

问题三:安全性差

程序可以直接访问任何物理内存地址,包括其他程序的数据和操作系统的数据。一个恶意程序可以轻易读取或修改其他程序的内容。

问题四:编程困难

程序员必须知道数据在物理内存中的具体位置,无法使用统一的地址空间。程序的可移植性极差。

#mermaid-svg-8WKukQbemsgmC0wN{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-8WKukQbemsgmC0wN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8WKukQbemsgmC0wN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8WKukQbemsgmC0wN .error-icon{fill:#552222;}#mermaid-svg-8WKukQbemsgmC0wN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8WKukQbemsgmC0wN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8WKukQbemsgmC0wN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8WKukQbemsgmC0wN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8WKukQbemsgmC0wN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8WKukQbemsgmC0wN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8WKukQbemsgmC0wN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8WKukQbemsgmC0wN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8WKukQbemsgmC0wN .marker.cross{stroke:#333333;}#mermaid-svg-8WKukQbemsgmC0wN svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8WKukQbemsgmC0wN p{margin:0;}#mermaid-svg-8WKukQbemsgmC0wN .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-8WKukQbemsgmC0wN .cluster-label text{fill:#333;}#mermaid-svg-8WKukQbemsgmC0wN .cluster-label span{color:#333;}#mermaid-svg-8WKukQbemsgmC0wN .cluster-label span p{background-color:transparent;}#mermaid-svg-8WKukQbemsgmC0wN .label text,#mermaid-svg-8WKukQbemsgmC0wN span{fill:#333;color:#333;}#mermaid-svg-8WKukQbemsgmC0wN .node rect,#mermaid-svg-8WKukQbemsgmC0wN .node circle,#mermaid-svg-8WKukQbemsgmC0wN .node ellipse,#mermaid-svg-8WKukQbemsgmC0wN .node polygon,#mermaid-svg-8WKukQbemsgmC0wN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8WKukQbemsgmC0wN .rough-node .label text,#mermaid-svg-8WKukQbemsgmC0wN .node .label text,#mermaid-svg-8WKukQbemsgmC0wN .image-shape .label,#mermaid-svg-8WKukQbemsgmC0wN .icon-shape .label{text-anchor:middle;}#mermaid-svg-8WKukQbemsgmC0wN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8WKukQbemsgmC0wN .rough-node .label,#mermaid-svg-8WKukQbemsgmC0wN .node .label,#mermaid-svg-8WKukQbemsgmC0wN .image-shape .label,#mermaid-svg-8WKukQbemsgmC0wN .icon-shape .label{text-align:center;}#mermaid-svg-8WKukQbemsgmC0wN .node.clickable{cursor:pointer;}#mermaid-svg-8WKukQbemsgmC0wN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8WKukQbemsgmC0wN .arrowheadPath{fill:#333333;}#mermaid-svg-8WKukQbemsgmC0wN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8WKukQbemsgmC0wN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8WKukQbemsgmC0wN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8WKukQbemsgmC0wN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8WKukQbemsgmC0wN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8WKukQbemsgmC0wN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8WKukQbemsgmC0wN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8WKukQbemsgmC0wN .cluster text{fill:#333;}#mermaid-svg-8WKukQbemsgmC0wN .cluster span{color:#333;}#mermaid-svg-8WKukQbemsgmC0wN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-8WKukQbemsgmC0wN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8WKukQbemsgmC0wN rect.text{fill:none;stroke-width:0;}#mermaid-svg-8WKukQbemsgmC0wN .icon-shape,#mermaid-svg-8WKukQbemsgmC0wN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8WKukQbemsgmC0wN .icon-shape p,#mermaid-svg-8WKukQbemsgmC0wN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8WKukQbemsgmC0wN .icon-shape .label rect,#mermaid-svg-8WKukQbemsgmC0wN .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8WKukQbemsgmC0wN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8WKukQbemsgmC0wN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8WKukQbemsgmC0wN :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

直接使用物理内存

地址冲突

内存不足

安全性差

编程困难

图 1:直接使用物理内存的四大问题。 地址冲突、内存不足、安全性差、编程困难。

1.2 一个直观的例子

假设物理内存只有 4GB,程序 A 需要 3GB,程序 B 需要 3GB。如果直接使用物理内存,两个程序无法同时运行,因为它们需要 6GB,超过了 4GB。

但如果你观察实际系统,两个 3GB 的程序可以同时运行。这是因为它们并不总是同时使用全部 3GB——程序 A 可能正在等待用户输入,程序 B 可能正在等待网络响应。虚拟内存让操作系统可以在它们之间灵活调度内存。

在这里插入图片描述

图 2:虚拟内存让多个程序共享有限的物理内存。 不活跃的数据被换出到磁盘,需要时再换入。

二、虚拟内存到底是什么?

2.1 从生活场景理解

我们可以用“图书馆”来类比虚拟内存。

图书馆场景操作系统
读者 进程
书架上的书 物理内存
图书馆总藏书 虚拟内存
借书证上的编号 虚拟地址
图书管理员 内存管理单元(MMU)
书库(不对外开放) 磁盘

读者想借书时,只需要知道书的编号(虚拟地址),不需要知道书具体在哪个书架(物理地址)。如果书在书架上(物理内存中),管理员直接拿给读者;如果书在书库里(磁盘上),管理员先去取来,再交给读者。

读者感觉自己可以借到图书馆的所有书(虚拟内存),但实际上书架(物理内存)只能放下一部分。

2.2 虚拟内存的定义

给出一个正式的定义:

虚拟内存(Virtual Memory)是操作系统提供的一种内存管理机制,它为每个进程提供一个独立、连续、远大于物理内存的虚拟地址空间,并通过地址映射和页面置换,在物理内存和磁盘之间动态调度数据。

这个定义里有四个关键点:

  • 独立:每个进程有自己独立的虚拟地址空间,互不干扰;
  • 连续:虚拟地址空间是连续的,程序员不需要关心物理内存的碎片;
  • 远大于物理内存:虚拟地址空间可以比物理内存大得多;
  • 动态调度:操作系统在物理内存和磁盘之间动态换入换出数据。
  • 2.3 虚拟内存的核心思想

    虚拟内存的核心思想可以概括为三句话:

    • 每个进程都以为自己独占内存;
    • 虚拟地址和物理地址分离;
    • 只有真正用到的数据才加载到物理内存。

    #mermaid-svg-MqU7orql8RzJQZPI{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MqU7orql8RzJQZPI .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MqU7orql8RzJQZPI .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MqU7orql8RzJQZPI .error-icon{fill:#552222;}#mermaid-svg-MqU7orql8RzJQZPI .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MqU7orql8RzJQZPI .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MqU7orql8RzJQZPI .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MqU7orql8RzJQZPI .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MqU7orql8RzJQZPI .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MqU7orql8RzJQZPI .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MqU7orql8RzJQZPI .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MqU7orql8RzJQZPI .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MqU7orql8RzJQZPI .marker.cross{stroke:#333333;}#mermaid-svg-MqU7orql8RzJQZPI svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MqU7orql8RzJQZPI p{margin:0;}#mermaid-svg-MqU7orql8RzJQZPI .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-MqU7orql8RzJQZPI .cluster-label text{fill:#333;}#mermaid-svg-MqU7orql8RzJQZPI .cluster-label span{color:#333;}#mermaid-svg-MqU7orql8RzJQZPI .cluster-label span p{background-color:transparent;}#mermaid-svg-MqU7orql8RzJQZPI .label text,#mermaid-svg-MqU7orql8RzJQZPI span{fill:#333;color:#333;}#mermaid-svg-MqU7orql8RzJQZPI .node rect,#mermaid-svg-MqU7orql8RzJQZPI .node circle,#mermaid-svg-MqU7orql8RzJQZPI .node ellipse,#mermaid-svg-MqU7orql8RzJQZPI .node polygon,#mermaid-svg-MqU7orql8RzJQZPI .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MqU7orql8RzJQZPI .rough-node .label text,#mermaid-svg-MqU7orql8RzJQZPI .node .label text,#mermaid-svg-MqU7orql8RzJQZPI .image-shape .label,#mermaid-svg-MqU7orql8RzJQZPI .icon-shape .label{text-anchor:middle;}#mermaid-svg-MqU7orql8RzJQZPI .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MqU7orql8RzJQZPI .rough-node .label,#mermaid-svg-MqU7orql8RzJQZPI .node .label,#mermaid-svg-MqU7orql8RzJQZPI .image-shape .label,#mermaid-svg-MqU7orql8RzJQZPI .icon-shape .label{text-align:center;}#mermaid-svg-MqU7orql8RzJQZPI .node.clickable{cursor:pointer;}#mermaid-svg-MqU7orql8RzJQZPI .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MqU7orql8RzJQZPI .arrowheadPath{fill:#333333;}#mermaid-svg-MqU7orql8RzJQZPI .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MqU7orql8RzJQZPI .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MqU7orql8RzJQZPI .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MqU7orql8RzJQZPI .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MqU7orql8RzJQZPI .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MqU7orql8RzJQZPI .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MqU7orql8RzJQZPI .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MqU7orql8RzJQZPI .cluster text{fill:#333;}#mermaid-svg-MqU7orql8RzJQZPI .cluster span{color:#333;}#mermaid-svg-MqU7orql8RzJQZPI div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MqU7orql8RzJQZPI .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MqU7orql8RzJQZPI rect.text{fill:none;stroke-width:0;}#mermaid-svg-MqU7orql8RzJQZPI .icon-shape,#mermaid-svg-MqU7orql8RzJQZPI .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MqU7orql8RzJQZPI .icon-shape p,#mermaid-svg-MqU7orql8RzJQZPI .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MqU7orql8RzJQZPI .icon-shape .label rect,#mermaid-svg-MqU7orql8RzJQZPI .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MqU7orql8RzJQZPI .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MqU7orql8RzJQZPI .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MqU7orql8RzJQZPI :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    虚拟内存核心思想

    每个进程以为自己独占内存

    虚拟地址与物理地址分离

    按需加载, 局部性原理

    图 3:虚拟内存的核心思想。 独立、分离、按需加载。

    三、虚拟地址与物理地址

    理解虚拟内存,首先要理解两种地址:虚拟地址和物理地址。

    3.1 两种地址的定义

    地址类型定义谁使用
    虚拟地址 程序使用的地址,也叫逻辑地址 程序员、编译器、应用程序
    物理地址 内存单元的实际地址 内存管理单元、硬件

    程序员写代码时,使用的是虚拟地址。CPU 执行指令时,先把虚拟地址转换为物理地址,再访问内存。

    3.2 地址转换过程

    物理内存页表内存管理单元CPU物理内存页表内存管理单元CPU#mermaid-svg-1Zhk7b8vhjtt9jvh{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1Zhk7b8vhjtt9jvh .error-icon{fill:#552222;}#mermaid-svg-1Zhk7b8vhjtt9jvh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1Zhk7b8vhjtt9jvh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1Zhk7b8vhjtt9jvh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1Zhk7b8vhjtt9jvh .marker.cross{stroke:#333333;}#mermaid-svg-1Zhk7b8vhjtt9jvh svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1Zhk7b8vhjtt9jvh p{margin:0;}#mermaid-svg-1Zhk7b8vhjtt9jvh .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1Zhk7b8vhjtt9jvh text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-1Zhk7b8vhjtt9jvh .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-1Zhk7b8vhjtt9jvh .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-1Zhk7b8vhjtt9jvh #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-1Zhk7b8vhjtt9jvh .sequenceNumber{fill:white;}#mermaid-svg-1Zhk7b8vhjtt9jvh #sequencenumber{fill:#333;}#mermaid-svg-1Zhk7b8vhjtt9jvh #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-1Zhk7b8vhjtt9jvh .messageText{fill:#333;stroke:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1Zhk7b8vhjtt9jvh .labelText,#mermaid-svg-1Zhk7b8vhjtt9jvh .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .loopText,#mermaid-svg-1Zhk7b8vhjtt9jvh .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-1Zhk7b8vhjtt9jvh .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-1Zhk7b8vhjtt9jvh .noteText,#mermaid-svg-1Zhk7b8vhjtt9jvh .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-1Zhk7b8vhjtt9jvh .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1Zhk7b8vhjtt9jvh .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1Zhk7b8vhjtt9jvh .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1Zhk7b8vhjtt9jvh .actorPopupMenu{position:absolute;}#mermaid-svg-1Zhk7b8vhjtt9jvh .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-1Zhk7b8vhjtt9jvh .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1Zhk7b8vhjtt9jvh .actor-man circle,#mermaid-svg-1Zhk7b8vhjtt9jvh line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-1Zhk7b8vhjtt9jvh :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}发出虚拟地址查询虚拟地址对应的页表项返回物理页框号拼接物理页框号和页内偏移访问物理地址返回数据

    图 4:地址转换过程。 CPU 发出虚拟地址,MMU 通过页表转换为物理地址。

    3.3 一个具体的例子

    假设虚拟地址是 32 位,页大小为 4KB。那么:

    • 虚拟地址的低 12 位(0~11)是页内偏移;
    • 虚拟地址的高 20 位(12~31)是虚拟页号。

    地址转换过程:

  • 取出虚拟页号(高 20 位);
  • 在页表中查找虚拟页号对应的页表项;
  • 页表项中存储着物理页框号(如 20 位);
  • 把物理页框号和页内偏移拼接,得到物理地址。
  • 例如:

    • 虚拟地址:0x00003A5C;
    • 虚拟页号:0x00003;
    • 页内偏移:0xA5C;
    • 页表项查到物理页框号为 0x00012;
    • 物理地址:0x00012A5C。

    #mermaid-svg-ZURiYql5JZTIapL6{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZURiYql5JZTIapL6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZURiYql5JZTIapL6 .error-icon{fill:#552222;}#mermaid-svg-ZURiYql5JZTIapL6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZURiYql5JZTIapL6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZURiYql5JZTIapL6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZURiYql5JZTIapL6 .marker.cross{stroke:#333333;}#mermaid-svg-ZURiYql5JZTIapL6 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZURiYql5JZTIapL6 p{margin:0;}#mermaid-svg-ZURiYql5JZTIapL6 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZURiYql5JZTIapL6 .cluster-label text{fill:#333;}#mermaid-svg-ZURiYql5JZTIapL6 .cluster-label span{color:#333;}#mermaid-svg-ZURiYql5JZTIapL6 .cluster-label span p{background-color:transparent;}#mermaid-svg-ZURiYql5JZTIapL6 .label text,#mermaid-svg-ZURiYql5JZTIapL6 span{fill:#333;color:#333;}#mermaid-svg-ZURiYql5JZTIapL6 .node rect,#mermaid-svg-ZURiYql5JZTIapL6 .node circle,#mermaid-svg-ZURiYql5JZTIapL6 .node ellipse,#mermaid-svg-ZURiYql5JZTIapL6 .node polygon,#mermaid-svg-ZURiYql5JZTIapL6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZURiYql5JZTIapL6 .rough-node .label text,#mermaid-svg-ZURiYql5JZTIapL6 .node .label text,#mermaid-svg-ZURiYql5JZTIapL6 .image-shape .label,#mermaid-svg-ZURiYql5JZTIapL6 .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZURiYql5JZTIapL6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZURiYql5JZTIapL6 .rough-node .label,#mermaid-svg-ZURiYql5JZTIapL6 .node .label,#mermaid-svg-ZURiYql5JZTIapL6 .image-shape .label,#mermaid-svg-ZURiYql5JZTIapL6 .icon-shape .label{text-align:center;}#mermaid-svg-ZURiYql5JZTIapL6 .node.clickable{cursor:pointer;}#mermaid-svg-ZURiYql5JZTIapL6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZURiYql5JZTIapL6 .arrowheadPath{fill:#333333;}#mermaid-svg-ZURiYql5JZTIapL6 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZURiYql5JZTIapL6 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZURiYql5JZTIapL6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZURiYql5JZTIapL6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZURiYql5JZTIapL6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZURiYql5JZTIapL6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZURiYql5JZTIapL6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZURiYql5JZTIapL6 .cluster text{fill:#333;}#mermaid-svg-ZURiYql5JZTIapL6 .cluster span{color:#333;}#mermaid-svg-ZURiYql5JZTIapL6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZURiYql5JZTIapL6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZURiYql5JZTIapL6 rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZURiYql5JZTIapL6 .icon-shape,#mermaid-svg-ZURiYql5JZTIapL6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZURiYql5JZTIapL6 .icon-shape p,#mermaid-svg-ZURiYql5JZTIapL6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZURiYql5JZTIapL6 .icon-shape .label rect,#mermaid-svg-ZURiYql5JZTIapL6 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZURiYql5JZTIapL6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZURiYql5JZTIapL6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZURiYql5JZTIapL6 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    虚拟地址0x00003A5C

    虚拟页号0x00003

    页内偏移0xA5C

    页表查找

    物理页框号0x00012

    物理地址0x00012A5C

    图 5:虚拟地址到物理地址的转换。 虚拟页号通过页表映射为物理页框号,加上页内偏移得到物理地址。

    四、分页机制

    虚拟内存的实现依赖分页机制。分页把虚拟地址空间和物理内存都划分为固定大小的块,分别称为页和页框。

    4.1 页和页框

    概念位置大小说明
    页(Page) 虚拟地址空间 4KB(常见) 虚拟内存的划分单位
    页框(Page Frame) 物理内存 4KB(常见) 物理内存的划分单位

    页和页框大小相同,这样虚拟页才能映射到物理页框。

    4.2 页表

    页表(Page Table)是存储虚拟页到物理页框映射关系的数据结构。每个进程有自己的页表。

    页表项通常包含:

    字段说明
    物理页框号 对应的物理页框
    有效位 该页是否在物理内存中
    访问位 该页是否被访问过
    修改位 该页是否被修改过
    保护位 读、写、执行权限

    #mermaid-svg-Ab5MygWR6U9pz6e3{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Ab5MygWR6U9pz6e3 .error-icon{fill:#552222;}#mermaid-svg-Ab5MygWR6U9pz6e3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ab5MygWR6U9pz6e3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .marker.cross{stroke:#333333;}#mermaid-svg-Ab5MygWR6U9pz6e3 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ab5MygWR6U9pz6e3 p{margin:0;}#mermaid-svg-Ab5MygWR6U9pz6e3 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster-label text{fill:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster-label span{color:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster-label span p{background-color:transparent;}#mermaid-svg-Ab5MygWR6U9pz6e3 .label text,#mermaid-svg-Ab5MygWR6U9pz6e3 span{fill:#333;color:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .node rect,#mermaid-svg-Ab5MygWR6U9pz6e3 .node circle,#mermaid-svg-Ab5MygWR6U9pz6e3 .node ellipse,#mermaid-svg-Ab5MygWR6U9pz6e3 .node polygon,#mermaid-svg-Ab5MygWR6U9pz6e3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .rough-node .label text,#mermaid-svg-Ab5MygWR6U9pz6e3 .node .label text,#mermaid-svg-Ab5MygWR6U9pz6e3 .image-shape .label,#mermaid-svg-Ab5MygWR6U9pz6e3 .icon-shape .label{text-anchor:middle;}#mermaid-svg-Ab5MygWR6U9pz6e3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .rough-node .label,#mermaid-svg-Ab5MygWR6U9pz6e3 .node .label,#mermaid-svg-Ab5MygWR6U9pz6e3 .image-shape .label,#mermaid-svg-Ab5MygWR6U9pz6e3 .icon-shape .label{text-align:center;}#mermaid-svg-Ab5MygWR6U9pz6e3 .node.clickable{cursor:pointer;}#mermaid-svg-Ab5MygWR6U9pz6e3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .arrowheadPath{fill:#333333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ab5MygWR6U9pz6e3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Ab5MygWR6U9pz6e3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ab5MygWR6U9pz6e3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster text{fill:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 .cluster span{color:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Ab5MygWR6U9pz6e3 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Ab5MygWR6U9pz6e3 rect.text{fill:none;stroke-width:0;}#mermaid-svg-Ab5MygWR6U9pz6e3 .icon-shape,#mermaid-svg-Ab5MygWR6U9pz6e3 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ab5MygWR6U9pz6e3 .icon-shape p,#mermaid-svg-Ab5MygWR6U9pz6e3 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Ab5MygWR6U9pz6e3 .icon-shape .label rect,#mermaid-svg-Ab5MygWR6U9pz6e3 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ab5MygWR6U9pz6e3 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Ab5MygWR6U9pz6e3 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Ab5MygWR6U9pz6e3 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    物理内存

    虚拟地址空间

    虚拟页 0

    虚拟页 1

    虚拟页 2

    虚拟页 3

    页表

    物理页框 5

    物理页框 2

    物理页框 8

    图 6:页表实现虚拟页到物理页框的映射。 多个虚拟页可以映射到不连续的物理页框。

    4.3 多级页表

    如果虚拟地址空间很大(如 64 位系统),页表会非常大。例如,48 位虚拟地址、4KB 页大小,页表有 2^36 个页表项,每个页表项 8 字节,总共需要 512GB 来存储页表。这显然不可行。

    解决方案是多级页表。把页表本身也分页,只加载用到的部分。

    以二级页表为例:

    • 顶级页表(页目录)指向二级页表;
    • 二级页表指向物理页框。

    #mermaid-svg-Q9GxCzrbp2KRDeaB{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Q9GxCzrbp2KRDeaB .error-icon{fill:#552222;}#mermaid-svg-Q9GxCzrbp2KRDeaB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Q9GxCzrbp2KRDeaB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .marker.cross{stroke:#333333;}#mermaid-svg-Q9GxCzrbp2KRDeaB svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Q9GxCzrbp2KRDeaB p{margin:0;}#mermaid-svg-Q9GxCzrbp2KRDeaB .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster-label text{fill:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster-label span{color:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster-label span p{background-color:transparent;}#mermaid-svg-Q9GxCzrbp2KRDeaB .label text,#mermaid-svg-Q9GxCzrbp2KRDeaB span{fill:#333;color:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .node rect,#mermaid-svg-Q9GxCzrbp2KRDeaB .node circle,#mermaid-svg-Q9GxCzrbp2KRDeaB .node ellipse,#mermaid-svg-Q9GxCzrbp2KRDeaB .node polygon,#mermaid-svg-Q9GxCzrbp2KRDeaB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .rough-node .label text,#mermaid-svg-Q9GxCzrbp2KRDeaB .node .label text,#mermaid-svg-Q9GxCzrbp2KRDeaB .image-shape .label,#mermaid-svg-Q9GxCzrbp2KRDeaB .icon-shape .label{text-anchor:middle;}#mermaid-svg-Q9GxCzrbp2KRDeaB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .rough-node .label,#mermaid-svg-Q9GxCzrbp2KRDeaB .node .label,#mermaid-svg-Q9GxCzrbp2KRDeaB .image-shape .label,#mermaid-svg-Q9GxCzrbp2KRDeaB .icon-shape .label{text-align:center;}#mermaid-svg-Q9GxCzrbp2KRDeaB .node.clickable{cursor:pointer;}#mermaid-svg-Q9GxCzrbp2KRDeaB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .arrowheadPath{fill:#333333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Q9GxCzrbp2KRDeaB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Q9GxCzrbp2KRDeaB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Q9GxCzrbp2KRDeaB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster text{fill:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB .cluster span{color:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Q9GxCzrbp2KRDeaB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Q9GxCzrbp2KRDeaB rect.text{fill:none;stroke-width:0;}#mermaid-svg-Q9GxCzrbp2KRDeaB .icon-shape,#mermaid-svg-Q9GxCzrbp2KRDeaB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Q9GxCzrbp2KRDeaB .icon-shape p,#mermaid-svg-Q9GxCzrbp2KRDeaB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Q9GxCzrbp2KRDeaB .icon-shape .label rect,#mermaid-svg-Q9GxCzrbp2KRDeaB .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Q9GxCzrbp2KRDeaB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Q9GxCzrbp2KRDeaB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Q9GxCzrbp2KRDeaB :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    虚拟地址

    页目录索引

    页表索引

    页内偏移

    页目录

    二级页表

    物理页框

    图 7:二级页表结构。 通过两级索引减少页表占用的内存。

    4.4 快表(TLB)

    每次访问内存都要查询页表,会降低性能。为了解决这个问题,CPU 引入了快表(Translation Lookaside Buffer,TLB),也叫页表缓存。

    TLB 是一个小容量的高速缓存,存储最近使用的页表项。CPU 先查 TLB,如果命中,直接得到物理页框号;如果不命中,再查页表。

    #mermaid-svg-7pfPrEXmg8NPfyol{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7pfPrEXmg8NPfyol .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7pfPrEXmg8NPfyol .error-icon{fill:#552222;}#mermaid-svg-7pfPrEXmg8NPfyol .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7pfPrEXmg8NPfyol .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7pfPrEXmg8NPfyol .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7pfPrEXmg8NPfyol .marker.cross{stroke:#333333;}#mermaid-svg-7pfPrEXmg8NPfyol svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7pfPrEXmg8NPfyol p{margin:0;}#mermaid-svg-7pfPrEXmg8NPfyol .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-7pfPrEXmg8NPfyol .cluster-label text{fill:#333;}#mermaid-svg-7pfPrEXmg8NPfyol .cluster-label span{color:#333;}#mermaid-svg-7pfPrEXmg8NPfyol .cluster-label span p{background-color:transparent;}#mermaid-svg-7pfPrEXmg8NPfyol .label text,#mermaid-svg-7pfPrEXmg8NPfyol span{fill:#333;color:#333;}#mermaid-svg-7pfPrEXmg8NPfyol .node rect,#mermaid-svg-7pfPrEXmg8NPfyol .node circle,#mermaid-svg-7pfPrEXmg8NPfyol .node ellipse,#mermaid-svg-7pfPrEXmg8NPfyol .node polygon,#mermaid-svg-7pfPrEXmg8NPfyol .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7pfPrEXmg8NPfyol .rough-node .label text,#mermaid-svg-7pfPrEXmg8NPfyol .node .label text,#mermaid-svg-7pfPrEXmg8NPfyol .image-shape .label,#mermaid-svg-7pfPrEXmg8NPfyol .icon-shape .label{text-anchor:middle;}#mermaid-svg-7pfPrEXmg8NPfyol .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7pfPrEXmg8NPfyol .rough-node .label,#mermaid-svg-7pfPrEXmg8NPfyol .node .label,#mermaid-svg-7pfPrEXmg8NPfyol .image-shape .label,#mermaid-svg-7pfPrEXmg8NPfyol .icon-shape .label{text-align:center;}#mermaid-svg-7pfPrEXmg8NPfyol .node.clickable{cursor:pointer;}#mermaid-svg-7pfPrEXmg8NPfyol .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7pfPrEXmg8NPfyol .arrowheadPath{fill:#333333;}#mermaid-svg-7pfPrEXmg8NPfyol .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7pfPrEXmg8NPfyol .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7pfPrEXmg8NPfyol .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7pfPrEXmg8NPfyol .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7pfPrEXmg8NPfyol .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7pfPrEXmg8NPfyol .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7pfPrEXmg8NPfyol .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7pfPrEXmg8NPfyol .cluster text{fill:#333;}#mermaid-svg-7pfPrEXmg8NPfyol .cluster span{color:#333;}#mermaid-svg-7pfPrEXmg8NPfyol div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7pfPrEXmg8NPfyol .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7pfPrEXmg8NPfyol rect.text{fill:none;stroke-width:0;}#mermaid-svg-7pfPrEXmg8NPfyol .icon-shape,#mermaid-svg-7pfPrEXmg8NPfyol .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7pfPrEXmg8NPfyol .icon-shape p,#mermaid-svg-7pfPrEXmg8NPfyol .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7pfPrEXmg8NPfyol .icon-shape .label rect,#mermaid-svg-7pfPrEXmg8NPfyol .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7pfPrEXmg8NPfyol .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7pfPrEXmg8NPfyol .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7pfPrEXmg8NPfyol :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    命中

    未命中

    CPU 发出虚拟地址

    TLB 是否命中

    直接得到物理页框号

    查询页表

    更新 TLB

    访问物理内存

    图 8:TLB 加速地址转换。 大部分地址转换都能在 TLB 中命中。

    五、缺页中断与页面置换

    虚拟内存的关键机制是:只有真正用到的页才加载到物理内存。当程序访问一个不在物理内存中的页时,就会触发缺页中断(Page Fault)。

    5.1 缺页中断的处理流程

    磁盘内核MMUCPU磁盘内核MMUCPU#mermaid-svg-kBgForRK8HS26Xi1{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-kBgForRK8HS26Xi1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-kBgForRK8HS26Xi1 .error-icon{fill:#552222;}#mermaid-svg-kBgForRK8HS26Xi1 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-kBgForRK8HS26Xi1 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-kBgForRK8HS26Xi1 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-kBgForRK8HS26Xi1 .marker.cross{stroke:#333333;}#mermaid-svg-kBgForRK8HS26Xi1 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-kBgForRK8HS26Xi1 p{margin:0;}#mermaid-svg-kBgForRK8HS26Xi1 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-kBgForRK8HS26Xi1 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-kBgForRK8HS26Xi1 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-kBgForRK8HS26Xi1 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-kBgForRK8HS26Xi1 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-kBgForRK8HS26Xi1 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-kBgForRK8HS26Xi1 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-kBgForRK8HS26Xi1 .sequenceNumber{fill:white;}#mermaid-svg-kBgForRK8HS26Xi1 #sequencenumber{fill:#333;}#mermaid-svg-kBgForRK8HS26Xi1 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-kBgForRK8HS26Xi1 .messageText{fill:#333;stroke:none;}#mermaid-svg-kBgForRK8HS26Xi1 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-kBgForRK8HS26Xi1 .labelText,#mermaid-svg-kBgForRK8HS26Xi1 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-kBgForRK8HS26Xi1 .loopText,#mermaid-svg-kBgForRK8HS26Xi1 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-kBgForRK8HS26Xi1 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-kBgForRK8HS26Xi1 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-kBgForRK8HS26Xi1 .noteText,#mermaid-svg-kBgForRK8HS26Xi1 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-kBgForRK8HS26Xi1 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-kBgForRK8HS26Xi1 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-kBgForRK8HS26Xi1 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-kBgForRK8HS26Xi1 .actorPopupMenu{position:absolute;}#mermaid-svg-kBgForRK8HS26Xi1 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-kBgForRK8HS26Xi1 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-kBgForRK8HS26Xi1 .actor-man circle,#mermaid-svg-kBgForRK8HS26Xi1 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-kBgForRK8HS26Xi1 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}访问虚拟地址查询页表,发现有效位为 0触发缺页中断检查地址是否合法从磁盘读取缺失的页返回页数据把页放入空闲页框更新页表重新执行触发缺页的指令再次访问虚拟地址地址转换成功

    图 9:缺页中断的处理流程。 从磁盘加载缺失的页,更新页表,重新执行指令。

    5.2 页面置换算法

    如果物理内存已满,需要置换(换出)一些页,腾出空间给新页。选择换出哪个页,就是页面置换算法要解决的问题。

    常见的页面置换算法:

    算法原理优缺点
    FIFO 先进先出 简单,但可能置换常用页
    LRU 最近最少使用 效果好,但实现开销大
    Clock 近似 LRU 折中方案,实际常用
    OPT 最优置换 理论最优,无法实现

    #mermaid-svg-sQRlAYuHfEdK1ozQ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sQRlAYuHfEdK1ozQ .error-icon{fill:#552222;}#mermaid-svg-sQRlAYuHfEdK1ozQ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sQRlAYuHfEdK1ozQ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .marker.cross{stroke:#333333;}#mermaid-svg-sQRlAYuHfEdK1ozQ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sQRlAYuHfEdK1ozQ p{margin:0;}#mermaid-svg-sQRlAYuHfEdK1ozQ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster-label text{fill:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster-label span{color:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster-label span p{background-color:transparent;}#mermaid-svg-sQRlAYuHfEdK1ozQ .label text,#mermaid-svg-sQRlAYuHfEdK1ozQ span{fill:#333;color:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .node rect,#mermaid-svg-sQRlAYuHfEdK1ozQ .node circle,#mermaid-svg-sQRlAYuHfEdK1ozQ .node ellipse,#mermaid-svg-sQRlAYuHfEdK1ozQ .node polygon,#mermaid-svg-sQRlAYuHfEdK1ozQ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .rough-node .label text,#mermaid-svg-sQRlAYuHfEdK1ozQ .node .label text,#mermaid-svg-sQRlAYuHfEdK1ozQ .image-shape .label,#mermaid-svg-sQRlAYuHfEdK1ozQ .icon-shape .label{text-anchor:middle;}#mermaid-svg-sQRlAYuHfEdK1ozQ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .rough-node .label,#mermaid-svg-sQRlAYuHfEdK1ozQ .node .label,#mermaid-svg-sQRlAYuHfEdK1ozQ .image-shape .label,#mermaid-svg-sQRlAYuHfEdK1ozQ .icon-shape .label{text-align:center;}#mermaid-svg-sQRlAYuHfEdK1ozQ .node.clickable{cursor:pointer;}#mermaid-svg-sQRlAYuHfEdK1ozQ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .arrowheadPath{fill:#333333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sQRlAYuHfEdK1ozQ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sQRlAYuHfEdK1ozQ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sQRlAYuHfEdK1ozQ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster text{fill:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ .cluster span{color:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-sQRlAYuHfEdK1ozQ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sQRlAYuHfEdK1ozQ rect.text{fill:none;stroke-width:0;}#mermaid-svg-sQRlAYuHfEdK1ozQ .icon-shape,#mermaid-svg-sQRlAYuHfEdK1ozQ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sQRlAYuHfEdK1ozQ .icon-shape p,#mermaid-svg-sQRlAYuHfEdK1ozQ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sQRlAYuHfEdK1ozQ .icon-shape .label rect,#mermaid-svg-sQRlAYuHfEdK1ozQ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sQRlAYuHfEdK1ozQ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sQRlAYuHfEdK1ozQ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sQRlAYuHfEdK1ozQ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    页面置换算法

    FIFO先进先出

    LRU最近最少使用

    Clock时钟算法

    OPT最优置换

    图 10:常见的页面置换算法。

    5.3 LRU 算法示例

    假设物理内存有 3 个页框,访问序列为:1, 2, 3, 4, 1, 2, 5, 1, 2, 3。

    LRU 置换过程:

    访问页框状态缺页
    1 1
    2 1, 2
    3 1, 2, 3
    4 4, 2, 3(换出 1)
    1 4, 1, 3(换出 2)
    2 4, 1, 2(换出 3)
    5 5, 1, 2(换出 4)
    1 5, 1, 2(命中)
    2 5, 1, 2(命中)
    3 5, 3, 2(换出 1)

    总共缺页 8 次。

    #mermaid-svg-3ftDp7NZtc9nOFHf{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3ftDp7NZtc9nOFHf .error-icon{fill:#552222;}#mermaid-svg-3ftDp7NZtc9nOFHf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3ftDp7NZtc9nOFHf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3ftDp7NZtc9nOFHf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3ftDp7NZtc9nOFHf .marker.cross{stroke:#333333;}#mermaid-svg-3ftDp7NZtc9nOFHf svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3ftDp7NZtc9nOFHf p{margin:0;}#mermaid-svg-3ftDp7NZtc9nOFHf .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster-label text{fill:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster-label span{color:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster-label span p{background-color:transparent;}#mermaid-svg-3ftDp7NZtc9nOFHf .label text,#mermaid-svg-3ftDp7NZtc9nOFHf span{fill:#333;color:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf .node rect,#mermaid-svg-3ftDp7NZtc9nOFHf .node circle,#mermaid-svg-3ftDp7NZtc9nOFHf .node ellipse,#mermaid-svg-3ftDp7NZtc9nOFHf .node polygon,#mermaid-svg-3ftDp7NZtc9nOFHf .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3ftDp7NZtc9nOFHf .rough-node .label text,#mermaid-svg-3ftDp7NZtc9nOFHf .node .label text,#mermaid-svg-3ftDp7NZtc9nOFHf .image-shape .label,#mermaid-svg-3ftDp7NZtc9nOFHf .icon-shape .label{text-anchor:middle;}#mermaid-svg-3ftDp7NZtc9nOFHf .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-3ftDp7NZtc9nOFHf .rough-node .label,#mermaid-svg-3ftDp7NZtc9nOFHf .node .label,#mermaid-svg-3ftDp7NZtc9nOFHf .image-shape .label,#mermaid-svg-3ftDp7NZtc9nOFHf .icon-shape .label{text-align:center;}#mermaid-svg-3ftDp7NZtc9nOFHf .node.clickable{cursor:pointer;}#mermaid-svg-3ftDp7NZtc9nOFHf .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-3ftDp7NZtc9nOFHf .arrowheadPath{fill:#333333;}#mermaid-svg-3ftDp7NZtc9nOFHf .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-3ftDp7NZtc9nOFHf .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-3ftDp7NZtc9nOFHf .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3ftDp7NZtc9nOFHf .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-3ftDp7NZtc9nOFHf .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3ftDp7NZtc9nOFHf .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster text{fill:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf .cluster span{color:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-3ftDp7NZtc9nOFHf .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-3ftDp7NZtc9nOFHf rect.text{fill:none;stroke-width:0;}#mermaid-svg-3ftDp7NZtc9nOFHf .icon-shape,#mermaid-svg-3ftDp7NZtc9nOFHf .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3ftDp7NZtc9nOFHf .icon-shape p,#mermaid-svg-3ftDp7NZtc9nOFHf .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-3ftDp7NZtc9nOFHf .icon-shape .label rect,#mermaid-svg-3ftDp7NZtc9nOFHf .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3ftDp7NZtc9nOFHf .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-3ftDp7NZtc9nOFHf .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-3ftDp7NZtc9nOFHf :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    访问序列

    1

    2

    3

    4

    1

    2

    5

    1

    2

    3

    图 11:LRU 页面置换示例。 访问序列和缺页情况。

    六、虚拟内存的优势

    6.1 突破物理内存限制

    虚拟内存让程序可以使用比物理内存更大的地址空间。不活跃的页被换出到磁盘,需要时再换入。这就是为什么 8GB 内存能跑几十 GB 的程序。

    6.2 地址空间隔离

    每个进程有独立的虚拟地址空间,进程之间不能直接访问对方的内存。这提高了系统的安全性。

    6.3 简化编程

    程序员不需要关心物理内存的分配和碎片,只需要使用虚拟地址。编译器、链接器都可以基于虚拟地址工作。

    6.4 内存共享

    多个进程可以共享同一块物理内存,例如共享库。多个进程的虚拟页可以映射到同一个物理页框。

    #mermaid-svg-yLRnLm4k4wgwSfkv{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yLRnLm4k4wgwSfkv .error-icon{fill:#552222;}#mermaid-svg-yLRnLm4k4wgwSfkv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yLRnLm4k4wgwSfkv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yLRnLm4k4wgwSfkv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yLRnLm4k4wgwSfkv .marker.cross{stroke:#333333;}#mermaid-svg-yLRnLm4k4wgwSfkv svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yLRnLm4k4wgwSfkv p{margin:0;}#mermaid-svg-yLRnLm4k4wgwSfkv .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster-label text{fill:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster-label span{color:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster-label span p{background-color:transparent;}#mermaid-svg-yLRnLm4k4wgwSfkv .label text,#mermaid-svg-yLRnLm4k4wgwSfkv span{fill:#333;color:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv .node rect,#mermaid-svg-yLRnLm4k4wgwSfkv .node circle,#mermaid-svg-yLRnLm4k4wgwSfkv .node ellipse,#mermaid-svg-yLRnLm4k4wgwSfkv .node polygon,#mermaid-svg-yLRnLm4k4wgwSfkv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yLRnLm4k4wgwSfkv .rough-node .label text,#mermaid-svg-yLRnLm4k4wgwSfkv .node .label text,#mermaid-svg-yLRnLm4k4wgwSfkv .image-shape .label,#mermaid-svg-yLRnLm4k4wgwSfkv .icon-shape .label{text-anchor:middle;}#mermaid-svg-yLRnLm4k4wgwSfkv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yLRnLm4k4wgwSfkv .rough-node .label,#mermaid-svg-yLRnLm4k4wgwSfkv .node .label,#mermaid-svg-yLRnLm4k4wgwSfkv .image-shape .label,#mermaid-svg-yLRnLm4k4wgwSfkv .icon-shape .label{text-align:center;}#mermaid-svg-yLRnLm4k4wgwSfkv .node.clickable{cursor:pointer;}#mermaid-svg-yLRnLm4k4wgwSfkv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yLRnLm4k4wgwSfkv .arrowheadPath{fill:#333333;}#mermaid-svg-yLRnLm4k4wgwSfkv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yLRnLm4k4wgwSfkv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yLRnLm4k4wgwSfkv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yLRnLm4k4wgwSfkv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yLRnLm4k4wgwSfkv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yLRnLm4k4wgwSfkv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster text{fill:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv .cluster span{color:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yLRnLm4k4wgwSfkv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yLRnLm4k4wgwSfkv rect.text{fill:none;stroke-width:0;}#mermaid-svg-yLRnLm4k4wgwSfkv .icon-shape,#mermaid-svg-yLRnLm4k4wgwSfkv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yLRnLm4k4wgwSfkv .icon-shape p,#mermaid-svg-yLRnLm4k4wgwSfkv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yLRnLm4k4wgwSfkv .icon-shape .label rect,#mermaid-svg-yLRnLm4k4wgwSfkv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yLRnLm4k4wgwSfkv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yLRnLm4k4wgwSfkv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yLRnLm4k4wgwSfkv :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    虚拟内存优势

    突破物理内存限制

    地址空间隔离

    简化编程

    内存共享

    图 12:虚拟内存的四大优势。

    七、虚拟内存的代价

    虚拟内存不是免费的,它也有代价。

    7.1 地址转换开销

    每次访问内存都要进行地址转换。虽然有 TLB 加速,但仍然有开销。

    7.2 缺页中断开销

    缺页中断需要访问磁盘,磁盘速度比内存慢几个数量级。频繁的缺页中断会导致系统性能急剧下降。

    7.3 页表占用内存

    页表本身占用内存。多级页表减少了占用,但仍然需要一定的空间。

    7.4 抖动

    如果物理内存严重不足,系统会频繁换入换出,导致 CPU 大部分时间花在换页上,而不是执行程序。这种现象称为抖动(Thrashing)。

    在这里插入图片描述

    图 13:虚拟内存的代价。 抖动是最严重的性能问题。

    八、虚拟内存的实际应用

    8.1 Linux 中的虚拟内存

    Linux 使用分页机制实现虚拟内存。每个进程有独立的页表,内核维护物理页框的分配和回收。

    常用命令:

    # 查看内存使用情况
    free -h

    # 查看虚拟内存统计
    vmstat

    # 查看进程内存映射
    pmap PID

    8.2 Windows 中的虚拟内存

    Windows 使用页面文件(Page File)实现虚拟内存。页面文件通常位于 C:\\pagefile.sys,大小可以配置。

    8.3 交换空间

    Linux 使用交换空间(Swap)作为虚拟内存的磁盘部分。交换空间可以是分区,也可以是文件。

    # 查看交换空间
    swapon –show

    # 创建交换文件
    fallocate -l 2G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile

    #mermaid-svg-rRiPzDxzJekeQCcz{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rRiPzDxzJekeQCcz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rRiPzDxzJekeQCcz .error-icon{fill:#552222;}#mermaid-svg-rRiPzDxzJekeQCcz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rRiPzDxzJekeQCcz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rRiPzDxzJekeQCcz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rRiPzDxzJekeQCcz .marker.cross{stroke:#333333;}#mermaid-svg-rRiPzDxzJekeQCcz svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rRiPzDxzJekeQCcz p{margin:0;}#mermaid-svg-rRiPzDxzJekeQCcz .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-rRiPzDxzJekeQCcz .cluster-label text{fill:#333;}#mermaid-svg-rRiPzDxzJekeQCcz .cluster-label span{color:#333;}#mermaid-svg-rRiPzDxzJekeQCcz .cluster-label span p{background-color:transparent;}#mermaid-svg-rRiPzDxzJekeQCcz .label text,#mermaid-svg-rRiPzDxzJekeQCcz span{fill:#333;color:#333;}#mermaid-svg-rRiPzDxzJekeQCcz .node rect,#mermaid-svg-rRiPzDxzJekeQCcz .node circle,#mermaid-svg-rRiPzDxzJekeQCcz .node ellipse,#mermaid-svg-rRiPzDxzJekeQCcz .node polygon,#mermaid-svg-rRiPzDxzJekeQCcz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-rRiPzDxzJekeQCcz .rough-node .label text,#mermaid-svg-rRiPzDxzJekeQCcz .node .label text,#mermaid-svg-rRiPzDxzJekeQCcz .image-shape .label,#mermaid-svg-rRiPzDxzJekeQCcz .icon-shape .label{text-anchor:middle;}#mermaid-svg-rRiPzDxzJekeQCcz .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-rRiPzDxzJekeQCcz .rough-node .label,#mermaid-svg-rRiPzDxzJekeQCcz .node .label,#mermaid-svg-rRiPzDxzJekeQCcz .image-shape .label,#mermaid-svg-rRiPzDxzJekeQCcz .icon-shape .label{text-align:center;}#mermaid-svg-rRiPzDxzJekeQCcz .node.clickable{cursor:pointer;}#mermaid-svg-rRiPzDxzJekeQCcz .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-rRiPzDxzJekeQCcz .arrowheadPath{fill:#333333;}#mermaid-svg-rRiPzDxzJekeQCcz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-rRiPzDxzJekeQCcz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-rRiPzDxzJekeQCcz .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rRiPzDxzJekeQCcz .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-rRiPzDxzJekeQCcz .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rRiPzDxzJekeQCcz .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-rRiPzDxzJekeQCcz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-rRiPzDxzJekeQCcz .cluster text{fill:#333;}#mermaid-svg-rRiPzDxzJekeQCcz .cluster span{color:#333;}#mermaid-svg-rRiPzDxzJekeQCcz div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-rRiPzDxzJekeQCcz .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-rRiPzDxzJekeQCcz rect.text{fill:none;stroke-width:0;}#mermaid-svg-rRiPzDxzJekeQCcz .icon-shape,#mermaid-svg-rRiPzDxzJekeQCcz .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rRiPzDxzJekeQCcz .icon-shape p,#mermaid-svg-rRiPzDxzJekeQCcz .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-rRiPzDxzJekeQCcz .icon-shape .label rect,#mermaid-svg-rRiPzDxzJekeQCcz .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rRiPzDxzJekeQCcz .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-rRiPzDxzJekeQCcz .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-rRiPzDxzJekeQCcz :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    虚拟内存实现

    Linux: 分页 + Swap

    Windows: 分页 + 页面文件

    macOS: 分页 + 压缩内存

    图 14:不同操作系统的虚拟内存实现。

    九、常见误区澄清

    误区真相
    “虚拟内存就是磁盘上的交换文件” 虚拟内存是地址空间抽象,交换文件只是它的磁盘部分
    “虚拟内存越大越好” 交换空间过大会导致频繁换页,性能下降
    “有了虚拟内存就不需要物理内存” 物理内存仍然是性能的关键,虚拟内存只是补充
    “虚拟地址就是物理地址” 两者完全不同,通过页表映射
    “页越大越好” 页越大,内部碎片越多;页越小,页表越大
    “缺页中断就是错误” 缺页中断是正常机制,不是错误
    “虚拟内存只用于内存不足时” 虚拟内存始终在工作,即使内存充足
    赞(0)
    未经允许不得转载:171主机测评 » 什么是虚拟内存?为什么 8GB 内存能跑几十 GB 的程序
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址