我正在做一些测试分配和释放内存。这是我正在使用的代码:为什么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;
}
在休息期间我检查过程中使用的总内存,我没有看到我的预期。
直到第一次暂停,我看到内存消耗增加。但是,第二次暂停时,我没有看到它被释放。
这是一个操作系统的东西?如果我的机器负载很高,会发生什么情况,我没有可用内存,另一个进程尝试分配?