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.
operating systems maintain mappings called "page tables" that map virtual memory addresses to physical memory addresses.
0x100000
and 0x100008
be mapped to 2
far apart locations in physical RAM?
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!.
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
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.
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.
0x5c69a2a3
), what part of your computer is responsible for looking up that address in the page table?
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.
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
data always needs to be read back into RAM before your processes can read it.
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.
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
.