Mega Code Archive

 
Categories / Delphi / Examples
 

Windows swap and virtual memory

An example of how to list process memory, and trick windows not caching out pages. Introduction The windows operating system (mainly talking about NT now, that is: windows 2000 Professional, but probably applies to NT4 and XP as well), have a rather 'stupid' caching system. Sure, for servers it might work well, but for desktop systems it is no fun. We put 512 Mb of memory in a PC, only to notice that windows still does quite a lot of swapping if we just played a game and want to restore our desktop. Why? because explorer.exe and other desktop applications were not used for a while, so their memory is swapped to the page file. Whereas the system file cache is full of game data, since that was the last data read from disk... Problem description It bugged me quite a while that windows has susch annoying memory management. And whatever your system specs are, windows will swap out things you don't want it to, since we want a responsive system. Also, personally i think it it strange that file caching gets more priority than application memory, but again: i suspect the caching schemes are optimized for server applications. What can be done about this. First, windows memory management system allows us to tag a memory page as 'non-cachable'.. Secondly, we have to find a way to flush the disk cache. Both issues are not entirely solved with my experiments, but at least they give a clou. They do work, but have some limitations. Examples First, i will give an example of how to list all memory pages in the system. Limitations: This excludes processes we have no acces to, writing a system service may or may not solve this. Secondly, i will show how to tag a page as non-cacheble. Limitation: only memory pages marked as "writable" can be altered. Third, i will show a quick-and-dirty, but very effective sample, of how to force windows to clean up the file cache. And last but not least i will show how we can monitor disk activities to make a pre-caching utility. For convenience and readability, i split this up in several articles. Hope this is OK with the siteadmins, else, admin, feel free to edit.