top | item 2937938

(no title)

cowmixtoo | 14 years ago

Hmm... I still think its valid to interchangeably use "Virtual memory" and swap space. Swap space is just where your "virtual memory" lives, right?

discuss

order

scott_s|14 years ago

On modern systems, there are two kinds of addresses. "Virtual" addresses and physical addresses. Virtual addresses are tracked by the operating system, and they can span the entirety of the addressable address space. So, on a 32-bit system that isn't playing any high-memory tricks, that's 0 - 2^32, or 0 - 4 GB.

But your system may not have 4 GB. So the operating system has a data structure called a page table that has the virtual to physical mapping for each process. The processor accesses this table (it caches it in something called a TLB) so that it can convert the virtual address to the physical address.

An example using small numbers. Your program has a pointer to data. That pointer may have value 800. Let's assume that the amount of memory on your system is only between 0 - 400. So the processor has to convert the value 800 to a value between 0 - 400. It's the operating system's job to maintain that valid mapping.

Why does this matter, and why is it so tied up with paging to and from disk? Let's say the OS pages out the page containing that data. Then, later, it's paged back in, but in a different physical location in memory. Your program still has the pointer value 800, but your program still works correctly because the operating system keeps track of where in physical memory 800, for your process, maps to.

People in the Windows world often say "virtual memory" when they mean "swap space" because Windows would call the amount of swap space "virtual memory size." But virtual memory is the technique described above. Read the Wikipedia entry from above, or read an operating systems textbook for a full discussion of it.

pdubs|14 years ago

That's not entirely correct. The MMU generally handles virtual to physical memory address translation and the OS is only ever involved if there is a page fault. Outside of OS architecture and very specific and intended application, virtual/physical memory is completely transparent. When I hear "virtual memory" I assume reference to swap space unless otherwise noted because the technical meaning has such a specific domain.

cowmixtoo|14 years ago

Awesome explanation.

Still, an SSD makes this all better. When I was using virtual memory on a Ubuntu server and put the swap partition on an SSD, everything worked great. On a rotating platter, not so much.

fennokin|14 years ago

It's where your "virtual memory lives" in the event that you run out of physical memory. They are not interchangeable.

Hoff|14 years ago

It is entirely possible to have a virtual memory design with less virtual address space than physical address space (eg: 32-bit virtual, 34-bit physical), and virtual addressing would still be useful in this context.

Locke1689|14 years ago

Assuming you have demand paging, your virtual memory doesn't "live" anywhere.