Windows system cache and huge files

I have a problem with the Windows system cache. Sample code:

int main(int argc, char* argv[])
{
    HANDLE file_ = INVALID_HANDLE_VALUE;
    file_ = CreateFile(
                            "test_file.txt",
                            GENERIC_WRITE, 
                            FILE_SHARE_READ, 
                            0, 
                            OPEN_ALWAYS, 
                            FILE_FLAG_SEQUENTIAL_SCAN, 
                            NULL
                            );
    if (file_ == INVALID_HANDLE_VALUE || file_ == NULL)
    {
        std::cout << "CreateFile error " << GetLastError() << std::endl;
        return GetLastError();
    }
    int counter = 0;
    DWORD io_bytes = 0;
    while(true)
    {
        char buffer[0x1000];
        int len = _snprintf_s(buffer, 0x1000, 0xffff, "test message %d\r\n", counter);
        counter++;
        if ( !WriteFile(file_, buffer, len, &io_bytes, NULL) )
        {
            std::cout << "WriteFile error " << GetLastError() << std::endl;
            return GetLastError();
        }
        if (counter > 10000000)
        {
            system("pause");
            return 0;
        }
    }
}

      

if you run this code and see the size of the system cache everything will be fine. But if you open this file (test_file.txt) for reading in some kind of view (for example, with the lister plugin for the full commander) while this program is running, you will see the size of the system cache grows even if you already close your viewer program. It looks like a memory leak. Is this normal behavior?

+1


source to share


3 answers


The Windows cache will try to store as many files as possible in RAM. On Windows NT, loading a very large file may cause an error. If the file doesn't fit into the cache, it will no longer be released to push everything else from RAM into the paging file until your machine is constantly changing.

But your case is simpler. Opening the file in the viewer will load the file into the cache. Since you are still writing it, the cache entry will grow accordingly. Now you exit the viewer, but you are still writing. What is Windows supposed to do? Apparently it stores the file in the cache.



It is not a memory leak if the cache is never compressed. Does it eliminate a) when you run out of RAM or b) when you close a file while writing?

+3


source


When my system runs out of RAM cache it doesn't shrink, the system dumps everything into the swap file. When I close the file while writing, the system cache is compressed.



0


source


If I were you, I would try to understand better what constitutes the "system cache" number that the Task Manager displays.

I suspect this is not what you are thinking, and your use of the phrase like "the system swaps everything to the swap file" especially makes me wonder if you are trying to transfer the experience from another system to Windows.

Mark Russonovich's books are considered the best reference for this kind of material.

0


source







All Articles