Flush Windows File Cache

sweepHere is a small utility I made whose purpose is to flush (empty) the Windows file cache. It can also flush and purge memory working sets, standby and modified lists.

It can be useful to reduce memory usage of a particular VM (on a host with dynamic memory) or for testing and bench-marking purposes.

The utility was compiled with Delphi XE and comes as a simple command-line executable with just two options

  • by default (no option) it will flush the file cache working set and empty the memory working set, this is usually very fast and has limited impact.
  • “full” will also flush the modified list and purge memory standby list, this can be slow and may slow down the machine for a while after it, as the OS will reload in memory what it really needs.
  • “help” will display a small help.

You can download it here: FlushFileCache.zip (13 kB)

Note that it relies on the undocumented NtSetSystemInformation function. The source code will be available on a github repository as soon as I clean up the proprietary bits.

FWIW, if you have some memory-frugal service to run, it’s entirely feasible to use this utility on a Windows 2008 R2 server and stay under 128 MB RAM usage. Most of the unused OS services will be swapped out, leaving only core services in RAM.

4 thoughts on “Flush Windows File Cache

  1. Originally I wasn’t, I had optimized memory usage the classic way by shutting down unnecessary services. But I’ve been trying it now, and it seems to work alright, provided your need CPU more than RAM.

    Running it periodically is definitely an option, but if you have lots of data, you’ll be trading metered RAM for metered I/O (as things get swapped out and re-read), and more disks accesses can mean lower performance. But it gives an extra trade-off option, so more options for finding a good balance 😉
    I don’t know if it would make sense to run it from the service itself, or just use a scheduled task or an event (both are quite rich since Windows 2008). I’ve been using an event so far.

    Apart from that, it makes the low-RAM Windows VM much more viable for heavy-duty, as you can page out all the unnecessary OS services, leaving all the RAM for your needs.

  2. I mean this in all honesty and sincerity….why would I ever want to manually “reduce memory usage of a particular VM (on a host with dynamic memory) or for testing and bench-marking purposes.”…isn’t that the job of the OS memory manager ?

    Isn’t it preferable to have my workingset memory in my much faster RAM as opposed to pages being cached on disk? Or am I missing something?

    Barry Kelly (former Delphi compiler engineer) answered a question on SO a while back that indicates to me there aren’t many cases where a tool like this would be beneficial: Can memory be cleaned up? http://stackoverflow.com/a/2031886/12458

  3. @Mick If you have plenty of RAM, you want everything in RAM of course, but when you don’t, the OS will most likely make stupid guesses, because at best it’ll only have access to VM priority values and CPU workloads.
    It won’t have access to intents, desires, insight, foresight or experience, but you (as developer or system admin) do, or should at least 😉

    It’s perfectly possible (likely even) that a busy VM shouldn’t be prioritized because it’s only doing backups or background processing. Conversely, you may want an almost idle VM to get priority because you want low latency.

    If you let the OS handle everything, it’ll favor memory for the busy background VM. Tweaking VM priority is feasible if you only have a few VMs, but on modern hardware, you can have literally hundreds of VMs, each doing a variety of tasks with variable priorities throughout the day.

Comments are closed.