All episodes · From One Switch to a CPU · Episode 15

Virtual Memory: Pages, Page Tables, the TLB and Page Faults

Pressing play loads the video from YouTube.

Two programs both use address 1000, and each one finds its own number there. How?
Every program gets virtual addresses. We cut memory into pages, translate address 8200 through a page table (page 2, offset 8, frame 5: address 20,488), see how this keeps programs apart, speed it up with the TLB, and find out what happens on a page fault.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Two programs are running at the same time. Both store a number at address one thousand. One stores a seven. The other stores a forty-two. Neither one overwrites the other. Same address, different numbers. Today: how every program gets its own private memory.

The trick: the addresses a program uses aren't real. They're virtual addresses. Every program sees its own memory, starting at zero, as if it had the whole machine to itself. On every single access, the processor quietly translates the virtual address into a physical one: a real place in the memory chips.

Translating every byte separately would need an enormous table. So memory is cut into pages, commonly four kilobytes each. A virtual address splits into two parts: a page number, and an offset inside that page. Take address eight thousand two hundred. Pages are four thousand ninety-six bytes, so that's page two, eight bytes in. Each program has a page table, kept by the operating system. It says: your page two is really in frame five. So the real address is frame five, eight bytes in. The program never knows.

This also keeps programs apart. A page that isn't in your table simply doesn't exist for you. Try to touch it, and the processor stops you and calls the operating system. One program can't read or wreck another's memory.

But there's a cost. The page table lives in memory, so every access would need an extra trip to memory first. So the processor keeps a tiny cache of recent translations, called the TLB. Most of the time, the translation is already there.

And what if a page isn't in memory at all? Then the access causes a page fault. The operating system finds the page on the disk, loads it into a free frame, updates the table, and lets the program carry on as if nothing happened. Disks are thousands of times slower than memory, so page faults are expensive. But they let programs use more memory than the machine really has.

So: programs use virtual addresses, page tables map them to real frames, the TLB makes it fast, and page faults fetch what's missing from the disk. Next time: what's inside that disk. Spinning platters, flash chips, and the whole memory hierarchy. See you there.

Next episode: where your files live when the power goes off. Hard disks and SSDs.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal