AlgoViz
Operating Systems
Operating Systems · MediumLesson 9 of 12

Virtual memory

Here's a magic trick the operating system pulls on every program: it lets each one believe it owns one big, private, unbroken stretch of memory, all to itself, starting at box 0. But that's an illusion. In reality the one set of memory boxes is shared among dozens of programs, each program's stuff is scattered all over, and some of it might not even be in RAM at all. The OS keeps a secret map that turns each program's pretend addresses into the real ones. This trick is called virtual memory, and it makes computers safe, simple, and roomier than they really are.

Everyone gets apartment #1

What the program seesaddress 0address 1address 2one private block, from 0translateOS mapsecret tableReal RAM (shared)other programour address 2 ➜ hereother programour address 0 ➜ hereparked on diskreal spots are scattered & shared
The program asks for its own address; the OS map secretly translates it to wherever it really sits in shared RAM — possibly scattered, possibly parked on disk.

Imagine a giant apartment building where every tenant is told the exact same thing: 'your apartment is #1, on your own private floor, and the floor next to it is #2, then #3…'. Every single tenant believes they live in #1. How is that not chaos? Because the building manager keeps a secret book that says 'when tenant A says #1, that's really room 504; when tenant B says #1, that's really room 217'. The tenants never know or care where they really are. Virtual memory works exactly like this. Each program is handed its own pretend address space — its own neat row of boxes starting at 0. These are 'virtual' addresses: the program's private make-believe. The OS keeps a secret map from each program's virtual addresses to the real 'physical' locations in actual RAM. When the program reads its address 0, the OS quietly looks it up and fetches the real box — which might be sitting anywhere.

Why pull this trick? Three big wins.

It seems like a lot of effort just to lie to programs. But the illusion buys three huge things. First — SIMPLICITY. Every program can pretend it has the whole machine to itself, one clean block starting at 0. It never has to worry about which other programs are running or where the free space happens to be today. Writing programs gets much easier. Second — PROTECTION. A program can only ever name its OWN virtual addresses. There is literally no virtual address it can say that points into another program's memory — the OS map simply won't translate it that way. So a buggy or nasty program can't peek at your bank app's passwords or stomp on another program's data. The wall between programs is built into the trick. Third — MORE MEMORY THAN YOU HAVE. Because the OS controls the map, it can take parts of memory a program hasn't touched in a while and PARK them on the disk, freeing up real RAM for whoever needs it now. If the program later asks for a parked part, the OS quietly fetches it back. So the computer can act as if it has more memory than the physical RAM actually holds.

Virtual vs. physical, in one line

Keep these two words straight and you've got it. A VIRTUAL address is what the program says — its private, pretend 'box number', from its own world that starts at 0. A PHYSICAL address is where that box REALLY is in the actual RAM chip. The OS map is the translator between them, and every single memory access goes through it. The program speaks virtual; the hardware speaks physical; the OS map is the interpreter sitting in the middle, turning one into the other so fast you never notice. The next topic, paging, is the clever method the OS actually uses to keep that map.

It's a tradeoff

Option👍 Pro👎 Con
Virtual memory (the OS map)Programs are simple (each owns a clean space from 0), protected from each other, and you can pretend to have more memory than you do.Every memory access needs a translation step, and parking parts on disk and fetching them back is slow when it happens.
No virtual memory (programs use real addresses directly)No translation step — a program's address IS the real spot, so access is direct.Programs must know exactly where they sit and dodge each other; any program can read or wreck any other's memory; you can't use more than the physical RAM.

Questions you might have

If every program thinks it starts at address 0, don't they all crash into each other?

No, and that's the whole magic. Each program's '0' is a VIRTUAL address — a private make-believe. The OS map translates one program's 0 to one real spot and another program's 0 to a totally different real spot. They never actually share a box. It's like everyone living in 'apartment #1' on their own private floor: same name, different real room.

Where is this 'map' kept, and isn't looking things up slow?

The map lives in memory, managed by the OS, and the computer has special hardware whose only job is to do these translations crazily fast — so fast you never feel it. (It even keeps a little cheat-sheet of recent lookups to skip the work.) The next topic, paging, shows exactly how the map is organized.

How can the computer 'pretend' to have more memory than it really has?

By parking idle parts on the disk. If a program has a chunk of memory it hasn't touched in a while, the OS can move it out to the (much bigger, but slower) disk and reuse that RAM for something active. If the program asks for the parked chunk later, the OS fetches it back into RAM first. So you can run more than your RAM would normally hold — it's just slower when something has to be un-parked.

Can one program ever read another program's memory, then?

Not by accident, and that's the point of protection. A program can only name its own virtual addresses, and the OS map only ever translates those to that program's own real boxes. There's simply no virtual address a program can say that the map will turn into another program's memory. The wall is built into the system.

🧠Virtual memory gives every program the illusion of its own big, private memory starting at 0, while the OS keeps a secret map to the real, shared, scattered locations. It makes programs simple to write, walled off from each other, and lets the machine act roomier than its RAM really is.
✅ Check yourself3 quick questions — prove the idea stuck.Start →🧭 Take it furtherA new system you haven't read about — would you reach for this idea, and what does it cost?Try it →

Best read after: Memory & addresses