复制100万个小文件后内存占用过高(Win10 x64)

kov*_*and 10 memory windows windows-10

我制作了一个小型 C++ 程序,将一个 2.7KB 的小 png 文件复制 100 万次。这是我的记忆在副本之前的样子: 在此输入图像描述

这是复制程序完成后的样子: 在此输入图像描述

复制程序关闭后我等了几分钟,也许内存使用量会下降,但只下降了500MB。

这是 RamMap 显示的内容: 在此输入图像描述

如果我再运行复制程序几次,那么 RAM 使用率会更高,但不会超过此点: 在此输入图像描述

这里发生了什么?为什么copy程序退出后内存占用这么高?

这是复制程序代码:

//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;
    } 
}
Run Code Online (Sandbox Code Playgroud)

如果我删除创建的文件,那么内存使用量就会下降: 在此输入图像描述

Roh*_*pta 11

我认为这是因为文件被操作系统缓存了。删除文件后,就不再需要缓存。而且记忆是可以恢复的。

  • 我很惊讶地发现任务管理器将“缓存文件”显示为“使用中”内存。这与 Linux 非常不同,在 Linux 中,“缓存”是单独计算的,并且通常包含在“可用”总量中。 (2认同)