Windows and Virtual Memory Usage

I’ve always felt that virtual memory in Windows could be done better, and lately I’ve been noticing it more than usual.


When you do something that uses a lot of memory (like play a game, compile a large program, etc), it seems that Windows doesn’t do a very good job of figuring out what to swap out and when to swap it back in.  The problem is especially noticeable once the memory hogging application is done.


I’ve created a simple program that demonstrates the problem nicely.  It’s called ForceSwap.cpp and the source is here: 



http://www.stevex.org/code/ForceSwap.cpp.txt


There’s a compiled binary here if you prefer.


This program simulates a program that uses a lot of memory.  It does this by continually allocating 64k chunks, and then walking through them after it’s allocated them, forcing them all to be in main memory.  This is a worst case scenario for the memory manager – an application wants a LOT of memory, and wants it all in physical memory because it’s using all of it.  This isn’t that far off from how a compiler, or a game behaves.


Run this program, and it will print the available physical memory after each allocation and walk of the allocated chunks of memory.  Once the available physical memory drops to near 0, it will continue to run, but will start hitting the disk, paging other applications out to make room for this program’s memory requirements and eventually thrashing virtual memory on each pass through the allocated chunks.


Let it run this way for a while, and then stop it (Ctrl-C) – this is the situation I want to look at.  Windows is now in a state where everything that can be paged out, has been.  Sometimes when you stop the program, the disk will continue to grind for a while – I’m not sure what that is, but I’m guessing that Windows started swapping stuff out and is continuing to do so. 


Anyway, once your system settles and seems to be idle again, try to use it.  Everything you do will be frustratingly slow.


For example, on my system, bringing up the Task Manager took 7 seconds.  Opening the My Documents folder took almost 30 seconds!  Right-clicking on a folder took 8 seconds.


These delays are because the virtual memory manager has to page a lot of stuff back in.  Opening the My Documents folder means much of the Windows Explorer, the Common Controls library, the icons, and many other things have to be fetched from the page file.  That’s understandable.


My point here isn’t that it’s wrong that the applications were swapped out.  The memory manager was doing it’s best to satisfy the requirements of this application.  But, I have a gig of RAM, and I absolutely hate the huge drop in performance for things that I expect to be instant, like clicking on the Start menu, hitting Windows-R to bring up the Run dialog, or opening a folder.


So what I want to do is find a way to lock into memory things that are part of maintaining UI responsiveness.  Explorer, for example, and all the memory it’s allocated.  The task manager.  Maybe even Outlook.


It would probably cost me 10% of my available RAM to do this, but to me, it’s worth it. 


The question is how.  The VirtualLock function is what I want to call, but I need to inject calls to it into the process whose memory I want to lock.  This isn’t so difficult for Explorer since there are lots of ways to write plugins for it that get called at various times; but is there a general way of getting code to run inside another process?


Another option would be to have the virtual memory manager notice that the memory shortage no longer exists, and start lazily bringing pages back into memory.  This would be a good solution I think but it would be helped a lot by having some way to prioritize what gets swapped in.


Anyone have any other thoughts on this?