There is a special type of filesystem used to swap memory.

This is not something you’ll need to use directly. But since we’re talking about filesystems, now’s a good time to explain paging and swapping.

Listen to the full episode to learn more details including an imaginary example that explains virtual memory and paging. You’ll also learn a bit about how the microprocessor enables virtual memory by going into protected mode. And a bit about random access memory or RAM. Putting all this together, you’ll understand how your computer can appear to have more memory than exists and what role the filesystem plays to enable all this. You can also read the full transcript below.


This is not something you’ll need to use directly. But since we’re talking about filesystems, now’s a good time to explain paging and swapping.

Depending on your operating system, this might be managed for you. I know that Linux users should have a partition with a Linux-swap filesystem setup. Other operating systems might give you the option for controlling paging or offer to manage everything for you.

I’ll get to what paging is in just a moment. For now, just know that this is something the operating system needs to do sometimes when it gets low on memory. The benefit of having a dedicated partition for this is the operating system has one place to go when it needs more memory and doesn’t have to squeeze things into the same space you’re using to hold your important documents. Without a separate partition and dedicated filesystem, then the operating system will use a file that it creates.

Usually, these files created by the operating system (and there are others), will be hidden from you so that you don’t accidentally delete them. The idea is that it also helps avoid clutter. However, one of the first things I do when setting up a new computer is to turn on the display of hidden and system files. I also choose to show filename extensions. Now, you might not be used to all this extra information. As a programmer, my advice is to start paying attention to these little details. And knowing what your operating system is doing with your filesystem definitely qualifies as something you should be aware of.

Modern Windows operating systems make things a little more confusing with the addition of a swap file and a paging file. I haven’t done a lot of the new Windows App programming but from what I understand, the OS uses the swap file to hibernate the new Windows apps that you download from the Windows App Store when they’re not being used.

Don’t get this confused with the swap partition in Linux. That’s more like the Windows paging file. Until Windows started making the distinction, I would normally use the terms swap and page to mean the same thing.

Alright, let’s start with why a computer needs a swap partition or a paging file. I mentioned that it helps when the computer gets low on memory.

Imagine this scenario, you have a clean desk. That’s already a stretch for me. Maybe you’re more tidy. I’ve got stuff all over my desk until I normally can’t see the surface anymore. But pretend for a moment that such a thing actually exists and you have a clean desk that can only hold let’s say 10 sheets of paper without any overlap. Since this is just imagination, we don’t want any paper to lay on top of another sheet. Let’s just call these pages.

Now, what do you do when you need more than 10 pages? You could buy a bigger desk. That would be like installing more memory in your computer. But even then, you’ll eventually hit some limit to how many pages can fit on your desk. So for now, let’s assume we already have the biggest desk we can afford. And it will only hold 10 pages.

A simple solution is to get a box that you can put on the floor that can hold pages you don’t need right now. You don’t want to just put the pages on the floor for the same reason that you don’t want any overlap on the desk. In this imagination, there are strict rules that must be followed.

Now you can work with 20, 30, or even a hundred or more pages. As long as you don’t need to have more than 10 pages on your desk at any given time, you have a system that works. But it’s a bit slow when you need to bring in a page from the box. Here’s what happens.

You’ve got your 10 pages and realize that you need to write something on a new page. What do you do? Well, you first have to decide which of the 10 pages currently sitting on your desk you can give up for a while. A good way to do this is to pick a page that you haven’t needed in a while. Preferably, you’ll keep all the pages you’ve used most recently and put the one last used the longest time ago into the box. Now you have some free space on your desk and can go to your drawer where you keep a stack of empty paper and take one out and lay the new page on your desk.

What do you do when you need something from the box? Just like the new page scenario, you first need to make room for the needed page by putting an existing page into the box. Then you can flip through all the pages until you find the one you want. Once you find it, you can remove it from the box and place the needed page back on your desk.

In this example, the desk acts like your computer’s available memory. It used to be that when your computer ran out of RAM memory, then that was all and your application would start getting out of memory errors.

RAM memory just means random access memory. All that really means is that you can read and write any memory address you want without having to go through a bunch of other addresses first.

If you remember the old cassette tape players for music, how did you get to a song you wanted to hear? You had to either wait until that song came up next or you had to fast forward or rewind the tape. And if the song you wanted was on the other end of the tape, you had to wait sometimes several minutes for the tape to move to the right spot. This is an example of something that is not random access.

If you go back in time further to 8-track tapes, they used a single loop of tape with a clever winding mechanism that would allow the tape to unwind from the inside of a coil while winding back on the outside. The result was that an 8-track tape could play continuously without needing to rewind. It also had a metal strip at one point in the tape that was detected and would cause the magnetic pickup heads to shift a little up or down. This resulted in 8 separate tracks of information and meant that the tape didn’t need to be as long as on a cassette tape. A cassette tape really had 2 tracks and you could switch between them by ejecting the tape and turning it over to the other side. Both of these systems had to go through long lengths of tape until you got to the song you wanted.

