5.4.11Memory Hierarchy & Caches

Virtual memory and paging

2,354 words11 min readdifficulty · medium6 backlinks

WHY does virtual memory exist?

Three problems it solves at once:

  1. Not enough RAM. Programs may need more memory than the physical RAM installed. Virtual memory lets us pretend RAM is as big as the disk by swapping inactive pages to disk.
  2. Multiple programs, one RAM. If program A used physical address 0x4000 directly and program B also wanted 0x4000, they'd collide. Virtual memory gives each process its own address space — isolation & protection.
  3. Fragmentation. Programs need contiguous chunks; RAM gets chopped into gaps over time. Paging removes the "must be contiguous in physical RAM" constraint.

WHAT are the key objects?


HOW does an address get translated? (derive it)

Let page size be P=2pP = 2^p bytes. A virtual address of nn bits splits into two parts.

Why split? Because within a single page, virtual and physical addresses agree exactly — moving a page to a frame keeps the internal layout identical. Only which page/frame differs. So:

  • The low pp bits = the offset inside the page. These are never translated.
  • The high npn - p bits = the Virtual Page Number (VPN). Only this gets mapped.

VA=VPNnp bits    offsetp bits\text{VA} = \underbrace{\text{VPN}}_{n-p \text{ bits}} \;\Vert\; \underbrace{\text{offset}}_{p \text{ bits}}

Deriving the split arithmetically. For a virtual address AA: offset=Amod2p=A&(2p1),VPN=A2p=Ap\text{offset} = A \bmod 2^p = A \,\&\, (2^p - 1), \qquad \text{VPN} = \left\lfloor \frac{A}{2^p} \right\rfloor = A \gg p

Why the mask/shift? Dividing by 2p2^p throws away the low pp bits (giving the page number); the remainder is exactly what's left inside the page.

Then look up the frame: PFN=PageTable[VPN]\text{PFN} = \text{PageTable}[\text{VPN}] PA=(PFN×2p)+offset=(PFNp)offset\text{PA} = (\text{PFN} \times 2^p) + \text{offset} = (\text{PFN} \ll p)\,|\,\text{offset}

Why multiply PFN by 2p2^p? Because frame number ff starts at physical byte fPf \cdot P. Add the untouched offset to land on the exact byte.

Figure — Virtual memory and paging

The TLB — why translation isn't slow

Effective access time (derive it). Let tTLBt_{TLB} = TLB lookup time, tmemt_{mem} = memory access, hit ratio hh. On a hit we pay TLB + memory. On a miss we pay TLB + page-table walk (one memory access, single-level) + the real memory access: EAT=h(tTLB+tmem)+(1h)(tTLB+tmem+tmem)\text{EAT} = h\,(t_{TLB} + t_{mem}) + (1-h)\,(t_{TLB} + t_{mem} + t_{mem}) =tTLB+tmem+(1h)tmem= t_{TLB} + t_{mem} + (1-h)\,t_{mem}

Why the last term? The miss penalty is exactly one extra tmemt_{mem} for the table walk, weighted by miss probability (1h)(1-h).


Worked examples


Common mistakes (steel-manned)


Recall

Recall Active recall — cover the answers
  • What two parts does a virtual address split into, and which one is translated? ⟶ VPN (translated) + offset (untranslated).
  • Formula for number of entries in a single-level page table? ⟶ 2np2^{n-p}.
  • What is a TLB and why does it work? ⟶ Cache of recent translations; works because of locality.
  • Is a page fault an error? ⟶ No — normal event; page is on disk / lazily allocated.
  • Why fixed-size pages? ⟶ Any page fits any frame ⇒ no external fragmentation + simple arithmetic translation.
Recall Feynman: explain to a 12-year-old

