Virtual Memory Management


Virtual Memory Management

Anna
So we're diving into virtual memory this week. I remember from last week we talked about paging and segmentation, but now we're essentially pretending we have unlimited memory, right?

Michael
Exactly. Virtual memory lets us run programs that are bigger than physical memory by only loading the parts we actually need. Think about PowerPoint—when you're just presenting slides, you don't need the spell check or animation code loaded into RAM.

Anna
That makes so much sense! Why waste memory on code that's not being used? So we load pages on demand, and that's called demand paging. But what happens when a program tries to access a page that isn't in physical memory?

Michael
That's a page fault. The CPU traps to the OS, which finds the page on disk (usually in swap space), loads it into a free frame, updates the page table, and restarts the instruction. But if there's no free frame, we need to swap something out first.

Anna
And swapping out can be expensive because you might have to write the old page back to disk if it's been modified. That's where the dirty bit comes in—if it's clean, you can just discard it.

Michael
Right. The goal is to minimize page faults because disk I/O is slow. Effective access time depends on the page fault rate. Even a small probability of faults can cause a huge slowdown—like, if memory access is 100 nanoseconds and a fault takes 10 milliseconds, the system can slow down by a factor of 40.

Anna
Wow, so we want page faults to be really rare. That's why we use page replacement algorithms to decide which page to evict when memory is full. We covered FIFO, which is simple, but it can suffer from Belady's anomaly—more frames can actually cause more faults.

Michael
Yeah, which is counterintuitive. That's why better algorithms exist, like Optimal replacement—which replaces the page that won't be used for the longest time. But we can't predict the future in practice, so it's just a benchmark.

Anna
Then there's Least Recently Used (LRU). It uses the past to predict the future: evict the page that hasn't been accessed for the longest time. For the example reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 with 3 frames, LRU gives 12 faults versus FIFO's 15 and Optimal's 9.

Michael
LRU performs well, but implementing it exactly can be costly. Some systems approximate it with a clock algorithm or use reference bits. The key is to avoid thrashing—when the system spends so much time swapping pages in and out that little actual work gets done.

Anna
Thrashing happens when the total working set of all processes exceeds physical memory. The working set is the set of pages a process is actively using. If it doesn't fit, you get constant page faults. One solution is to suspend a process to free up frames.

Michael
Right. And that's why adding more RAM often helps: it increases the number of frames available, so more of the working set can fit, reducing page faults. But you have to balance it—giving a process too few frames leads to thrashing, too many wastes memory.

Anna
So modern OSes use demand paging with clustering—when they bring in a page, they might bring in surrounding pages too because of locality. And they adjust the working set size dynamically based on memory pressure.

Michael
Exactly. Virtual memory is a huge leap—it lets us run massive programs and multiprogram effectively. But we have to manage it carefully to keep the system responsive. It's all about balancing memory between processes and minimizing those page faults!

Ask anything
We've read this whole study set, so questions about it get the best answers.