Clear file from memory cache

Millman, Mark 1 Reputation point

I'm developing in C++ and C# and working with larger files (tens to hundreds of GB) and performance is a major issue. However, once a file has been read it is cached in memory and subsequent reads no longer go to disk. This makes it difficult to gauge the performance. For example, in the program I'm currently working on a run immediately after a reboot takes 92 seconds. Re-running the program (and until I change input files or reboot again) it only takes 7 seconds.

I've used RamMap and this helps but it doesn't appear to let me clear individual files and if I Empty everything then my timings are off because everything in the system has to be reloaded.

Is there any way C++ or C# function to tell Windows to clear cached files out of memory using a file naming mask?

Thanks, Mark

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,591 questions
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,112 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor


    When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file.

    The function of FILE_FLAG_WRITE_THROUGH is write operations will not go through any intermediate cache, they will go directly to disk.

    You could use these two flags to bypass the cache when creating files, so as to avoid the inability to clear the cache of some files.

    Please refer to the document for more details.

    Best Regards,


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.