1. Home
  2. Explore
  3. Computer Science
  4. Virtual Memory Explained

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Virtual Memory Explained

Speaker 1Speaker 2

Virtual Memory Explained

4:58
0:00-4:58
  1. Corey

    Have you ever opened a browser with 50 tabs, a code editor, and a game running in the background, and wondered how your laptop just handles it? It feels like infinite memory, but you only have a couple of RAM sticks with a strict hardware limit.

  2. Maria

    Right! If you add up what all those apps actually want, they shouldn't fit. So how does the OS take a fixed amount of hardware and make it feel completely limitless?

  3. Corey

    That's the magic of virtual memory. But to understand it, we have to go back to the era of physical addressing. Back then, programs talked directly to the hardware. If code said 'store at address 500,' the CPU literally sent a signal to pin 500 on the memory chip. Fast, but a total disaster.

  4. Maria

    Because if you ran two programs, they'd collide in RAM. Program A writes a value, and then Program B overwrites it. No protection, no walls. One bug in a calculator could crash the entire machine.

  5. Corey

    Exactly. And even if you avoided crashes, there was external fragmentation. Imagine 64 KB of RAM with three programs taking 20 KB each, leaving a 4 KB gap. You close the middle program, freeing 20 KB, but now you have a 20 KB hole and a 4 KB hole. Try loading a 22 KB program — you have 24 KB free, but the largest contiguous block is only 20 KB, so it's rejected. That's fragmentation.

  6. Maria

    So computer scientists realized they needed an illusion — make every program think it owns the entire machine. That led to the base and bounds system.

  7. Corey

    Right. The MMU added a base register and a bound register. If the OS loaded a program at physical address 30,000 with 1,000 bytes, the base was 30,000 and the bound was 1,000. When the program tried to access virtual address 200, the MMU checked if 200 was less than 1,000, then added 30,000 to get physical address 30,200. If it tried address 1,500, the hardware blocked it and raised a segmentation fault. For the first time, memory protection was enforced by hardware.

  8. Maria

    But base and bounds still required programs to be one contiguous block. So fragmentation kept haunting us. The OS had to do compaction — stopping everything, shifting programs together to close gaps. That was painfully slow as programs got bigger.

  9. Corey

    So engineers evolved it into segmentation. Instead of one base and bound, the hardware gave each program several. Code got its own segment, heap got its own, stack got its own. Each segment had its own table entry. That let the OS scatter segments wherever they fit. Plus, we could make the code segment read-only, preventing accidental overwrites. And if you ran 10 instances of the same editor, the OS kept one copy of the code in RAM and shared it.

  10. Maria

    That was a big step forward, but segments were still variable-sized. So external fragmentation didn't disappear. We were still playing Tetris with RAM.

  11. Corey

    Exactly. The solution was paging. Chop all physical RAM into fixed-size 4 KB page frames. Then chop the program's virtual address space into same-sized pages. Now any page can go into any frame — no need for contiguous blocks. Page 1 of your browser could be at the start of RAM, page 2 at the end, page 3 in the middle. External fragmentation was dead forever.

  12. Maria

    But now we need a map to find those scattered pieces. That's the page table. And the problem is, every memory access now requires two trips to RAM: one to check the page table, and one to get the data. That doubles latency.

  13. Corey

    Right, that would be a performance disaster. So the hardware added the TLB — a tiny, lightning-fast cache that stores recent translations. When the CPU requests an address, it checks the TLB first. If it's a hit, translation happens instantly. Over 99% of the time we get a hit because memory accesses cluster. The TLB is the reason paging is practical.

  14. Maria

    Okay, but what about when RAM is full? How does the system make it feel like memory is unlimited?

  15. Corey

    That's demand paging. The OS doesn't load the entire program into RAM — it loads only what's needed to start, and marks the rest as 'not present' in the page table. When a program accesses one of those pages, the CPU triggers a page fault. The OS pauses the program, fetches the missing page from the SSD or hard drive, puts it in a free RAM frame, updates the table, and resumes the program. The program has no idea it was paused.

  16. Maria

    But if RAM is totally full, the OS has to kick something out. How does it decide what to evict?

  17. Corey

    It uses a replacement policy like Least Recently Used — it finds pages that haven't been touched in a while, writes them back to disk if needed, and frees the frame. But there's a breaking point: thrashing. When you have too many demanding apps, the system spends 99% of its time moving pages between RAM and disk, and only 1% actually running code. Your computer slows to a crawl, the cursor stutters, and the hard drive light goes solid. The illusion shatters.

  18. Maria

    So virtual memory is a masterpiece of hardware and software working together — base and bounds, segmentation, paging, TLBs, demand paging. It's elegant, but physics always wins in the end.

  19. Corey

    Exactly. Next time you open a dozen tabs and your laptop doesn't complain, appreciate the invisible war happening under your keyboard. If you want to unmask more magic tricks inside your machine, let us know what to dive into next.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent