What is virtual memory?

1.01K views

I have a vague idea of what is, but I really have no idea on how it works with physical memory and what it actually does. I also would like to know how virtual memory, physical memory, virtual address space are related and what is the job of MMU.

In: Engineering

3 Answers

Anonymous 0 Comments

Your computer has (at least) two different ways of storing data. One is on your harddrive/SSD these tends to be big and slow. the other is the memory on RAM which tends to be much, much faster but also much smaller.

When a computer works with data or programs it loads them from the disk into the memory to have faster access to it.

However it might happen that you give your computer too much to do or too much data to handle at once and its memory just isn’t big enough too hold all of it.

At this point it takes a part of your drive and turns it into a pretend RAM. It swapps out data and programs it currently doesn’t need as often onto this pretend RAM on the disk.

As far as the programs running on the computer are concerned this pretend memory is just as much RAM as the physical RAM. the only difference is that it takes longer to work with because the computer has to constantly swap stuff around between the disk and the real RAM, slowing everything down.

So a computer may have 8GB or physical RAM installed but by swapping out some data onto its disk it may have as much 24 GB or RAM available to work with, if it used a 16 GB swap file.

To differentiate this total RAM that is partially pretend from the real physical RAM, it gets called virtual RAM.

Virtual Memory = Physical Memory + Swap File Memory

Anonymous 0 Comments

Every program has a memory space which is however big the operating system decides it to be (nowadays at least 2^32).

This space limitation presents a problem when your program requires more memory above the approximately 4GB. I am leaving out some details for simplicity.

To address this you would just increase the size of the memory each process gets, which if you are on a 64bit machine is relatively trivial since you can represent addresses much larger than 2^32.

The problem becomes how you manage to cram so much memory for each program into your machine and having your operating system manage all of this. (We don’t have 2^64-1 byte memory sized machines…) This is where virtual memory spaces come in.

A virtual memory space is really the operating system promising the program it has a certain amount of linear memory space, which doesn’t actually physically exist. The virtual memory space allows the program to have a simple linear view of its memory while the underlying hardware and operating system do magic, which is done with pages.

Pages are typically 4KB chunks (there are larger ones on supercomputers) of memory which are allocated to a single program and moved between the hard drive and the random access memory. The MMU handles the translation from a virtual address space address to a physical address in the RAM or potentially triggers a load from a hard drive. (I am leaving out caching entirely).

Pages are really the bit of this that allow the flexibility required. It allows sharing of pages to save memory instead of loading common libraries many times. And the flexibility for you to “have” 2^64 bytes, but not actually needing to allocate these in physical memory.

Swapping memory which is the process of saving pages onto the hard drive is usually a bad idea. Performance is severely affected. It prevents out of memory errors, but it is not a viable way to actually use a computer.

Anonymous 0 Comments

Virtual memory is basically a section of your hard drive acting as RAM when you run out of RAM.