小编kov*_*and的帖子

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

我制作了一个小型 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()) …
Run Code Online (Sandbox Code Playgroud)

memory windows windows-10

10
推荐指数
1
解决办法
1193
查看次数

标签 统计

memory ×1

windows ×1

windows-10 ×1