Virtual Memory

Hugo Bayona
3 min readSep 29, 2020

--

The first thing we need to ask is, what is the virtual memory?

The very first time I hear of this concept my first instinct was thinking in the physical RAM

But, how is the machine putting the information there? And then you realize this must happen virtually. There must be some standard that allows every program or process running in your computer to use that physical device.

For this to happen the operating system is going to map virtual addresses into de physical address space for every process running, now let us see what this looks like.

Has you can see this would happen for every process, and this means not every part of the RAM is mapped, only some portions will be mapped, and those portions will be divided has you just see.

This also means that one process can’t see or access another process memory. And even in a deeper look, the kernel RAM is not accessible or visible from the userspace or by a process in the userspace.

The next thing we need to understand is that memory is not continuous it will be mapped and allocated depending on the current occupation and distribution.

And even in the case when you see a couple of variables or instructions that have continuous addresses in the virtual memory, this still does not mean that in the physical memory they are continuous.

Sometimes you will find a “swap partition” in your computer, the swapping is used to copy to or from the virtual memory (RAM) to or from a swap partition (Disk).

This partition is only going to be used when the RAM is full, at first the system will send the idle processes to the swap but if no more idle processes are available, it will start to swap active process, at this moment the computer will have a tremendous impact in the performance.

Once we have a general understanding of what is happening with the virtual memory we can think in memory regions (you can see this post), this regions have permissions and has a normal file they have read, write and execute.

And how we are used to, if you try to access a region of memory that is not mapped, or you do not have the permission you will get an error.

One of the most important if you are interested in Low-Level programming is that a non-mapped memory region will return a NULL pointer (address 0) … you guested, our old friend Segfault.

This is just a quick overview of what I learn about virtual memory, hope you enjoy this post and make you curious about the virtual memory.

Happy Hacking!!!

--

--