Understanding virtual memory, page files and swap space - a gentle tutorial
By Pcunix
Have you ever had a Post Office box?
We have a central mail pickup at the over 55 community where I live. Although it is outside, it looks very much like a large bank of Post Office boxes. Each box is numbered and we have keys to our own boxes. We drive or walk down to pick up our mail there.
The lowest box is just a few inches above the ground, the highest is about level with my forehead. When I pick up my mail, I sometimes see short people struggling to reach their unfortunately located box which I could reach easily, but they cannot. We sometimes joke that we should trade boxes, because mine is centrally located, but I am tall enough to reach any box. Of course the Post Office doesn't offer any such ability.
I also sometimes see overweight people struggling as they bend down to reach their box which is near the ground. They'd obviously prefer my postal box, too.
Everybody wants convenient height
Wouldn't it be wonderful if everyone could have a box at about mid height so that no one had to stretch and no one had to bend down? Everybody would love to have a central box.
Well, the Post Office can't do that, but let's indulge our imagination and pretend that they could. We'll just have one row of boxes, set at a convenient height. Let's say we have ten of those boxes.
Well, that won't work - there are over 800 people living here. Hold on, we are going to have to use some magic, but this is going to work. What we'll do is give 80 people the keys to box one, 80 the keys to box two and so on.
We'll get to how this is going to work in a moment, but because sometimes people do get a lot of mail and it won't all fit in one box, let's solve that problem too: we will give everyone a set of ten keys. Everyone in the place can open any box. People can get up to ten boxes full of mail, and no matter how short or overweight they are, they can easily get at it.
Not everybody needs ten boxes, of course. We don't care - the boxes are there if they will ever need them.
Introducing MIss Lightning
So how do we pull this off?
Simple. We just need a lightning fast Postal clerk on the inside of the boxes and a bit of electrical or mechanical help on the boxes themselves.
What happens is that if someone sticks a key in any of the ten boxes, a light goes on that shows this has happened. Our swift clerk jumps into action and leaps from the chair where she has been resting comfortably, nibbling on chocolate and sipping fine wine.
I think it's only fair to provide these perks, by the way: we'll be asking this clerk to do a lot of work.
So anyway, imagine that I have approached the boxes and put my key into box one.The clerk is alerted and she glances at the handy surveillance camera which shows her that it is me at the box.
She has previously memorized all of our faces, of course. This is yet another reason she deserves chocolates and fine wine.
Even before I can turn the key, she has rushed to box one. She looks inside. Does it currently contain my mail? If it does, she breathes a sigh of relief and returns to her chair. Her work is done.
But if it contains someone else's mail, or is empty, she must work very quickly. Before I can open the door, she removes any mail that is in there and quickly runs to her own interior boxes which we residents cannot see. Selecting an available box, she shoves the other persons mail into it, and then grabs a sticker and writes their name and "Box One" on it. She scans the hundreds of other boxes she has and if she sees one with my name and "Box One", she quickly grabs it and shoves it into my box.
She does this very, very quickly but also has another trick she uses. She possesses the remarkable ability to temporarily hypnotise me with a single word. She uses that to prevent me from actually opening the door before she's ready. I never notice this happening, because she is very, very quick.
So much mail!
Ah, but sometimes there is even more mail than can fit, even in all the hundreds of boxes she has inside. She has a way to deal with that, though. Back at the main post office they have thousands and thousands of boxes and more clerks. Those clerks are a little bit slower than she is, so we won't give them chocolates and fine wine, but we can still be nice to them, right?
Our clerk keeps a little book that tells her if there is still mail back at the main office. She might have had it here at one time and had to send it back because she ran out of room, or she might just have a note that tells her that, but either way, if there is, she calls up the main post office and tells them to put that mail in a truck and get it to her right quick.
This wil usually only take a minute or less - they have fast trucks. She'll hypnotise me, so I won't notice, but my wife, who was waiting for me to return with the mail, will likely notice that I'm taking longer than usual. She'll think I'm talking to a neighbor, maybe.
Realize that this isn't necessarily because I have a lot of mail. It might be other people's mail that filled up all her boxes and she shipped mine away because I skipped a day and didn't bother to check mail yesterday. She doesn't know when I might come back; I could be gone for days, so it made sense for her to send my mail to storage.
She's so good to us!
It's against Post Office rules, but Miss Lightning is so good at this that many of us also use these boxes to store our personal papers. We just stick them in one of the boxes and whenver we want them, all we have to do is open that box and our papers will be there!
If we don't access them for a while, she's sure to ship them back to the main Post Office, but if we go pull them out regularly to read them, she'll almost always keep them local unless she has a lot of other mail to handle.
She might even just leave it there in the box. If I put something in box ten and nobody else happens to try to use that box for a week, she'll just leave my stuff there. It saves her time and trouble if I go to look before anyone else does.
It all works out very nicely.
If you are like me and just love the geekery of this stuff, I highly recommend the book I have shown below. It's about Unix, but it really gets into the nitty gritty details of handling virtual memory and cache when dealing with multiple CPU's or cores.
![]() | UNIX® Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers Amazon Price: $45.00 List Price: $64.99 |
Virtual Memory and swap
The above fictional system is a close analogy to how virtual memory and swap work in almost every computer operating system.
A program is a resident. The 10 mail boxes are the ram memory of the computer. A program might want to use some, all or even more space than the boxes allow, just like a resident.
Also, just like a resident, programs prefer certain boxes.
Our Miss Lightning is the Virtual Memory subsystem in the computer operating system. Just as she got notified with a light that told her I had put my key in the lock, that VM system gets notified when a program wants to access a specific part of memory. If what is in there is what should be there for that program, the VM manager has nothing to do. If it is not the right contents, the VM manager acts to get the program pointed at the right box.
It doesn't work quite like Miss Lightning though. It can actually move things around - the program thinks it is about to look in box one, but the VM system actually rearranges things so it is looking in box 10 instead. It works that way because that's faster than copying things.
Sometimes it does have to copy, though - there's just no more room in the boxes - which are your computers RAM memory, of course. In that case, the main Post Office is your computer's hard drive.
Our analogy breaks down a little bit here, too. When the program initially loads from disk, the operating system may not load all of it. It will load what is called its "working set" - the minimum amount necessary to get it running. To go back to the analogy for a moment, let's say the program has boxes one to three but it would actually fill five boxes when fully loaded. If the program every tries to access box four or five, the rest wil be read from disk.
Programs also need their own data that they create - your input, the results of calculations and so one. This data will be stored in the remaining boxes, but if it needs to be moved out, it gets put in a special place on the hard drive. That place is sometimes always called "swap" because in early operating system design, entire programs and data were swapped out all at once. This was slow, so virtual memory systems were invented. These systems are working with memory pages, so the area is also called the "page file" today.
Armed with this analogy, I think you should be able to go read the Wikipedia article on virtual memory and paging and understand all or most of it very easily.
Do you know someone who should be reading this? Click the Share button below to send it to them easily or to post it to Facebook or Twitter.
- Why Windows 8 might Kill Microsoft
John Lennon was talking about world peace and the end of religion when he wrote that famous song. You might be easily forgiven if you assessed Microsoft's chances of going belly up as only slightly... - 6 months ago
- Three iPhone/iPad apps for U.S. Coin Collectors
I was surprised to find that there isn't much available. While there is a wealth of information available on-line, apparently few have thought about the tremendous advantage of mobile apps. After... - 17 months ago
- How to use Quickbooks in a cash basis business
While there are advantages and disadvantages to both, cash basis is often easier for a small businessperson to understand: you record income for taxing authorities when you actually receive or pay... - 16 months ago
- Would you really want to win the lottery?
People who object to all gambling would say the solution is simply not to have lotteries. That's unrealistic - there may or may not be valid arguments that the social cost of these activities exceeds... - 14 months ago
- Treasure in Your Pocket Change - WarTime Nickels (35% Silver)
I suspect the 1950's were probably the ideal time for collecting U.S. coins from pocket change. You could find Mercury dimes and Buffalo nickels daily. Even the older Barber dimes and the Liberty or... - 19 months ago
- How to clean your shower, your car and even your pepper mill with the Mr. Clean Magic Eraser (Melamine Sponges)
I used to hate cleaning the shower. The built up soap scum on the walls and the glass doors seemed to be nearly impossible to remove. People said to use vinegar - that didn't work. Abrasive cleaners... - 21 months ago
- How to keep mice out of your house or camper
The mice that live near the tree line see our home as a convenient source of warmth and food and would love to live inside with us, especially in cold weather. We would prefer that the mice stayed... - 21 months ago
- When to Take Early Social Security Benefits
You may have read other articles about taking early Social Security benefits. I certainly have, and have been disappointed to find that most ignore the more complicated aspects that are apt to be of... - 22 months ago


DzyMsLizzy 16 months ago
Very excellent analogy! Kudos!
Voted up, useful and shared.