Imagine your book has page numbers 1, 2, 3... (that's what you see). But the librarian actually stores those pages in random shelves in the back room. You always ask for "page 5"; the librarian secretly looks at a little notebook that says "page 5 is on shelf 200" and fetches it. You never see the shelves — you just enjoy a neat book. The "little notebook" is the page table, "shelf number" is the physical frame, and if a page is in deep storage (disk), the librarian runs to get it — that little wait is a page fault. Every kid gets their own notebook, so kid A's "page 5" and kid B's "page 5" are totally different shelves.


Connections

  • Memory hierarchy — VM is the level between RAM and disk (RAM caches disk pages).
  • CPU caches — TLB is a specialized cache; VA vs PA affects VIPT/PIPT cache indexing.
  • Page replacement algorithms — LRU/Clock decide which frame to evict on a fault.
  • Multi-level page tables — the fix for gigantic single-level tables.
  • Locality of reference — the reason TLBs and demand paging actually work.
  • Segmentation — the alternative/complement to paging.
What is virtual memory in one line?
An illusion giving each process a large, private, contiguous address space, mapped page-by-page onto physical RAM (and disk).
In a virtual address split, which part is translated?
Only the Virtual Page Number (high bits); the offset (low pp bits) is copied unchanged.
Given page size 2p2^p, how do you extract the offset and VPN from address AA?
offset =Amod2p=A&(2p1)= A \bmod 2^p = A \&(2^p-1); VPN =A/2p=Ap= \lfloor A/2^p\rfloor = A \gg p.
How do you build the physical address from PFN and offset?
PA=(PFNp)offsetPA = (PFN \ll p) \,|\, offset.
How many entries in a single-level page table for an nn-bit VA and 2p2^p-byte pages?
2np2^{n-p}.
What is a page fault?
A trap raised when the referenced page is not currently in RAM; the OS loads it from disk and resumes — a normal event, not a crash.
What is a TLB and why is it fast?
A small associative cache of recent VPN→PFN translations; effective due to locality, giving >99% hit rates so translation is nearly free.
Effective access time with hit ratio hh (single-level table)?
EAT=tTLB+tmem+(1h)tmemEAT = t_{TLB} + t_{mem} + (1-h)\,t_{mem}.
Why fixed-size pages instead of variable regions?
Any page fits into any frame ⇒ no external fragmentation, and translation reduces to a shift+lookup+add.
Downside of very large pages?
Internal fragmentation (wasted RAM) and heavier per-fault disk I/O.
Why doesn't process A's VA 0x4000 clash with process B's?
Each process has its own page table, so identical VAs map to different physical frames (isolation).

Concept Map

solves

splits memory into

maps to

split into

split into

passed unchanged to

looked up in

yields

combined with offset

cached recent maps in

performs

page not in RAM triggers

fetches page from

Virtual Memory Illusion

RAM limits, collisions, fragmentation

Page - virtual block

Frame - physical block

Virtual Address

VPN high bits

Offset low bits

Physical Address

Page Table

Frame Number

TLB

MMU Hardware

Page Fault

Disk

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, virtual memory ka core idea ye hai: har program ko lagta hai ki uske paas ek badi, private memory hai jo address 0 se shuru hoti hai. Lekin asal mein RAM chhoti hoti hai aur usme kayi programs share karte hain. Isliye CPU ke andar MMU chupke se program ke "virtual address" ko "physical address" (asli RAM location) mein translate karta hai. Ye translation page-by-page hoti hai — page ek fixed-size chunk hota hai (aam taur par 4 KiB).

Address ko do hisson mein tod dete hain: upar wale bits = VPN (virtual page number), aur neeche wale p bits = offset. Yaad rakho — VPN translate hota hai, offset waisa ka waisa rehta hai. Kyun? Kyunki page ko frame mein daalna sirf ek shift hai; page ke andar ka layout same rehta hai. Page table ek notebook ki tarah hai jo bolta hai "VPN 0x1A ka frame 0x7 hai". Physical address banane ke liye: frame number ko pp se left shift karo, phir offset OR kar do.

Agar page abhi RAM mein nahi hai (disk pe hai), to page fault hota hai — ye koi error/crash nahi hai, ye bilkul normal cheez hai. OS us page ko disk se laata hai aur instruction dobara chala deta hai. Har process ka apna page table hota hai, isliye process A ka 0x4000 aur process B ka 0x4000 alag-alag RAM location pe jaate hain — yahi isolation hai.

Har baar page table ko memory se padhna slow hoga, isliye TLB naam ka chhota cache hota hai jo recent translations yaad rakhta hai. Locality ki wajah se TLB ka hit rate 99%+ hota hai, to translation almost free ho jaati hai. Exam ke liye ye formulas pakad lo: entries =2np= 2^{n-p}, aur EAT =tTLB+tmem+(1h)tmem= t_{TLB} + t_{mem} + (1-h)t_{mem}. Bas itna samajh liya to virtual memory clear hai.

Go deeper — visual, from zero

Test yourself — Memory Hierarchy & Caches

Connections