代码之家  ›  专栏  ›  技术社区  ›  rplusg

使用“空闲”时内存使用率没有下降吗?

  •  2
  • rplusg  · 技术社区  · 16 年前

    不知怎的,这个电话 free() 它不起作用。我在Windows上运行此应用程序,并在任务管理器中跟踪内存使用情况,但在调用 免费的 .

    int main(int argc, char *argv[])
    {
        int i=0;
        int *ptr;
    
        ptr = (int*) malloc(sizeof(int) * 1000);
    
        for (i=0; i < 1000; i++)
        {
            ptr[i] = 0;
        }
    
        free(ptr); // After this call, the program memory usage doesn't decrease
    
        system("PAUSE");
    
        return 0;
    }
    
    3 回复  |  直到 13 年前
        1
  •  13
  •   Thomas Padron-McCarthy    16 年前

    典型的C实现不会将空闲:d内存返回到操作系统。它可供同一程序使用,但不可供其他程序使用。

        2
  •  2
  •   Ponting    16 年前

    free 内存将返回到操作系统。通常,CRT实现有一些优化,因为它们可能不会立即返回此内存。这允许CRT以更快的方式分配后续内存分配请求。

        3
  •  1
  •   Viliam    16 年前

    请注意,任务管理器将显示libc从系统“借用”的内存。但并不是所有的malloc都会通过libc进入操作系统,同样,也不是所有空闲的malloc都会释放系统内存。