-1

For POCs, I have written test methods for multiple approaches to achieve the same result. Problem is when I run the first test, some (.txt) files are cached. It is visible with RAM usage going up pretty much by the same amount as the size of those files. The consolidated files' size is almost a GB and reading them from the disk is not trivial. This will give the second method an unfair advantage over the first.

How do I purge the RAM programmatically on Windows desktop in my C# code before both tests? Essentially, I need the code for (or some tips to write) the FreeRAM() method in the snippet below:

public static void Main()
{
    FreeRAM(); // Need code for this method
    RunAndTimeTestOne();
    FreeRAM();
    RunAndTimeTestTwo();
    FreeRAM();
    RunAndTimeTestThree();
    .
    .
    .
}

Also, if doing this is not possible programmatically, I would be happy to learn that too.

displayName
  • 13,888
  • 8
  • 60
  • 75

2 Answers2

1

Let us first understand what 'purge' on Mac OS X exactly does...

purge -- force disk cache to be purged (flushed and emptied)

Purge can be used to approximate initial boot conditions with a cold disk buffer cache for performance analysis. It does not affect anonymous memory that has been allocated through malloc, vm_allocate, etc.

However Windows caches file data that is read from disks and written to disks. This implies that read operations read file data from an area in system memory known as the system file cache, rather than from the physical disk. Correspondingly, write operations write file data to the system file cache rather than to the disk, and this type of cache is referred to as a write-back cache. Caching is managed per file object. Therefore, 'When the data would be written back to disk?' is decided by Cache Manager.

How does Cache Manager work? Read below...

Caching occurs under the direction of the cache manager, which operates continuously while Windows is running. File data in the system file cache is written to the disk at intervals determined by the operating system, and the memory previously used by that file data is freed—this is referred to as flushing the cache. The policy of delaying the writing of the data to the file and holding it in the cache until the cache is flushed is called lazy writing, and it is triggered by the cache manager at a determinate time interval. The time at which a block of file data is flushed is partially based on the amount of time it has been stored in the cache and the amount of time since the data was last accessed in a read operation. This ensures that file data that is frequently read will stay accessible in the system file cache for the maximum amount of time.

Can you change the behavior of your cache manager ? Yes, you can. Here is how you do it and this page also tells about all other things that can be done to your RAM using Windows' Cache Manage API. But it might result in unexpected behavior.

If the system is not flushed often enough, then the likelihood is greater that either system memory will be depleted by the cache, or a sudden system failure (such as a loss of power to the computer) will happen before the flush. In the latter instance, the cached data will be lost.

Hope you get the sense of how to perform 'purging' in Windows and why it is a never needed operation.

displayName
  • 13,888
  • 8
  • 60
  • 75
etr
  • 1,252
  • 2
  • 8
  • 15
0

It's really unnecessary. Win 7 manages memory quite well.

But, if that's not a good enough answer for you, take a look- http://www.tomshardware.com/forum/37230-63-free-memory

Chris Fremgen
  • 4,649
  • 1
  • 26
  • 26