相关疑难解决方法(0)

为什么free()不会真正释放内存?

我正在做一些分配和释放内存的测试.这是我正在使用的代码:

#include <stdlib.h>
#include <stdio.h>

#define WAVE_SIZE 100000000

int main(int argc,char* argv[]){

    int i;
    int **p;

    printf("%d allocs...\n",WAVE_SIZE);

    // Malloc
    printf("Allocating memory...\n");
    p = (int**)malloc(WAVE_SIZE*sizeof(int*));
    for(i = 0;i < WAVE_SIZE;i++)
            p[i] = (int*)malloc(sizeof(int));

    // Break
    printf("Press a key to continue...\n");
    scanf("%*s");

    // Dealloc
    printf("Deallocating memory...\n");
    for(i = 0;i < WAVE_SIZE;i++)
            free(p[i]);             
    free(p);

    // Break
    printf("Press a key to continue...\n");
    scanf("%*s");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在休息期间,我检查过程使用的总内存,我看不到我的期望.

直到第一次暂停,我看到内存消耗增加.但是,在第二次暂停时我没有看到它被释放.

这是OS的事吗?如果我的机器负载很高,我没有空闲内存而另一个进程尝试分配会发生什么?

c malloc free

3
推荐指数
2
解决办法
1242
查看次数

标签 统计

c ×1

free ×1

malloc ×1