seegongsik
Operating systems

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."

01

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.

Real 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.

02

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.

Program A : My memory
0
1
2
3
4
5
A's 0 != B's 0 (different real spots)

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?

03

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.

Real spots
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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?

04

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.

Real memory
P1
P2
P3
P4
Disk
P5
P6

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.

05

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.

Real memory
Disk

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.

In one lineThe operating system gives each process its own "virtual addresses," so each one seems to have a wide memory all to itself. Virtual addresses are quietly moved to real places, and when real memory is full, unused parts are sent out to the slow disk and brought back when needed. Frequently used parts stay in nearby real memory. Thanks to this, each program can use more memory than really exists, separately and quickly.
← Operating systems
Was this helpful? Support seegongsik