我正在做一些測試分配和釋放內存。這是我正在使用的代碼:爲什麼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;
}
在休息期間我檢查過程中使用的總內存,我沒有看到我的預期。
直到第一次暫停,我看到內存消耗增加。但是,第二次暫停時,我沒有看到它被釋放。
這是一個操作系統的東西?如果我的機器負載很高,會發生什麼情況,我沒有可用內存,另一個進程嘗試分配?