Appearance
2.5.1 — Virtual Memory & Address Translation
Two copies of the photo-upload service are running on one machine. In each copy, the counter of uploads sits at the very same memory address. Copy 1 sets its counter to 7. Copy 2 sets its counter to 500. Copy 1 reads its counter back, and gets 7.
How? Both copies wrote to the same address. There is only one set of memory chips in the machine. Yet each copy sees only its own value, as if each had a whole computer's memory to itself.
This page has been promised since 2.2.1, which said every process has "its own private memory" and left it at that. The trick that makes it true is virtual memory. It is one of the most important ideas in computing. It is what keeps processes apart, what makes copy-on-write fork work (2.2.2), and what lets a program use more memory than the machine physically has.
1. The world without it
Picture a computer where programs use the real addresses of the memory chips directly. Early computers worked this way, and many tiny devices, such as the controller inside a microwave, still do. Three problems appear at once.
- No protection. If our upload service writes to address 5000, and so does the music player, they overwrite each other. Any program could read any other program's memory, including the browser tab where your bank is open.
- No freedom to place things. A program needs its memory in one continuous block. As programs start and stop, the free memory gets broken into scattered holes. A program that needs 100 MB in one piece might fail even when 200 MB is free in scattered pieces.
- No more memory than the chips hold. If a program needs more memory than the machine has, it simply cannot run.
2. The idea: a translator in front of the memory
Virtual memory solves all three with one idea. Give each process its own private set of addresses, and put a translator between those addresses and the real memory.
- The addresses a program uses are called virtual addresses. Every pointer in your program, every variable's location, is a virtual address.
- The real locations in the memory chips are called physical addresses.
- On every single memory access, the hardware translates the virtual address into a physical one.
The program never sees a physical address. It sees what looks like one huge, continuous, private memory, starting at address 0. Behind the scenes, the translator can put each part of it anywhere in the real memory. It can put the same virtual address of two processes in two different physical places, which is how our two counters stayed apart. It can even say "this part is not in memory at all right now", which section 6 and 2.5.3 use.
Look at the top of the figure. Both copies run the same program, so their code is identical. The translator points both copies' code at the same physical memory, so the code is held in RAM only once. The counters, which differ, go to different places. That is the general rule: the translator lets processes share exactly what should be shared, and keeps apart everything else.
Who does the translating? A piece of hardware inside the CPU called the memory management unit, or MMU. The kernel sets up the rules; the MMU applies them on every access, billions of times a second, far too fast for any software.
3. Pages: translating in blocks, not bytes
Translating each byte separately would need one rule per byte, which is absurd. So memory is translated in fixed-size blocks.
- A block of virtual memory is a page.
- A block of physical memory is a page frame, usually just called a frame.
- Pages and frames are the same size. On almost every PC and phone that is 4 KB: 4,096 bytes.
Translation then works on whole pages: "virtual page 0x12345 lives in physical frame 0x00abc". The bytes inside a page keep their order. If a byte is 1,656 bytes into its page, it is also 1,656 bytes into its frame. That distance from the start of the page is called the offset, and it is copied across unchanged. Only the page number needs translating.
4. A translation, worked by hand
Here is the clever part. Because 4,096 is 2^{12}, read "two to the power twelve", the offset is simply the lowest 12 bits of the address, and the page number is everything above them. No division is needed. The hardware just splits the bits.
Take the virtual address 0x12345678. The 0x means the number is written in hexadecimal, base 16 (1.3), where each hex digit is exactly 4 bits. So the lowest 12 bits are the last three hex digits.
| Part | Hex | Bits |
|---|---|---|
| Virtual address | 0x12345678 | all 32 |
| Page number | 0x12345 | the top 20 |
| Offset | 0x678 | the bottom 12 |
The page number, 0x12345, is looked up in the process's page table, the list of rules that says which frame each page lives in. Suppose the entry says frame 0x00abc. The physical address is that frame's number, with the same offset attached: 0x00abc followed by 678, which is 0x00abc678.
Here is exactly that, in TypeScript, along with the checks the hardware makes on the way.
ts
// translate.ts — turn a virtual address into a physical one, the way the hardware does
const PAGE_SIZE = 4096; // 4 KB = 2^12 bytes
const OFFSET_BITS = 12;
type Entry = { frame: number; present: boolean; writable: boolean };
const pageTable = new Map<number, Entry>([ // (1) one process's page table (tiny part of it)
[0x12345, { frame: 0x00abc, present: true, writable: true }],
[0x12346, { frame: 0x00117, present: true, writable: false }],
[0x12347, { frame: 0, present: false, writable: true }],
]);
function translate(virtualAddress: number, isWrite: boolean): string {
const page = virtualAddress >>> OFFSET_BITS; // (2) high bits: which page
const offset = virtualAddress & (PAGE_SIZE - 1); // (3) low 12 bits: where in the page
const entry = pageTable.get(page);
if (!entry) return "fault: no mapping (a bad pointer)"; // (4)
if (!entry.present) return "fault: page not in RAM yet";
if (isWrite && !entry.writable) return "fault: page is read-only";
const physical = entry.frame * PAGE_SIZE + offset; // (5) frame start + same offset
return "physical 0x" + physical.toString(16).padStart(8, "0");
}
const hex = (n: number) => "0x" + n.toString(16);
for (const [va, w] of [[0x12345678, false], [0x12346010, true], [0x12347000, false], [0x99999000, false]] as const)
console.log(`${hex(va)} ${w ? "write" : "read "} -> ${translate(va, w)}`);
// Prints:
// 0x12345678 read -> physical 0x00abc678
// 0x12346010 write -> fault: page is read-only
// 0x12347000 read -> fault: page not in RAM yet
// 0x99999000 read -> fault: no mapping (a bad pointer)(1)A real page table is a hardware-defined structure, not aMap, but the information is the same: for each page, which frame, and some permission bits. Page 2.5.2 shows the real shape.(2)Shifting right by 12 bits throws the offset away and leaves the page number.>>>is TypeScript's shift that treats the number as unsigned.(3)PAGE_SIZE - 1is 4,095, which in binary is twelve 1s. ANDing with it keeps only the lowest 12 bits: the offset.(4)Each check matches something real hardware does. If any fails, the MMU does not complete the access. It raises a page fault, an exception that hands control to the kernel (2.1.2 listed exceptions as one of the three ways into the kernel).(5)The frame number times 4,096 is where the frame starts in physical memory. Add the offset and you have the byte.
The first line of output is the worked example. The other three are the three kinds of fault, and they are the subject of 2.5.3. Briefly: the second is a write to read-only memory, the third is a page the kernel has chosen not to load yet, and the fourth is an address the process was never given at all.
5. The page table is also a permission system
Each page-table entry holds more than a frame number. It holds permission bits, checked by the MMU on every access. The important ones are:
| Bit | Meaning |
|---|---|
| present | the page is in RAM right now |
| writable | the page may be written, not only read |
| user | ordinary user-mode code may touch it |
| no-execute (NX) | the CPU must never run instructions from it |
| accessed / dirty | set by the hardware when the page is read / written |
These bits turn the translator into a guard.
- Read-only code. The text segment from 2.2.1, which holds a program's instructions, is mapped without the writable bit. A stray write into the program's own code faults instead of silently corrupting it.
- Kernel memory is hidden. The kernel's own memory is mapped into every process, but without the user bit. User-mode code that touches it faults. That is the hardware half of the wall between user mode and kernel mode.
- Data cannot be run as code. Many attacks work by sneaking machine instructions into a program's data, such as a long text field, and then tricking the program into jumping there. Marking the stack and heap no-execute means the CPU refuses to run them. This is one of the main defences in Part 8.
- The hardware keeps notes for the kernel. The accessed and dirty bits tell the kernel which pages have been used recently and which have been changed. 2.5.3 uses them to decide which pages to move out of RAM.
6. Why one idea solves all three problems
Go back to the three problems in section 1.
Protection. Each process has its own page table. A process can only ever produce virtual addresses, and its page table only maps its own pages. There is simply no virtual address it could use that leads to another process's memory. It cannot even name the other process's data. The kernel, which alone may change page tables, decides what each process can reach. That is the real mechanism behind every "processes cannot see each other" sentence in this Part.
Freedom to place things. A program's memory looks continuous in virtual addresses, but its pages can be scattered across any free frames. The kernel never needs one large free hole. Any free 4 KB frame will do for any page.
More memory than the chips hold. A page-table entry can say "not present". The kernel can keep some pages on disk instead of in RAM, and bring each one back only when the program touches it. The program cannot tell, except that the access takes longer. Here is a taste of that, from the upload service.
ts
// demand-paging.ts — memory you are given is not memory you are using
const mb = (n: number) => (n / 2 ** 20).toFixed(0).padStart(5) + " MB";
const rss = () => process.memoryUsage().rss;
console.log("at start: ", mb(rss()));
const big = Buffer.allocUnsafeSlow(1024 * 2 ** 20); // (1) ask for 1 GB, do not touch it
console.log("after asking for 1 GB: ", mb(rss())); // (2)
for (let i = 0; i < big.length / 2; i += 4096) big[i] = 1; // (3) touch one byte in each 4 KB page of the first half
console.log("after touching half: ", mb(rss()));
for (let i = big.length / 2; i < big.length; i += 4096) big[i] = 1;
console.log("after touching all of it:", mb(rss())); // (4)
// Prints:
// at start: 54 MB
// after asking for 1 GB: 54 MB
// after touching half: 567 MB
// after touching all of it: 1079 MB(1)Buffer.allocUnsafeSlowasks for 1 GB without filling it with zeros, so nothing writes to it yet.(2)The process's resident memory, the part actually in RAM, has not changed at all. The kernel gave the program 1 GB of virtual addresses, marked "not present", and no physical memory.(3)Now we write one byte into each 4 KB page of the first half. Each first touch of a page causes a page fault. The kernel finds a free frame, maps it, and lets the write continue. Resident memory grows by about 512 MB.(4)Touching the second half adds the other 512 MB.
So "the program has 1 GB" and "the program is using 1 GB of RAM" are different statements. That gap is the whole subject of 2.5.3.
7. Shuffling the layout: ASLR
One more security feature lives here. If every copy of a program put its stack, heap and libraries at the same virtual addresses every time, an attacker who found a bug could predict exactly where to aim. Address space layout randomisation, or ASLR, makes the kernel place these regions at random virtual addresses each time a program starts. The program works the same, because it only ever uses the addresses it is given. But an attacker can no longer predict them, which turns many reliable attacks into guesses. Every major operating system does this today.
What the interviewer will push on
"What is virtual memory, and what problems does it solve?" They are checking whether you can name all three: protection, flexible placement, and using more memory than the machine has. The tell is explaining why protection follows: a process can only produce virtual addresses, and its page table maps only its own pages, so it cannot even name another process's memory.
"Walk me through translating a virtual address." Split it into page number and offset. With 4 KB pages the offset is the low 12 bits. Look up the page number in the page table to get the frame, then attach the same offset. Be ready to do it with a real hex address, as in section 4.
"What is in a page-table entry besides the frame number?" The permission bits: present, writable, user, no-execute, accessed and dirty. Strong answers connect each to a use, such as read-only code, the hidden kernel, and no-execute stacks against injected code.
"A process allocated 1 GB but only uses 50 MB of RAM. How?" The kernel hands out virtual addresses first and physical frames only when pages are touched. Resident memory counts touched pages.
"Who does the translation, hardware or software?" The MMU in the CPU, on every access. The kernel only sets up the tables. Doing it in software would make every memory access dozens of times slower.
Something to volunteer that nobody asks for: identical, unchanging pages are shared. Ten copies of the upload service share one physical copy of Node's code and of every library they load, because each process's page table points at the same frames. That is why ten copies use far less RAM than ten times one.
Recall
- Virtual memory gives each process its own private virtual addresses, which the memory management unit translates into physical addresses on every access, using that process's page table.
- Memory is translated in pages and page frames of usually 4 KB. The low 12 bits of an address are the offset, copied unchanged; only the page number is translated.
- Each page-table entry carries permission bits: present, writable, user, no-execute, accessed and dirty. They give read-only code, hidden kernel memory and non-executable data.
- It solves three problems: a process cannot even name another's memory, its pages can sit in any free frames, and pages can be absent from RAM until touched. Identical pages, such as program code, are shared.
- Address space layout randomisation places the stack, heap and libraries at random virtual addresses so attackers cannot predict them. A failed check raises a page fault.
Self-test: Why did two copies of the service, writing the same address, not see each other's counter? Translate 0x12345678 with page 0x12345 in frame 0x00abc. Why does a 4 KB page size make the offset exactly the low 12 bits? Name three permission bits and what each protects. Why did asking for 1 GB not raise resident memory?
Quiz Bank
FoundationalWhat is virtual memory and what problems does it solve?
Virtual memory gives each process its own private set of addresses, and the hardware translates each one into a real location in the memory chips on every access. It solves three problems at once:
- Protection. Each process has its own page table, so it cannot even name another process's memory.
- Flexible placement. A process sees one continuous block of addresses, while its pages are scattered across any free frames, so no large free hole is ever needed.
- Capacity. Pages can be absent from RAM and brought in only when touched, so programs can be given more memory than the machine physically has.
The process never sees a physical address and is unaware that any translation happens.
FoundationalWhat are pages, frames and the page table?
Memory is divided into fixed-size blocks, usually 4 KB. A page is a block of virtual memory; a page frame is a block of physical memory of the same size. The page table belongs to one process and says, for each of its pages, which frame holds it, together with permission bits such as present, writable and no-execute. To translate, the hardware splits an address into a page number, looked up in the table to get a frame, and an offset, the position inside the page, which is copied across unchanged.
AppliedTranslate virtual address 0x12345678 if page 0x12345 is in frame 0x00abc, with 4 KB pages.
With 4 KB pages, the offset is the lowest 12 bits, which in hexadecimal is the last three digits: 0x678. The page number is the rest: 0x12345. The page table maps page 0x12345 to frame 0x00abc. The physical address is the frame number followed by the same offset: 0x00abc678. Arithmetically, that is 0x00abc × 4096 + 0x678.
InterviewHow does virtual memory keep one process from reading another's memory?
Every memory access a process makes uses a virtual address, and the hardware translates it through that process's page table. The kernel builds each page table to map only the frames that process is allowed to use. So there is no virtual address the process could produce that would translate to another process's frames; it cannot even name that memory. Only the kernel can change page tables, and switching to another process switches to that process's table. Shared memory exists only where the kernel deliberately maps the same frame into two page tables, such as read-only program code or an explicit shared-memory region.
InterviewA program allocates 1 GB but its resident memory grows only as it uses the memory. Explain.
Allocation gives the program virtual addresses, and the kernel records them in the page table as "not present", with no physical frames behind them. The first time the program touches each page, the hardware raises a page fault; the kernel finds a free frame, maps it, and lets the access continue. Resident memory, the RSS, counts only pages that have frames, so it grows one page at a time as pages are touched. This is demand paging, covered in 2.5.3, and it is why a program's virtual size and its real use of RAM can be very different.
StaffTen copies of a Node service run on one host. Monitoring adds up their resident memory and reports more RAM in use than the host has. How is that possible?
Resident memory counts every page a process has in RAM, including pages shared with other processes. All ten copies map the same physical frames for the Node program's code, its shared libraries and other read-only files, because the kernel points each page table at one copy. Adding the ten RSS figures counts those shared frames ten times.
A fairer measure is the proportional set size, which divides each shared page's size by the number of processes sharing it, or the unique set size, which counts only private pages. On Linux both can be read from each process's memory statistics files. For capacity planning, the right question is how much private memory each copy adds, not each copy's RSS.
Scenario Drill
DrillA colleague is debugging a crash in the photo-upload service and sees that a pointer held the address 0x0000000000000008. They ask why the program crashed instead of reading whatever is at address 8. Explain using this page.
Address 8 is a virtual address. The kernel deliberately leaves the lowest pages of every process's address space unmapped, so the page table has no entry for page 0. When the program reads address 8, the MMU finds no mapping and raises a page fault. The kernel sees that the address belongs to no region the process was ever given, so this is not a page to load but a bug. It sends the process the signal SIGSEGV, which ends it with a segmentation fault (2.5.3 covers this verdict).
Address 8 in particular is the classic sign of a null pointer: the code took a pointer that was null, address 0, and read a field 8 bytes into the object it thought was there. Leaving page 0 unmapped is exactly so that this mistake crashes loudly at once, instead of quietly reading or corrupting real data.
Next: our page table was a Map with three entries. A real 64-bit process has room for billions of pages. Storing one entry for each would take more memory than the machine has, and looking up the table on every access would double the cost of every memory read. Page 2.5.2 shows the real structure, a tree of small tables, and the tiny cache that makes translation almost free.