When you commit memory, you get a commitment to receive memory when you need it, but no sooner

Raymond Chen

When you allocate memory, say by creating a shared memory section or by calling VirtualAlloc with the MEM_COMMIT flag, what you have is known to the memory manager as a commit. The memory manager has promised that if you try to access that memory, the memory access will succeed. But that doesn’t mean that the memory manager knows exactly which memory chip on your motherboard it’s going to use. (Yes, I know that the memory is actually spread out over multiple memory chips, but I’m talking metaphorically. If you want to get all nitpicky, I’ll call them physical page frames since that’s what they are.)

This is actually obvious if you think about it. After all, if the memory gets paged out, and then paged back in, it might get assigned to a different physical page frame when it comes back. Therefore, the memory manager cannot promise “Okay, if you try to access memory at address 0x00900000, I will map that to physical page frame 0x471.”

But what if I turn off paging by disabling my page file?

Sure, that takes the “page it out and back in” scenario out of the picture, but the memory manager still doesn’t decide which page frame to assign to your virtual linear address until you actually access it.

Think of a memory commitment as obtaining a contract with a cleaning service to clean your room. You hire them to clean your room a week from Saturday. At the time you sign the contract, the cleaning service will typically not decide which member of its cleaning staff will come to your room. It just remembers that it has committed to cleaning your room, and it ensures that there will be somebody available to do it. For example, if they have a staff of ten people, and it takes a day to clean a room (hey, it’s a messy dorm room), then they won’t book more than ten jobs per day.

Why don’t they pre-assign the jobs? Well, different members of the staff may have different skills, and not all jobs may require all the skills of a particular staff member. For example, suppose the staff consists of two people, Alice and Bob. Alice is good at carpets and Bob is good at blinds. Your dorm room has neither. If the cleaning service decided to assign your job to Alice at the time you signed the contract, then it is taking the risk that it may receive another offer for a carpet job and it would have to turn it down because Alice is already assigned to your room. On the other hand, if it assigned Bob, then it’s taking the risk that an offer would come in for a job that included blinds. The best thing for the cleaning service to do is not to assign anybody yet, but still remember that it needs to assign somebody by Saturday morning, so it knows it can accept one more job, but not two. (And it can’t accept any jobs which involve both carpets and blinds.)

The memory manager does the same thing. It knows that it needs to assign some page frame to your process, but it doesn’t make any decision until you actually use the memory, so it can have maximum flexibility to satisfy other memory allocations in the meantime. And if you free the memory without ever accessing it, then the memory manager didn’t need to make the decision at all!

For example, a device driver might request some memory with a particular characteristic; for example, it might ask for memory below the 4GB boundary (because it’s communicating with a hardware device that doesn’t support addresses above 4GB), or it might ask for 64KB of physically contiguous memory. If the memory manager had decided ahead of time to assign physical page frame 0x471 to your process, it might have to turn down one of those other requests because your page frame was the only one available that fit the bill.

As one of my colleagues explained, “Commit is the system’s guarantee you will get a page when you need it. In giving you this guarantee, it does not have to give you the page yet.” Commit is merely a promise that when you need the page, it will be there.

The fire department promises that if there’s a fire in your house, they will come to try to put it out. But that doesn’t mean that there’s a tank of water at the fire station with your name on it.

0 comments

Discussion is closed.

Feedback usabilla icon