questions about virtual memory

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

if your computer has 16GB of RAM, can programs have memory addresses larger than 16,000,000,000?

yes!

when your program references an address (like 0x5c69a2a3), that's not a physical memory address -- it's a virtual address. The size of the address has nothing to do with how much memory your computer has.

where is the mapping from virtual addresses to physical addresses stored?

in the page table!

operating systems maintain mappings called "page tables" that map virtual memory addresses to physical memory addresses.

can the virtual addresses 0x100000 and 0x100008 be mapped to 2 far apart locations in physical RAM?

nope!

a page table maps chunks ("pages") of virtual memory to "pages" of physical memory. Each page is a lot bigger than a few bytes -- often about 4KB. If memory wasn't mapped in chunks like this, the page table would be way too big -- we'd use up all our memory just storing where memory is mapped!.

does every process have the same mapping of virtual memory addresses to physical addresses?

nope!

every process has its own page table, and your operating system needs to switch the page table every time it changes which process is running

can 2 different processes have virtual memory addresses that reference the same physical memory addresses?

yes!

for example, shared libraries (like libc) are in memory only once, but many different processes can have references to them. This saves a lot of RAM! These shared library references are read-only.

on Linux, how can you find out which virtual memory addresses a process is using?

cat /proc/$PID/maps

this will show you the memory maps for the process's executable, any shared libraries it's using, its heap, its stack, and any other memory maps it has.

when your program looks up an address (like 0x5c69a2a3), what part of your computer is responsible for looking up that address in the page table?

the MMU!

the MMU is a piece of hardware in your computer that's responsible for looking up addresses in the page table and mapping them to the right address in physical RAM.

is every virtual memory address in the page table mapped to an address in physical RAM?

nope!

for example, if memory is swapped out to disk, then the page table will instead have a different entry that lets the OS load the information back from disk into memory when that address is accessed

if you have data swapped to disk, can you read the data directly from the disk without using any RAM?

nope!

data always needs to be read back into RAM before your processes can read it.

does having swapped out memory always make your computer run more slowly?

nope!

if you swap out some memory to disk and never read it again, that's totally fine! Just having data stored in swap by itself doesn't use any CPU. Constantly moving data in and out of swap is what will really slow your computer down.

on Linux, how can you tell if your computer is moving data in/out of memory from disk right now?

dstat!

my favourite is the "paging" column of dstat, which will tell you how much memory was written to disk / read from disk every second. there are also other tools, like iostat or iotop.