Compare that with a vinyl record. Switching songs on a record just took a steady hand to drop the needle into the grooves between songs. Sure, you had to look closely to find the wider grooves between songs and sometimes needed to carefully count the songs, but this was essentially random access. You could go to any song directly with just a flip of the record and a drop of the needle. CDs came out, and later DVDs, and you could no longer manually control the position of the pickup heads, but they were also random access. A computer memory is like this too. You can go anywhere in memory to read or write. All you need is the memory address.

Let’s talk for a moment about those memory addresses. First, there’s actual physical memory addresses each byte of memory in your computer. The computer microprocessor places a series of ones and zeros on the address lines that make up a specific memory address and the memory chips respond by first figuring out which memory chip holds that address. There could be quite a few memory chips and each one is responsible for just a portion of the total RAM in your computer. Anyway, one of the chips will be selected and will dive into itself to get the information and then the data will be places on the data lines also as a bunch of ones and zeros. Each address line or data line is used to hold a single one or zero.

These addresses never change. And the space in the memory chip that holds information will always respond to the same memory address. This is controlled by the actual wires running from your microprocessor through the motherboard and into the memory chips. That’s what I mean by a physical memory address.

When a computer starts up, all it knows about is physical addresses. It knows where in memory to go to get its first instruction and how to get to the next instructions. And then it just starts reading and running the commands it finds. At some point, the commands will tell it to start building a special table in memory where it can control virtual memory.

Now, I’ve seen some confusion about virtual memory and how the paging to the hard drive is virtual memory. It’s not. Sure, you need virtual memory in order for paging to work. But you don’t need paging to have virtual memory.

When I described the example of the desk with the pages, this was virtual memory. The physical memory would be the spots on the desk that were used to hold the pages. Without virtual memory, it would be like writing directly on your desk. Nobody does that, right? At least most of us outgrow that in grade school. Virtual memory allows you to work with pages of memory that you can move around like pieces of paper on your desk.

The really cool part is that you can place the pages in the same spot on your desk when doing your budgeting as when paying your bills or when drawing some art.

Okay, the microprocessor gets this table setup in physical memory and a few other tasks and then it runs a special instruction that sets a very important bit in a control register. This bit tells the microprocessor to switch modes into protected mode. And it’s like the microprocessor suddenly comes alive. Entering protected mode enables virtual memory and with it the ability to page memory to disk when needed. It also enables isolation between processes and other things such as privilege levels.

What started out as a plain desk where you had to write directly onto the desk now becomes a system where you can divide the memory into pages with their own address. This address is specific to a process and can map to different physical addresses. This means that two different running applications might think they each have the same memory addresses when in fact, these are just virtual memory addresses that map to some block of memory that can get moved around in the actual physical memory and even moved out of memory entirely and written to the hard drive without the application being aware of any of this.

This is how your computer can let you run hundreds of applications and each application thinks it has 4 GB of memory to work with all by itself when your computer might only have 8 GB of memory total.

If you try running too many applications or some of your applications request a lot of memory, then your operating system will use up much if not all of its physical memory and will need to start removing pages of memory and putting them in the swap space or in the paging file on your hard drive until that memory is needed again. So a page of memory is just a block of many addresses, maybe 1024 bytes or 4096 bytes or more, that can get moved around together. Managing pages is better than managing individual bytes.

And remember that this is called virtual memory because these pages have addresses that no longer match the physical addresses. The pages can be moved out of main memory and into a file or partition in your filesystem so that your computer can work with more memory than it actually has.

You can still get out of memory errors if you try to use more memory than a process is allowed to use. Or if your memory has become fragmented so that the memory manager can’t find a single block of virtual addresses that are unused. When your application asks for a hundred thousand bytes of memory, what happens is the runtime tries to find a single section of memory in the heap that is a hundred thousand bytes long. This is all virtual memory and will need to be mapped to many pages of virtual memory that can then be loaded into available places in physical memory. The physical memory may not be all together. But each page will be held together in physical memory.

Now, when your application starts working with that memory, it reads or writes to virtual memory which gets mapped to some page of virtual memory. That page is looked up in the table that was created before the microprocessor entered protected mode to see where in physical memory the page actually resides. Hopefully, the page is still loaded into physical memory. If not, then this is called a page fault and the application will be paused while that page is found on the hard drive and loaded into memory. Once it’s been loaded, the page fault completes and the application continues as if the page was loaded in physical memory the whole time.