RE: VM Vs Swap Space

From: David Schwartz
Date: Wed Oct 13 2004 - 18:37:51 EST



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

One of the problems with a 'basic' explanation is that you always wind up
leaving out something that someone else things is important. ;)

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

Even if it only had one page, it could load half the program in one page.
When the program tried to access the other page, it would fault. The first
page could be discarded and the second loaded. So the system could operate
even if it didn't have enough pages or any swap space.

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

Or, if the data is already stored somewhere (if it's a page from a file
that hasn't been modified), it can just throw the page away.

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

This works the same whether the place is a file or a swap page.

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

Swap space, basically, allows a virtual memory system to move pages that it
can't discard out of precious physical memory.

DS


-
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/