So why does NT require such a wonking great big paging file on my machine?

UPDATE 5/5/2004: I posted a correction to this post here . Sorry if there was any inconvenience.

 Raymond Chen posted a fascinating comment on the dangers that paging causes server applications the other day.

One of the people commenting asked one of the most common questions about NT:

Why does the OS keep on asking for a wonking great big paging file?

I'm not on the Memory Management team, but I've heard the answer enough times that I think I can answer it :)

Basically the reason is that a part of the contract that NT maintains with applications guarantees that every page of virtual memory can be flushed from physical memory if necessary.

If the page is backed by an executable (in other words it's code or static data), then the page will be reloaded from the executable file on disk. If the page isn't backed by an executable, then NT needs to have a place to put it.

And that place is the paging file.

You see, even if your machine has 16G of physical RAM, NT can't guarantee that you won't suddenly decide to open up 15 copies of Adobe Photoshop and start editing your digital photo album. Or that you won't all of a sudden decide to work on editing the movie collection you shot in ultra high-def. And when you do that, all of a sudden all that old data that Eudora had in RAM needs to be discarded to make room for the new applications that want to use it.

The operating system has two choices in this case:

1. Prevent the new application from accessing the memory it wants. And this means that an old application that you haven’t looked at for weeks is stopping you from doing the work you want to do.

2. Page out the memory being used by the old application, and give the memory to the new application.

Clearly #2's the way to go. But once again, there's a problem (why are there ALWAYS problems?)

The first problem is that you need a place to put the old memory. Obviously it goes into the paging file, but what if there's no room in the paging file? Then you need to extend it. But what if there's not enough room on disk for the extension? That's "bad" - you have to fail Photoshop’s allocation.

But again, there's a solution to the problem - what if you reserve the space in the paging file for Eudora's memory when Eudora allocates it? In other words, if the system guarantees that there's a place for the memory allocation in the paging file when memory's allocated, then you can always get rid of the memory. Ok, problem solved.

So in order to guarantee that future allocations have a better chance of succeeding, NT guarantees that all non-code pages be pre-allocated in the paging file when the memory is allocated. Cool.

But it doesn't explain why NT wants such a great big file at startup. I mean, why on earth would I want NT to use 4G of my hard disk just for a paging file if I never actually allocate a gigabyte of virtual memory?

Well, again, there are two reasons. One has to do with the way that paging I/O works on NT, the second has to do with what happens when the system blue screens.

Paging I/O in NT is special. All of the code AND data associated with the paging file must be in non-pagable memory (it's very very bad if you page out the pager). This includes all the metadata that's used to describe the paging file. And if your paging file is highly fragmented, then this metadata gets really big. One of the easiest ways of guaranteeing that the paging file isn't fragmented is to allocate it all in one great big honking chunk at system startup time. Which is what NT tries to do - it tries to allocate as much space for the paging file when the paging file is created to attempt to help keep the file contiguous. It doesn't always work, but...

The other reason has to do with blue screens. When the system crashes, there's a bit of code that runs that tries to write out the state of RAM in a dump file that Microsoft can use to help diagnose the cause of the failure. But once again, it needs a place to hold the data. Well, if the paging file's as large as physical RAM, then it becomes a convenient place to write the data - the writes to that file aren't going to fail because your disk is full after all.

Nowadays, NT doesn't always write the entire contents of memory out - it's controlled by a setting in the Startup and Recovery settings dialog on the Advanced tab of the system control panel applet - there are 4 choices - None, a small "minidump", a Kernel memory dump and a full memory dump. Only the full memory dump will write all of RAM, the others limit the amount of memory that's written out. But it still goes to the paging file.