So far, I’ve talked about the mechanism by which virtual memory works. This isn’t the whole story. An operating system has to make a number of decisions. The biggest of these is which memory to swap out and which to swap in. In theory, the memory that won’t be needed for the longest time should be swapped out. In practice, determining which memory this will be is very difficult. Determining what to swap in is even harder. Most operating systems use what’s known as demand paging—they load pages back in as they’re accessed. This technique can slow things down a lot, because a memory access typically takes a few tens of nanoseconds, while a disk access takes several milliseconds. Working out which memory to swap back in before it’s needed can give a significant performance boost.
Virtual memory is a complex topic, and one that is the focus of a lot of research. Modern AMD systems, where each processor has its own memory and accesses to other processors’ memory are more expensive, make these tasks even more complicated.