Memory Management Overview


Memory Management Overview

Corey
Hey Maria, let's talk about memory management. You know how an operating system runs multiple programs at once? It actually swaps them in and out of memory super fast, but to do that safely, it needs to manage memory carefully.

Maria
Right, because if programs could write over each other's memory, things would crash. So how does the OS protect memory? I remember something about base and limit registers.

Corey
Exactly! Each process gets a base register that points to its starting memory address and a limit register that tells how much memory it can use. Every time the process accesses memory, the hardware checks that the address is within that range. That's hardware address protection.

Maria
So the OS sets those registers when a process starts. But what about when the process is compiled? How does it know where it'll be loaded in memory?

Corey
That's where address binding comes in. The compiler generates relative addresses from the start of the process. At load time, the OS adds the actual base address. For some systems like embedded ones, binding happens at compile time since the address is fixed.

Maria
So there's a difference between the logical addresses the process uses and the physical addresses in RAM. That's why we need a Memory Management Unit, right?

Corey
Exactly! The MMU maps logical to physical addresses quickly. For example, a process might think it's at address 0, but the MMU relocates it to, say, 74000 using a relocation register. That way the process doesn't need to know where it really is.

Maria
And that's also useful for dynamic linking. Why load the entire library if you only need one function? You can load DLLs on demand, which saves memory.

Corey
Yes, and swapping is another technique. If a process isn't running, you can move it to disk to free up RAM. Then bring it back when needed. But swapping takes time—that's why SSDs speed things up compared to hard drives.

Maria
But if we just load processes contiguously, we get holes when they finish. That leads to fragmentation. There's external fragmentation, where we have enough total memory but not in one block, and internal fragmentation, where allocated blocks have wasted space inside.

Corey
Right! To deal with external fragmentation, you could compact memory by moving processes together. But that's costly. Or you could use different allocation strategies like first-fit, best-fit, or worst-fit. But they all have trade-offs.

Maria
That's why modern systems use paging instead. The OS divides physical memory into fixed-size frames and logical memory into pages of the same size. Then it just maps pages to frames using a page table.

Corey
Exactly! Paging eliminates external fragmentation because any free frame can hold any page. There's only a little internal fragmentation—on average half a page wasted per process. But we need to store the page table, which itself takes memory.

Maria
And every memory access now requires two memory accesses: one to look up the page table and one to get the actual data. That slows things down. So they use a Translation Lookaside Buffer, a small associative memory that caches recent mappings.

Corey
Yeah, the TLB speeds up address translation. If the page number is in the TLB, you get the frame number in parallel. The effective access time improves a lot with a high hit rate. And you can also add protection bits to pages, like read-only or valid/invalid.

Maria
And shared memory? Like multiple processes using the same DLL? With paging, you can have the same page frames mapped into different processes' page tables, as long as the code is reentrant.

Corey
Right. So paging with a TLB is the foundation of modern memory management. It handles protection, sharing, and fragmentation issues much better than older methods like segmentation or contiguous allocation.

Maria
It's neat how the OS manages all this behind the scenes. This is definitely one of those topics where the details matter for performance.

Corey
Absolutely. Understanding memory management helps you appreciate why systems behave the way they do. And for programmers, it's crucial for efficiency.

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