🪄 虚拟内存:硬盘冒充内存🪄 Virtual Memory: the Disk Pretending to Be Memory
上一片里,菜谱住进了储物架。可程序越开越多,餐桌不够摆。这片请硬盘来冒充内存。Last leaf, the recipes moved into the storage shelves. But more and more programs open up, and the table cannot hold them all. This leaf asks the disk to pretend to be memory.
第 1 步:每桌都有一张“大桌子”Step 1: Every table gets a "huge table"
桌子不够分,经理画了假桌子。Not enough table to share, so the manager draws pretend tables.
每桌客人都以为:大桌只归自己。桌号都从 0 开始,长得一样。这叫虚拟地址,是画出来的。真正的内存,只有右边那一小排。小电脑抱着页表这本账。虚拟几号对真实几号,全记清。Every table believes the huge table is all its own. The numbers all start from 0 and look alike. These are virtual addresses — drawn on paper. The real memory is only that short row on the right. Little Computer hugs the page table ledger. Which virtual slot maps to which real slot — all written down.
假桌子有了,真桌子仍不够,怎么办?Pretend tables are ready, but the real table is still too small — what now?
第 2 步:最久没动的菜先回后厨Step 2: The longest-untouched dish goes back first
内存就是餐桌,后厨架子是硬盘。桌子满了,就端走最久没动的。这叫换出,腾出位置上新菜。吃到它时再端回来,叫缺页。钟表犯困,蛛网密布,都在说:这盘菜,好久没人动了。Memory is the dining table; the shelf in the kitchen is the disk. When the table is full, the longest-untouched dish is carried away. That is swapping out, making room for new dishes. When someone orders it, it is carried back — a page fault. The sleepy clock and the cobweb both say the same thing: nobody has touched this dish for ages.
端来端去管用,可开多了一样会卡?Carrying dishes back and forth works — so why do many programs still lag?
第 3 步:缺页太多,电脑就卡Step 3: Too many page faults, and the computer lags
这台电脑内存 8 GB,程序要 12 GB。多出的部分,住进交换空间。它是硬盘上的一块备胎分区。去后厨端一趟,慢上千倍。偶尔端一次,几乎没感觉。一直端来端去,蜗牛都超你。This laptop has 8 GB of memory, but the programs want 12 GB. The overflow moves into the swap space — a spare corner of the disk. One trip to the kitchen is a thousand times slower. Once in a while you feel nothing; carried back and forth nonstop, even the snail overtakes you.
🎮 当一回换菜经理(30 秒)🎮 Your turn: be the swap manager (30 seconds)
道理讲完了,换你当经理。餐桌只有 3 个位,10 位客人依次来。满了又缺菜时,挑最久没动的端走。Theory done — now you're the manager. The table has only 3 spots, and 10 guests arrive one by one. When it's full and a dish is missing, send back the one untouched longest.
一句话记住它:虚拟内存 = 硬盘冒充的内存。每道程序都觉得内存很大,不够时,最久没用的先回硬盘。Remember it in one line: virtual memory is the disk pretending to be memory. Every program feels memory is huge; when room runs out, the stalest data goes back to disk first.
人人一张假大桌:虚拟地址从 0 开始,页表对到真实内存A pretend-big table each: virtual addresses start from 0, the page table maps them to real memory桌满就换出:最久没用的去交换空间,用到再缺页端回Full table swaps out: the stalest goes to swap space, paged back when needed后厨太远了:端一趟慢上千倍,缺页太多电脑就卡The kitchen is far: one trip is a thousand times slower, too many page faults and the computer lags
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.