How does virtual memory work
In lesson 2 we saw that several processes run at the same time. Each of those programs needs a place to remember things: memory. But real memory (RAM) comes in a fixed amount, so if everyone grabbed real space directly, it would quickly run out and they'd bump into each other. That's why the operating system gives each one a "fake, big memory."
Everyone wants memory
Every program running at the same time needs a place to remember things. But real memory is a limited, small space.
If everyone tried to take real space directly, it would soon run short and their areas would collide.
It's a squabble over memory.
Add more programs and the real memory bar quickly fills up and overflows.
Real memory alone can't handle it all. Yet you can't tell programs to "give up some memory," because each needs plenty to work with.
Here the operating system pulls a clever trick.
Giving each its own big memory
The operating system gives each program its own fake memory: wide, neatly starting from slot 0, and seemingly used by that program alone.
A's "slot 0" and B's "slot 0" are actually different places. Each sees only its own memory and can't even see anyone else's.
This private, fake memory is called virtual memory.
Look at A and B in turn: each has its own "my memory" starting from slot 0.
Both start neatly from slot 0, which is very convenient. But a fake address isn't a real place.
When a program says "my slot 3," who turns that into a real place, and how?
Moving fake addresses to real places
Every time a program uses "my slot 3," the operating system and hardware quietly turn it into a real place.
A small map connects them, like "my 3 → real 27." A's "my 0" is real slot 5, and B's "my 0" is real slot 40.
Even with the same fake number, the real places differ, so nothing collides.
Press a fake address, and it leads through the map to a real place. A and B reach different places even with the same number.
Because the map turns fake into real, each lives safely apart.
But add up everyone's fake memory, and it can be far larger than real memory. How can all that fit into small real memory?
Bigger than real: send what's unused to the disk
Here's the operating system's secret. It temporarily moves the parts not in use right now out to the slow disk, and loads the parts needed now into that space.
Later, if something it moved out is needed again, it brings it back.
That's how a program can use more memory than the real memory holds.
Load a new part: when real memory is full, something unused goes out to the disk and what's needed comes in.
Moving things out and bringing them back makes a memory bigger than the real one possible. But the disk is much slower than real memory.
Going all the way to the disk every time would be frustrating, so the operating system takes care of one last thing.
Keeping what's used often close by
The operating system keeps the frequently used parts in nearby real memory and sends only what's used once in a while to the far, slow disk.
It's the same idea as the cache we met in computer structure: "keep what you use often close by."
So most work finishes right away in fast real memory, and trips to the disk are only occasional, so you barely notice the slowness.
The more often you take things out, the more the frequently used ones stay in real memory (close) and the rarely used ones on the disk (far).
To sum up, virtual memory is the skill of moving fake addresses to real places, sending what's unused to the disk, and keeping what's used often close by. Thanks to it, each program feels as if it alone has a memory bigger than the real one.
But the fact that each sees only "its own space" and can't see anyone else's actually becomes the wall that separates and protects mine from yours. That's the next story.