# What Is Virtual Memory?? And how do you retrieve it?

**URL:** <https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919>\
**Category:** Factual Questions\
**Created:** [May 10, 2009, 4:42am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919 "2009-05-10T04:42:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougie\_monty](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@dougie\_monty](https://boards.straightdope.com/u/dougie_monty)\
**Post date:** [May 10, 2009, 4:42am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/1 "2009-05-10T04:42:37Z")

</div>

I have a Dell Laptop–4100 Inspiron. I was preparing photos (scanning, editing, captioning) when the procedure suddenly broke down–I got a message that the memory was low. This warning message came before I deleted extra copies of photos from the scan list in My Pictures. (After I did so, the out-of-memory message did not appear.) Do photos really take that much of a bite out of virtual memory?

---

<div class="post-metadata">

**Author:** ![J\_Cubed](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@J\_Cubed](https://boards.straightdope.com/u/J_Cubed)\
**Post date:** [May 10, 2009, 4:45am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/2 "2009-05-10T04:45:24Z")

</div>

It’s not so much the number of photos on disk, but the system trying to manipulate lists of them and/or display all the thumbnails.

---

<div class="post-metadata">

**Author:** ![Casserole](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@Casserole](https://boards.straightdope.com/u/Casserole)\
**Post date:** [May 10, 2009, 5:23am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/3 "2009-05-10T05:23:47Z")

</div>

Virtual Memory is the page file on your system. It usually occurs when you have many programs open simultaneously, using up all of your physical memory, as well as the page file.

Windows can dynamically alter the size of the page file temporarily so that you can get your work done, but because accessing the page file is incredibly slow compared to the physical memory, it is not advised.

---

<div class="post-metadata">

**Author:** ![SenorBeef](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senorbeef/32/2220_2.png) [@SenorBeef](https://boards.straightdope.com/u/SenorBeef)\
**Post date:** [May 10, 2009, 5:29am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/4 "2009-05-10T05:29:06Z")

</div>

Virtual memory and page files are different things. Virtual memory is the technique of having programs making memory reads/writes from a virtual range that windows assigns them instead of directly attempting to address specific physical memory addresses as they used to with older operating systems.

Page files interface with virtual memory when windows decides that the data in the virtual memory allocated to a program should go on a hard drive instead of ram.

---

<div class="post-metadata">

**Author:** ![TBG](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@TBG](https://boards.straightdope.com/u/TBG)\
**Post date:** [May 10, 2009, 8:00am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/5 "2009-05-10T08:00:32Z")

</div>

> [@SenorBeef](#):
>
> Virtual memory and page files are different things.

From a technical standpoint that may be true, but with regards to general usage, particuarly with Windows, the terms have more or less become interchangeable. Probably because when you go to the “virtual memory” settings of My Computer the only options there are about adjusting the size and location of the “paging file”.

---

<div class="post-metadata">

**Author:** ![engineer\_comp\_geek](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/engineer_comp_geek/32/504_2.png) [@engineer\_comp\_geek](https://boards.straightdope.com/u/engineer_comp_geek)\
**Post date:** [May 10, 2009, 9:00am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/6 "2009-05-10T09:00:15Z")

</div>

Virtual memory is a technique used by most operating systems today. Let’s say your computer has 1 GB of RAM. What happens if you tried to load more than 1 GB of data into it? Well, if you didn’t have virtual memory, your computer would come crashing to a halt because it ran out of memory. What windows and most other operating systems do these days is they take some chunk of disk space (let’s say another 1 GB) and use that as a “swap file” or “page file”.

Let me use an oversimplified example to show how this works. Let’s say your memory has 10 pieces in it, numbered 1 to 10. Now, in the old days, if a program wanted piece 5, it asked for piece 5, and that was that. These days it is more complicated. Now, let’s say you have 2 programs running. Program A asks for 4 pieces of memory and program B asks for 3 pieces of memory. Windows says ok, piece 1, 2, 3 and 4 belong to program A, and 5, 6, and 7 belong to program B. But here’s the thing. Windows doesn’t tell program B that he’s got 5, 6, and 7. Program B thinks he has pieces 1, 2, and 3. When program B asks for piece 2, windows really gives him piece 6. Each program’s memory has been “virtualized”. Each program asks for piece 1 or 2 or whatever, but it has no way of knowing if it’s actually getting piece 1 or 2. Of course, it doesn’t matter. Program B gets 3 total pieces of memory, and he doesn’t care if he’s really getting piece 2 or piece 6. As long as he gets a piece when he asks for it, he can’t tell where it really came from.

Now here’s another problem. Let’s say we start up program C, and he wants 4 pieces of memory. We only have 3 left (8, 9, and 10). In the old days, your computer would crash (out of memory error). These days, windows and most other operating systems will take a chunk of disk space and will pretend that it is memory. So, lets say windows takes space for 10 more pieces of memory on the disk. So now, windows has 20 “virtual” pieces of memory. But here’s the thing, only 10 of them actually fit into RAM at any given time. So, windows says ok, program 4 is using piece 8, 9, 10, and 11, but 11 is over on the disk.

Now, what happens when program C actually tries to access what it thinks is piece 4? Windows goes ok, that’s really piece 11, and oops, it’s over on the disk. This is called a “page fault”. Windows immediately stops everything, and he picks one of the pieces that he thinks hasn’t been used much lately (let’s say he picks piece 4) and he shoves that onto the disk. Then he takes piece 11, and shoves it into the “real” piece 4. So, your physical memory has pieces 1, 2, 3, 11, 5, 6, 7, 8, 9, and 10, and the disk has piece 4.

Now what if program A asks for piece 4 again? Windows doesn’t necessarily shove it back into the 4th slot. It may say that oh, 9 hasn’t been used in a while, I’d rather shove it there. So your physical memory ends up being 1, 2, 3, 11, 5, 6, 7, 8, 4, and 10, and the disk has piece 9 on it.

Each program is still asking for piece 1 through whatever, but the real location of that piece in physical RAM could be anywhere. Hence the entire memory is “virtualized”.

Now one thing to keep in mind is that swapping things to and from the disk is SLOW, so windows tries very hard to keep the most commonly used pieces in RAM.

Now, in our example here, we have room for 10 pieces in RAM and 10 more on disk. Now let’s say program A keeps asking for more and more pieces. Well, we’ve used up 11 so far and we’ve got 20 total, so the first 9 times it does this, no problem. The next time it asks, though, we’ve got a problem. All 10 pieces in RAM are used up and so are all 10 pieces on the disk. This is when windows goes aw shit, we ran out of swap space, and it pops up that annoying message about adjusting the size of your swap file. What it’s doing is it’s figuring oh, maybe if I increased the disk file size from 10 to 15 I’ll be ok for a while. Then I’ll have 25 total virtual chunks to play with.

In reality you’ve got a lot more than 25 total chunks of memory to play with, but I hope that illustrates the basic idea. If you bring up the task manager (ctrl-alt-del) you can see how much physical memory you are using, and how much of your swap file you are using as well. My system that I am typing this on has 1 GB of RAM, and about a 1.5 GB swap file, and it’s using about 0.5 GB of that swap file.

So, the way that you “get back” virtual memory is you free up memory somewhere. If you keep opening up more and more pictures, each of those pictures takes up some chunk of memory, and eventually you are going to run out. You can close out your pictures, but some programs actually keep your pictures in memory even when you close them, so closing the picture on the screen doesn’t always work. It depends on what program you are using and how it manages its memory. “Good” programs clean up after themselves, so when you close out your picture on the screen the memory it was using gets freed up.

Deleting files off of your disk doesn’t do anything for freeing up chunks of RAM.

---

<div class="post-metadata">

**Author:** ![Rhythmdvl](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Rhythmdvl](https://boards.straightdope.com/u/Rhythmdvl)\
**Post date:** [May 10, 2009, 2:56pm UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/7 "2009-05-10T14:56:36Z")

</div>

Excellent post **engineer\_comp\_geek** (and others), thanks!

There have been lot of RAM tools during the years – are any of them worth the download?

---

<div class="post-metadata">

**Author:** ![Casserole](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@Casserole](https://boards.straightdope.com/u/Casserole)\
**Post date:** [May 11, 2009, 5:35am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/8 "2009-05-11T05:35:06Z")

</div>

It depends how they work. My guess is that they push “stale” memory into the page file to give the impression of “freeing up” memory for use.

The more aggressive you set the program, the earlier the program will declare a memory segment “stale” and shift it over.

What this means in practice, is that while you may have more memory for a new program run right afterwards, all of your other programs will dramatically slow down as windows literally hiccoughs for a second, finds the memory in the page file instead of the physical memory, and spends time putting it BACK into the physical memory (since it’s no longer stale).

Give and take. I’d rather spend $20 and buy another gigabyte of DDR2 RAM 🙂

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [May 11, 2009, 6:12am UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/9 "2009-05-11T06:12:01Z")

</div>

[The standard explanation seems to be “The Thing King And The Paging Game”](http://duartes.org/gustavo/blog/post/the-thing-king), which is old and doesn’t reflect current OS design but, hell, I seriously doubt **dougie\_monty** will ever need to optimize page faults:

> [@](#):
>
> This note is a formal non-working paper of the [Project MAC](http://en.wikipedia.org/wiki/MIT_Computer_Science_and_Artificial_Intelligence_Laboratory#Project_MAC) [stands for Multiple-Access Computer and Machine-Aided Cognition, and was around a decade or so before Apple] Computer Systems Research Division. **It should be reproduced and distributed wherever levity is lacking** , and may be referenced at your own risk in other publications.
> 
> Rules:  
> [ol]  
> [li]Each player gets several million things. [Each program has millions of bytes of information to use as it wants. (This was written in 1972.)][/li][li]Things are kept in crates that hold 4096 things each. [Bytes are kept in pages, 4096 bytes to a page.] Things in the same crate are called crate-mates.[/li][li]Crates are stored either in the workshop or the warehouses. [The workshop is your actual RAM. The warehouse is somewhere on the hard drive.] The workshop is almost always too small to hold all the crates. [You never have enough RAM.][/li][li]There is only one workshop but there may be several warehouses. Everybody shares them.[/li][li]Each thing has its own thing number. [Each byte is numbered with an address that is only unique within a given program. The OS lies so two programs can think they’re operating out of the same addresses.][/li][li]What you do with a thing is to zark it. [Each program modifies its own data.] Everybody takes turns zarking. [Only one program can run at a time, assuming you only have a single core CPU.][/li][li]You can only zark your things, not anybody else’s. [Programs generally don’t share much memory. Sharing memory leads to locking, locking leads to semaphores, and semaphores lead to the dark side.][/li][li]Things can only be zarked when they are in the workshop. [Only data in RAM can be directly accessed. (Note to geeks: The original author was using a PDP-10.)][/li][li]Only the Thing King knows whether a thing is in the workshop or in a warehouse. [Only the OS knows where any particular byte is. It lies to programs.][/li][li]The longer a thing goes without being zarked, the grubbier it is said to become. [Data is shuffled between disk and RAM based on how frequently it gets used.][/li][li]The way you get things is to ask the Thing King. He only gives out things by the crateful. This is to keep the royal overhead down. [The OS gives programs RAM a page at a time, and it shifts data from disk to RAM a page at a time.][/li][li]The way you zark a thing is to give its thing number. If you give the number of a thing that happens to be in a workshop it gets zarked right away. If it is in a warehouse, the Thing King packs the crate containing your thing back into the workshop. If there is no room in the workshop, he first finds the grubbiest crate in the workshop, whether it be yours or somebody else’s, and packs it off with all its crate-mates to a warehouse. In its place he puts the crate containing your thing. Your thing then gets zarked and you never know that it wasn’t in the workshop all along. [The OS lies to you. All the time. There is no escape.][/li][li]Each player’s stock of things have the same numbers as everybody else’s. [The OS lies about where your data really is in RAM.] The Thing King always knows who owns what thing and whose turn it is, so you can’t ever accidentally zark somebody else’s thing even if it has the same thing number as one of yours. [The OS is good at keeping track of its own lies.][/li][/ol]
> 
> Notes  
> [ol]  
> [li]Traditionally, the Thing King sits at a large, segmented table and is attended to by pages (the so-called “table pages”) whose job it is to help the king remember where all the things are and who they belong to.[/li][li]One consequence of Rule 13 is that everybody’s thing numbers will be similar from game to game, regardless of the number of players.[/li][li]The Thing King has a few things of his own, some of which move back and forth between workshop and warehouse just like anybody else’s, but some of which are just too heavy to move out of the workshop.[/li][li]With the given set of rules, oft-zarked things tend to get kept mostly in the workshop while little-zarked things stay mostly in a warehouse. This is efficient stock control.[/li][/ol]

---

<div class="post-metadata">

**Author:** ![si\_blakely](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@si\_blakely](https://boards.straightdope.com/u/si_blakely)\
**Post date:** [May 11, 2009, 3:00pm UTC](https://boards.straightdope.com/t/what-is-virtual-memory-and-how-do-you-retrieve-it/495919/10 "2009-05-11T15:00:41Z")

</div>

> [@Rhythmdvl](#):
>
> There have been lot of RAM tools during the years – are any of them worth the download?

> [@Casserole](#):
>
> It depends how they work. My guess is that they push “stale” memory into the page file to give the impression of “freeing up” memory for use.

Many of the “Memory Optimizers” try to reduce memory fragmentation, by forcing the allocation of large chunks of contiguous memory, and then releasing it again.

When the computer has finished starting and is ready to load applications, all of the memory is available in one big lump, called the _free list_. As programs start, that big lump gets broken up, with each program getting a small bit of the lump, and the big single lump in the free list gets smaller. At some point, an application will need to return some memory. This additional bit of memory will not be added back into the big lump, but will be added as a new small item on the free list. When a later request for memory comes along, the small free item may not be big enough to satisfy the request, so the big free item is broken down a bit more. Eventually, with requests and returns being made all the time, the free list is a long list of lots of small chunks of memory. Some of these items are next to each other (contiguous), and so can be merged back into bigger bits of memory that can satisfy new requests. This _free list optimisation_ is an expensive operation, and the OS will put off doing it until it really needs too - after a large memory request that cannot be satisfied has been made. This slows down the starting of applications that want lots of memory when it has been fragmented. All memory optimisers do is use background cycles to request (and then free) big chunks of memory to trigger free list optimisation in the background, so that it does not happen when you don’t want it to happen.

OS designers work very hard to avoid these fragmentation issues (First Fit strategy vs Best Fit, merge on return, background memory optimisation), so I doubt that memory optimisers actually add much benefit to XP or Vista, and can slow the machine down. It was an issue for Windows 3.1/95/98/ME, where there were critical areas of memory (under the 1Mb limit) that were critical for OS/Application operation and at severe risk of fragmentation. Even then, the cost of running an optimiser (in terms of system reliability and performance) was often greater than the benefit returned (more apps running, fewer _out of system memory/out of handles_ errors).

Si
