Why does Windows 7 use the page file when there's free physical RAM?
I am using Windows 7 64 bit with 8G ram. After some use, I feel Windows is getting sluggish. The drive is thrashing. When I look at the resource monitor and disk activity, I see a few instances of use of the page file (c:\pagefile.sys). I check the physical memory and I see about 2.3G available memory and 700M free memroy.
Why doesn't Windows use more of the free memory and less of the page file? Does it need to leave some x amount of free ram, how much would that be? Is it a percentage of physical ram?
My plan is get more ram and an SSD for the main drive. Meanwhile I am suffering from slow performance.
First, don't place your page file on an SSD. While SSDs have gotten better about wear leveling, the page file is written to frequently and it will degrade your SSD much faster than general use.
What a lot of people don't understand is that windows never really has free memory. There's a few MBs which are kept free for burst demands, but otherwise, the difference between active in-use application memory and the total memory is generally consumed by what is known as "standby" memory.
These are memory pages which can be dumped if needed (standby memory is a great, big cache), so from an application standpoint, it's available, but they are not by any means not being used. Usually, they function as disk cache, or a pagefile cache.
Windows' goal is to keep the data most likely to be used in this standby cache, based on usage patterns. To use a contrived example, let's compare the relative value of the private memory of a program like Windows Update (usually configured to run once a week), over the caching the contents of your desktop in this standby memory:
For a good majority of the time, Windows Update is sleeping. It's holding up memory and for the most part, doing absolutely nothing of value with it while it's waiting for the schedule to come around. The contents of your desktop folder on the other hand, might be queried constantly, especially if you like to save files to it.
In this case, what Windows will do is page out the memory allocated to Windows Update (even though memory isn't "full", and use the space made available in RAM to cache the contents of your desktop. This results in better performance for you.
Windows is making thousands of these decisions and managing a disk cache for hundreds of files being constantly written to by background services while trying to balance this with memory demands of active applications. Sometimes it gets it wrong for a moment, and we might have to wait for it to page data back into memory when we switch to an application that's been sleeping in the background for a while. But what you have to think about is if it had kept that application completely in memory, how many other applications would in turn be bogged down waiting for disk writes and reads to complete, or themselves be forced to page out? What if those were applications you were using in the meantime?
Applications frequently allocate memory pages which are used very rarely, such as start-up code (used once and then not needed), shut-down code (used once and then not needed), or update code. It's not practical to keep all this in memory when there are much more important uses, so once Windows identifies sections of code that haven't been needed for the current operation of an application, it happily pages out those sections to the pagefile, even if it technically could retain them in memory.
(And actually, depending on the applications, systems might frequently allocate more memory than they actually have, expecting most of it to wind up paged out. If you're looking at a detailed memory breakdown, the "Commit" or "Commit" charge is how much memory Windows has allocated to various applications. The pagefile is used to provide guarantees for this memory, even if it doesn't have enough physical RAM to cover it.)
I just noticed you did make a distinction between available and free memory in your question; My apologies if you feel lectured and already knew the difference. Ideally, free memory is always 0. However, while standby memory is memory that can be released, it's not always memory that can be released quickly. If I try to write a 1GB file to disk, windows is going to stick it in a disk cache in memory if it can, and then slowly write it out to the harddrive in the background. If an application needs to request 50MB of additional memory, but none is available because this huge disk cache is still being flushed out, then the application will hang until it's available. Keeping a small buffer on hand allows the system to resolve this issue with minimal lag from the user's standpoint. You might also wind up with larger-than-normal buffers if Windows just emptied part of the standby cache or released a lot of in-use memory, but hasn't filled it with new cache data yet.
It's called planning ahead.
Writing memory pages to the pagefile when there is still plenty of RAM is a good thing. As soon as a program requests more memory than there is free, the OS can start clearing out space as soon as possible. Better to prepare now than later.
If the OS were to wait around, then you run into a performance bottleneck. If a program asks for more memory than is available, now you have to wait until the OS writes out changed memory block, and then free them up.
There is a tool to set the disk-cache by SysInternal.
cacheset.exe
You can find it here:
http://technet.microsoft.com/en-us/sysinternals/bb897561.aspx
I have had page file turned off for the last 7 years. Actually it's the first thing I do after fresh install. Never had any problem with it. (Actually I had one - game called "titan quest" performed a very stupid check at startup so I created a 4 MB pagefile only to make it happy and turned PF back off later). As for the original question. Windows uses our RAM for disk I/O cache. For some reason it thinks the disk cache is equally important to the active programs code and data. And there's no way to limit disk cache size. It's there by design and we can't do anything about it... Oh, wait! We can! Just turn the pagefile off. Running applications that are heavy on RAM? Buy more RAM or close one app before launching another. You can use Process Explorer to see how much memory is used at any given moment. Personally I have 8 GB RAM both at work (vmware + virtualbox + eclipse + firefox + delphi + adobe premiere + skype + more background programs ) and at home (heavy gaming) and can't remember when was the last time I saw the "low on memory" warning.