How to clean up windows file cache
To be honest, i haven't found a nice solution for it, that is: a (combination of) windows api that forces the system to clear the cache.
However, clearing the cache is quite easy. All we have to do is find out how many memory is allocated, and how many memory should be free.
Disk cache is not counted as allocated memory.
As soon as a (large) block of memory is allocated, windows (2000) will make place by removing cached items.
This shows how integrated the memory management and file caching system are.
Unfortunately, there is not a simple way to read the size of the system cache. This has to be done by reading windows performance counters, and is quite a bit of work it seems, so i did not figure out that part yet.
However, what we do know is the amount of memory that _should_ be free, according to the system. Just take a large part of that, effectively allocate it, and the file cache gets cleared. This all happens very rapidly, so allocating a large block of memory is not a real problem. In the process of comitting memory you could verify the size of available memory, but that is not in this example code:
procedure TForm1.tmrShrinkCacheTimer(Sender: TObject);
setlength (s, m.dwAvailPhys - m.dwTotalPhys div 8 - 32000000);
if s<>'' then
for i:=0 to length(s) div 4096 do
s[1+i*4096] := '.';
That was a lot of talk for a small piece of code :)
we only have to set 1 byte of each 4-Kb memory page to commit the whole page.