10

I made a small C++ program that copies a small 2.7KB png file 1 million times. This is how my memory looks before the copy: enter image description here

And this is how it looks after the copy program finished: enter image description here

I waited a few minutes after the copy program closed, maybe the memory usage will go down, but it went down only 500MB.

This is what RamMap is showing: enter image description here

If I run the copy program a few times more, then the RAM usage goes even higher, but won't go up past this point: enter image description here

What is happening here? Why is the memory usage so high after the copy program exited?

Here is the copy program code:

//called with loopSize=1'000'000
void TestFileCopy(const string& sourcePath, const string& destFolder, int loopSize)
{
    int counter = 0;

    while (counter < loopSize)
    {
        std::ifstream source(sourcePath, std::ios::binary);
        if (source.fail())
            throw std::runtime_error("File not found: " + sourcePath);

        string destPath = destFolder + std::to_string(counter) + ".png";
        std::ofstream dest(destPath, std::ios::binary);
        dest << source.rdbuf();
        source.close();
        dest.close();

        if (source.fail())
            throw std::runtime_error("Error reading file: " + sourcePath);

        if (dest.fail())
            throw std::runtime_error("Error writing file: " + destPath);

        ++counter;
    } 
}

And if I delete the created files, then the memory usage goes down: enter image description here

0

1 Answer 1

11

I think it's because the files are cached by the OS. Once you delete the files, the cache is no longer required. And the memory can be recovered.

2
  • 2
    I am very surprised to learn that Task Manager shows "cached files" as "in-use" memory. This is very different from Linux where "cached" is counted separately and is often included in the total "available" amount.
    – iBug
    Dec 3, 2023 at 18:51
  • @iBug The pages containing cached file data aren't counted as in use (they're "standby", not "active", in RamMap), but metadata in kernel heaps ("pool") about those pages probably is counted, and with 1 million small cached files it adds up. I suspect it's the same in Linux.
    – benrg
    Dec 18, 2023 at 23:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .