Clear file cache to repeat performance testing

PerformanceFileCachingDisk

Performance Problem Overview


What tools or techniques can I use to remove cached file contents to prevent my performance results from being skewed? I believe I need to either completely clear, or selectively remove cached information about file and directory contents.

The application that I'm developing is a specialised compression utility, and is expected to do a lot of work reading and writing files that the operating system hasn't touched recently, and whose disk blocks are unlikely to be cached.

I wish to remove the variability I see in IO time when I repeat the task of profiling different strategies for doing the file processing work.

I'm primarily interested in solutions for Windows XP, as that is my main development machine, but I can also test using linux, and so am interested in answers for that environment too.

I tried SysInternals CacheSet, but clicking "Clear" doesn't result in a measurable increase (restoration to timing after a cold-boot) in the time to re-read files I've just read a few times.

Performance Solutions


Solution 1 - Performance

Use SysInternal's RAMMap app.

rammap empty standby

The Empty / Empty Standby List menu option will clear the Windows file cache.

Solution 2 - Performance

For Windows XP, you should be able to clear the cache for a specific file by opening the file using CreateFile with the FILE_FLAG_NO_BUFFERING options and then closing the handle. This isn't documented, and I don't know if it works on later versions of Windows, but I used this long ago when writing test code to compare file compression libraries. I don't recall if read or write access affected this trick.

Solution 3 - Performance

A command line utility can be found here

from source:

> EmptyStandbyList.exe is a command line tool for Windows (Vista and > above) that can empty: > > - process working sets, > - the modified page list, > - the standby lists (priorities 0 to 7), or > - the priority 0 standby list only. > > Usage: > > EmptyStandbyList.exe workingsets|modifiedpagelist|standbylist|priority0standbylist

Solution 4 - Performance

A quick googling gives these options for Linux

  1. Unmount and mount the partition holding the files
  2. sync && echo 1 > /proc/sys/vm/drop_caches

Solution 5 - Performance

 #include <fcntl.h>

int posix_fadvise(int fd, off_t offset, off_t len, int advice);

with advice option POSIX_FADV_DONTNEED:
The specified data will not be accessed in the near future.

Solution 6 - Performance

I've found one technique (other than rebooting) that seems to work:

  1. Run a few copies of MemAlloc
  2. With each one, allocate large chunks of memory a few times
  3. Use Process Explorer to observe the System Cache size reducing to very low levels
  4. Quit the MemAlloc programs

It isn't selective though. Ideally I'd like to be able to clear the specific portions of memory being used for caching the disk blocks of files that I want to no longer be cached.

Solution 7 - Performance

For a much better view of the Windows XP Filesystem Cache - try ATM by Tim Murgent - it allows you to see both the filesystem cache Working Set size and Standby List size in a more detailed and accurate view. For Windows XP - you need the old version 1 of ATM which is available for download here since V2 and V3 require Server 2003,Vista, or higher.

You will observe that although Sysinternals Cacheset will reduce the "Cache WS Min" - the actual data still continues to exist in the form of Standby lists from where it can be used until it has been replaced with something else. To then replace it with something else use a tool such as MemAlloc or flushmem by Chad Austin or Consume.exe from the Windows Server 2003 Resource Kit Tools.

Solution 8 - Performance

As the question also asked for Linux, there is a related answer here.

The command line tool vmtouch allows for adding and removing files and directories from the system file cache, amongst other things.

Solution 9 - Performance

There's a windows API call https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-setsystemfilecachesize that can be used to flush the file system cache. It can also be used to limit the cache size to a very small value. Looks perfect for these kinds of tests.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen DenneView Question on Stackoverflow
Solution 1 - PerformancesmallestView Answer on Stackoverflow
Solution 2 - Performanceuser901295View Answer on Stackoverflow
Solution 3 - PerformanceEdwin van MierloView Answer on Stackoverflow
Solution 4 - PerformanceJohn NilssonView Answer on Stackoverflow
Solution 5 - PerformanceMarkView Answer on Stackoverflow
Solution 6 - PerformanceStephen DenneView Answer on Stackoverflow
Solution 7 - Performancerobertcollier4View Answer on Stackoverflow
Solution 8 - PerformanceseekerView Answer on Stackoverflow
Solution 9 - PerformanceGeoff JohnsonView Answer on Stackoverflow