Virtual Memory, the way I wish someone had explained it to me

Memory has changed. Time to catch up.

Hi, I am GeoSn0w, programmer, and information security enthusiast. Let’s talk about computers and memory. I’ve made this write-up in an attempt to start explaining the great concepts behind the computer memory and how does the computer use it efficiently. But let’s start with small steps. Computers have changed quite a lot in the last couple of decades. Back when Commodore 64 was the best you could afford, things were a little bit different. The user had way more access to the memory of the computer. Nowadays the memory is a concept we take for granted and only programmers get to know it in-depth. Virtual Memory is sometimes brought into the discussion but more as a boogeyman than as an important feature.

When was the last time you used a POKE command? Me neither. Nowadays the memory is managed more by the Operating System itself and less by the user. Imagine working on a Commodore 64, in Basic, without knowing how memory works. Over the years, this concept of “memory” has changed quite a lot. What do we actually call memory? The RAM? The HDD? Or better said, the SSD? Many people nowadays call memory all of them. Computer manuals that teach you how to code and how to address the memory are no longer a thing. Just take a Commodore VIC 20 manual and read it, you’ll learn BASIC programming in no time. Nowadays computer manuals are just a bunch of legal stuff. A waste of paper as I doubt anybody loses his time with that.

New Memory. New Concepts.

A relatively new concept (if we start counting since bubble memory) is the Virtual Memory and Paging. Some people know about paging and virtual memory that is bad. This is some misinformation spread on the internet by people who have little to no understanding of how Virtual Memory works. Although it is correct that paging to the HDD/SSD is relatively bad, the concept of Virtual Memory is actually extremely useful for the PCs as we know them.

So what is paging? Well, When your amount of RAM (Random Access Memory) is too little for the demand created by your workload, a computer that uses the concept of Virtual Memory will start storing stuff on the hard disk or the SSD. Why is this bad then? Well, the HDD is way slower than the RAM, in fact, even an SSD is thousands of times slower than the RAM, which means that the data will be retrieved way slower and this will make the PC act crappy and slow.

But how does Virtual Memory work?

To properly understand what Virtual Memory is, let’s see how a system that doesn’t take advantage of it would work. Let’s take as an example the 32-Bit program address space which is 4 GB. Normally, without this layer that we call Virtual Memory or VM for short, the program would map itself directly to the physical memory. Let’s imagine a test program called TestME. The program address 0 will be mapped to physical address 0. 1 to 1, 2 to 2 and so on. You see what I am going for? It is pretty much a direct mapping between the program address space and the physical memory. But what if I have only 1 GB of RAM?

MIPS architecture guarantees a 4GB address space for any 32-Bit program, so what gives? If my amount of RAM is not physically 4GB how am I supposed to fit the whole program in there? See? This, amongst others, is one of the problems VM wants to fix. Without enough RAM, our program would crash. We don’t want that, so we must find a solution.

Up until now, TestME was using a 1:1 mapping. We’ve already established that it is a bad practice since we might not have enough RAM in the system. Virtual Memory is pretty much like an overlay, a map if you wish, that tells TestME where it is mapped in the physical memory. Instead of going 1:1, the mapping goes through this layer which then creates the actual mapping.

For example:

Program address 00000002 goes through the Virtual Memory and the VM maps it in the physical memory at address 00000005. Then program address 00000003 is being mapped by VM at physical address 00000001. And so on up until we fill the entire 1GB of RAM that we have.

Ok, so, what gives? I mean, isn’t it the same thing as mapping directly to the physical memory?

Well, not really. Now that we have this overlay or map that knows where each part of the program is mapped to the physical memory, we can solve the problem of running out of memory because now we can tell the program: You wanna map your address 00000004, but there is no more RAM, however, you can map yourself to the hard drive! See? All of the sudden, having the Virtual Memory overlay enables us to store the program chunks in different places when we’re low on RAM. Now our program will no longer crash because it ran out of memory to map itself into, it will simply be redirected by the VM to the hard disk for the rest of the chunks.

How virtual memory works

As you can see from the diagram, the program wants to map itself into the RAM. The Virtual Memory layer knows the whole program can’t fit there, and it will keep some of it on the HDD/SSD. Thanks to the VM, the program knows where to go on the HDD to retrieve its data back into the memory when needed.

This was a very simple write-up about the Virtual Memory. There is way more to it than this. The map contains pages. These pages contain WORDS (not actual English words). The pages are mapped into the physical memory and there are also some Page Tables we have to talk about. About these, about how is the address translation done and Multi-Level Page Tables, in the next write-up. I hope this one cleared the basic concepts of Virtual Memory.

— GeoSn0w (@FCE365)

 

Leave a Reply

Your email address will not be published. Required fields are marked *