What is virtual memory?
There’s memory and then there’s disk space. There’s memory that’s on disk, not to be confused with memory that looks like a disk. Disk that looks like memory? Isn’t the disk a kind of memory? Or is it something else?
It’s very confusing, but we can clear a few things up. Disks and memory are fairly easy. Virtual memory is one way that they overlap; with a little explanation, we can make that a little less confusing, too.
Become a Patron of Ask Leo! and go ad-free!
(Note: The sizes of RAM and hard disk mentioned below may seem small by today’s standards, but the concepts definitely still apply. No matter how much RAM or disk space becomes commonplace, we always seem to want more. .)
First, let’s review the basics: memory versus disk space.
When a computer geek like me (or a computer salesman not like me) talks about computer memory or RAM (for Random Access Memory), we’re talking about a bunch of silicon chips in your computer that hold things like the operating system, the programs that you’re actually running right now, the document currently showing in your word processor, or the email that you’re typing up. Computers these days typically have somewhere between 128 megabytes (128 million bytes) and 4 gigabytes (4 billion bytes). What’s important is that when you turn the computer off or if it crashes – *poof* – anything stored in RAM is gone. That’s why when you’re editing a document, it’s a good idea to save to disk often.
When we talk about disks, we’re talking about the hard disk drives in your machine. Quite literally, a hard disk drive is typically made of several metal disks coated with a magnetic material not unlike a video or audio tape or the strip on the back of a credit card. The disk spins at a fairly high rate of speed and special “heads” can read, or record, a pattern of bits (ones and zeroes) on the magnetic surface. The operating system assembles those bits into bytes, and the bytes into the files that you might save, receive, or create. Disks do not lose what’s on them when you turn off the power. Typical disk sizes these days start in the 20 to 40 gigabyte range and go as high as 250 gigabytes. It won’t be long before we see the next step, the terabyte (one trillion bytes), on a single disk.
Compared to memory, disks are much slower. Too slow in fact for your computer to work from directly. So the typical sequence of events is to load your program or document into memory from disk, have it run, or be worked on in memory, and then either remove it from memory if it hasn’t changed or save any updates back to the disk.
Virtual memory is simply the operating system using some amount of disk space as if it were real memory.
Exactly how virtual memory is implemented is complex and well beyond what I’d want to present here. But in an over-simplified nutshell, it works like this:
- You run programs that need memory. The operating system takes care of tracking which program is using what portions of memory and allocating each program the amount of memory that it needs.
- Those programs will need more memory as they do their jobs. Opening a large document may cause your word processor to request additional memory from the operating system in order to hold the document.
- If there isn’t enough memory available to satisfy a request, the operating system may decide that another program’s needs are less “important.” Some of that program’s memory will be freed, first by writing the contents to disk (the memory is
“swapped out”), and then allocated to the program making the request.
- Later, when the program whose memory was swapped out needs it back, that memory can be “swapped in” by reading it back from disk. This might cause memory from another program to be swapped out to make room.
Also remember that the operating system itself is also just a program. So it will have need for memory, too. It can allocate memory to itself and its memory may get swapped out to disk as other needs arise.
As I said, disks are slower than memory, so if the operating system is doing a lot of swapping between the two, it’s going to slow your computer down. If that’s happening frequently or if your computer seems to be “thrashing” or constantly swapping in and out from disk, then it might be time to add some memory to your machine. It can be one of the most cost-effective ways to increase your system’s speed.