2004-10-07 09:17:55

by Ankit Jain

[permalink] [raw]
Subject: VM Vs Swap Space

hi

if somebody can tell me that is this correct?

(1)can i say that swap area created by linux is
nothign
but virtual memory. (2)is it correct to use the term
interchangeably

thanks

ankit

________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html


2004-10-07 19:32:54

by Rik van Riel

[permalink] [raw]
Subject: Re: VM Vs Swap Space

On Thu, 7 Oct 2004, Ankit Jain wrote:

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

No. Virtual memory can mean a lot of other things, including
the address space of individual processes, or the kernel
address space.

--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan

2004-10-13 22:47:33

by Robert White

[permalink] [raw]
Subject: RE: VM Vs Swap Space

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

-----Original Message-----
From: [email protected] [mailto:[email protected]]
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".

2004-10-13 23:35:50

by David Schwartz

[permalink] [raw]
Subject: RE: VM Vs Swap Space


> 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


2004-10-14 04:13:20

by Lee Revell

[permalink] [raw]
Subject: RE: VM Vs Swap Space

On Wed, 2004-10-13 at 19:35, David Schwartz wrote:
> > 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. ;)
>

"Unix Internals" by Uresh Vahalia is the best book on the subject I know
of. You could never explain virtual memory in a single mailing list
thread...

Lee