Virtual Memory And Physical Memory


Well to start off, virtual memory is a huge topic to cover. Pretty sure, I won’t be able to cover everything in one post, so I might make another one. Also note, I am not a computer expert, so if I get any of the information wrong feel free to tell me ( Email @ Contact ). Other than that, if you have questions, contact me and I will try my best to answer as well. I am also going to talk about the windows OS and Intel processor. Why? Since different OS ( Linux ) have different ways of doing things. Page Size in Linux also varies and so on, so to avoid confusion and make my life way easier, I will be talking about windows and Intel. This post was also supposed to be dedicated to someone, but I realized that it would help others as well and I don’t want another private post, so might as well make it public. I will also be talking about physical memory as well, so in a way, this post talks about both physical memory and virtual memory

Virtual Memory

To start off on virtual memory. Virtual memory was introduced when people started having quite a bit of problems just using the physical memory directly.

Virtual Memory basically gives a program the illusion of a lot of memory, when in reality, only some are being stored in physical memory and the rest either not stored in the physical memory or is stored in V-RAM ( disk ).

Problems Without Virtual Memory:

  • – Memory “holes” ( Memory Fragmentation )
  • – Overriding Existing Memory
  • – Not Having Enough Memory

Memory Fragmentation

Let’s create a situation where this problem might happen. Lets imagine you have 3 programs. First program occupies 2 GB of ram, second occupies 3 GB of ram and the third occupies 3 GB of ram. We will have a total of 6 GB of physical ram.

Now, lets run two programs. Program 1 and Program 2.

As you can see, program 1 occupies the first 0 – 2 GB and the second program occupies 2 – 5 GB of physical ram.

But now, you will not be able to run the third process due to not having enough ram. So you close program 1 to free up enough physical memory to allow your PC to run the third and second program together.

However, as you tried to run Program 3, you get a error. “Not Enough Memory”. Why? well, without having a middle man to manage the memory, a program will not be able to just split its memory into two parts without any help from the OS itself or heavy adjustments to the code itself.

The yellow holes on top and below Program 2 are also known as Memory Fragmentation. A program can’t really just split itself into two parts to fill up the two holes in the physical memory, so it leads to the error “Not Enough Memory”

Overriding Existing Memory ( Shared Memory )

Let’s create a situation like how i did for the one before. Imagine having 2 Programs. Both programs being games in which you set a high score or a score of sorts. Since sharing one huge memory region, physical memory, a program basically has access to the whole address space. Both programs occupies 1 GB of ram. You open both games trying to set a high score for both programs.

After some time, you set a high score of 139999 on the first game, after setting the record, the game decides to store the high score in a different part of the physical memory. Let’s say, program one decides to store it in address 2048.

After some time, you decided to switch game to your second game ( Program 2 ). After a few hours, you finally beat your previous high score. Your game decided to store the high score in a different part of the physical memory, just like your first game and decided to store it in address 2048.

Now, two things can happen. Overriding the first game’s high score or if both are trying to write / read it at the same time, it crashes.

Since both the programs somewhat shares one address space, the problem of overriding data hops in.

Not Having Enough Memory

This is quite self explanatory. Imagine having 4 GB of ram and only being able to run like 4 programs which uses 1 GB each. This obviously does not happen in the modern PC as it would be a huge drawback.

Without virtual memory or at least the paging system, you would run out of physical memory in a matter of a few programs.

How It All Got Fixed

The simple answer would be Virtual Memory but in details, virtual memory basically allowed the system to manage memory much better. Since each process has it’s own virtual memory, it will not run into problems like accessing each other’s memory unintentionally, getting rid of memory override. Memory Fragmentation was fixed by simply having a middle man which redirects and manages memory. Systems also started using the disk to store memory if your pc really runs out of physical memory. Note that this slows down performances by quite abit.

We will simply call the middle man managing all memory “Mapper”.

So with this mapper, you can redirect memory and basically have control over it. So when Program 1 is ran, it does not necessary be in load order.

This also means that each program’s virtual memory address does not equal to its physical memory.

Another problem that could occur is, performance. Since the Mapper is mapping one byte from virtual memory to physical memory, this creates a problem. Having a table which has entries for each byte is also a problem. That’s where pages comes about. The physical memory is divided into pages, each page is about 4 kB which makes mapping simpler and effective. This decreases entries by quite a bit as well.

If you are wondering what happens if the “Mapper” is not able to find a entry in it’s table for a curtain address? It would crash giving a Page Fault.

Leave a Reply