[英]C malloc, memory usage only when populating
當我的應用程序啟動時,我正在為malloc分配一些空間。 如果我不填充此變量top顯示此應用程序使用的內存的0%,但如果我開始填充此變量top開始顯示ram的使用量增加我填充此數組的方式。
所以我的問題是:不應該將malloc分配的空間顯示為我的應用程序的已用空間嗎? 為什么當我填充此變量時,它僅顯示我的應用程序的RAM使用量增加?
我在Ubuntu 10.10 64位。 以下是填充它的代碼:
char pack(uint64_t list, char bits, uint64_t *list_compressed, char control, uint64_t *index){
uint64_t a, rest;
if(control == 0){
a = list;
}
else{
rest = list >> (64 - control);
a = (control == 64 ? list_compressed[*index] : list_compressed[*index] + (list << control));
if(control + bits >= 64){
control = control - 64;
//list_compressed[*index] = a;
(*index)++;
a = rest;
}
}
//list_compressed[*index] = a;
control = control + bits;
return control;
}
“malloqued”變量是list_compressed。
如果我取消注釋list_compressed填充,則ram使用率會增加,如果我保持注釋,則使用率為0%。
簡短的回答,沒有。 在許多操作系統上,當您調用malloc時,它不會直接為您分配內存,而是僅在您訪問它時。
來自malloc
手冊頁 :
默認情況下,Linux遵循樂觀的內存分配策略。 這意味着當malloc()返回非NULL時,無法保證內存確實可用。
現代操作系統可能只在您分配時返回虛擬內存地址,而實際上並不指向內存塊。 只有在您想要使用它時才會“分配”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.