RE: VM Vs Swap Space

From: Robert White
Date: Wed Oct 13 2004 - 17:50:29 EST


A long but basic explanation of the difference between "Virtual Memory" and "Swap
Space" follows:

-----Original Message-----
From: linux-kernel-owner@xxxxxxxxxxxxxxx [mailto:linux-kernel-owner@xxxxxxxxxxxxxxx]
On Behalf Of Ankit Jain
Sent: Thursday, October 07, 2004 2:18 AM
To: linux
Subject: VM Vs Swap Space

> (1)can I say that swap area created by linux is
> nothing but virtual memory.

It is initially appealing to make this observation, but "swap space" is not "virtual
memory" in exactly the same sense that "disk space" is not "files". When you create
(and mount) swap space you are reserving storage that the kernel *may* use to
temporarily store data it cannot afford to lose while managing memory operations.
But aside from being a big pile of potential, it isn't really anything...

> (2)is it correct to use the term(s) interchangeably

No. These terms are often used interchangeably in Windows documentation. This is
largely an error that comes from the days when all operations in windows were part of
a singular memory map. The confusion is carried forward today as a fossil.

The system (linux/windows/whatever) can have, need, and use virtual memory features
with no "swap space" whatsoever. The only real feature of virtual memory is the idea
that "where something is really stored" is not the same thing as where something (a
program/process/whatever) appears to be stored. This can happen usefully even when
it is all taking place in RAM (memory).

Imagine a computer with only five pages of memory. Imagine a moment when pages 1 3
and 5 are in use, and the system needs to load a program that is two pages long. In
the absence of virtual memory, that program couldn't be loaded because the two
available pages (2 and 4) are not "next to each other" so the program would be broken
in half.

In that same computer *with* virtual memory, the computer can create the "imaginary"
pages 6 and 7. The "virtual memory management hardware" takes the imaginary page 6
and maps it over page 4, and the imaginary page 7 and maps it over page 2. Those two
pages (6 and 7) are "next to each other" in virtual memory (even though the "real
pages" are not) and so could hold a two-page-long program.

Fundamentally, the best way to think about the term "virtual memory" is that a
virtual memory system insulates the way a task/program/programmer needs to see memory
from the way the hardware has memory actually arranged.

So the virtual memory system is playing a card game, where it is shuffling real
storage into place in various virtual/imaginary sets.

Now, in the same way that you, were you playing a game with hundreds of cards, would
like to be able to put some cards aside while sorting through things so that you can
handle the most important cards in play; the computer sometimes would like to put
aside pages of memory that are valid, but that nobody seems to need just now.

A swap file (which is also called a "paging file" in some systems) represents a place
where the system (linux/windows/whatever) can set aside (swap out) the data from a
page of real RAM memory, so that it can reuse that piece of real memory for another
purpose.

If the program that "owns" the original data tries to get to it a "page fault"
happens and the system finds a (usually different) page of real memory that isn't in
use (or that can be swapped out) and puts the data back (swaps) in this original data
page into the new place, and makes that new/other piece of RAM appear (to the
program) as if it had always been there and that it didn't move at all.

There are *LOTS* of other uses for Virtual Memory that have _nothing_ to do with the
swap file. Things like "sharing code" so that if 20 people are using a program the
computer doesn't actually have to have 20 copies of it in 20 different places in
memory. Things like being able to give the "screen memory" to a program to write to
directly, without having to "reserve a hole" for the screen memory in every program.
Things like copy-on-write where you share something until you want to change part of
it, and then the system "magically" makes a private copy of the page containing the
part you changed without it suddenly changing out from under (and crashing) another
program.

"Virtual Memory" is a short-hand name for either the task of making and maintaining
these logical maps/chunks, or the name for any one consistent logical chunk when seen
from inside. If phrase is used without enough context it can get meaningless. But
in no meaningful case is it "interchangeable" with the term "swap space".

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/