這裏是在窗口的簡單d /探戈代碼:malloc和免費的D /探戈不釋放內存?
module d_test.d;
import tango.util.log.Trace;
import tango.core.Thread;
import tango.stdc.stdlib : malloc, free;
void main() {
Trace.formatln("Checking in...");
Thread.sleep(10);
int total_n = (100 * 1000 * 1000)/int.sizeof; // fill mem with 100MB of ints
int* armageddon = cast(int*)malloc(total_n * int.sizeof);
for(int i = 0; i < total_n; ++i) {
armageddon[i] = 5;
}
Trace.formatln("Checking in...");
Thread.sleep(10);
free(armageddon);
armageddon = null;
Trace.formatln("Checking in...");
Thread.sleep(10);
}
當我運行該程序時,存儲器保持爲低〜2MB,當我分配100MB的陣列的指針存儲器使用跳轉到〜100MB,這很好。但是,在空閒內存仍然是(我在看任務管理器)100MB到程序結束。
我想這可能是到Windows頁面文件緩存或東西,所以我已經嘗試了簡單的C++程序:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
Cout << "Checking in..." <<< endl;
Sleep(10000);
int total_n = (100 * 1000 * 1000)/sizeof(int);
int* armageddon = (int*)malloc(total_n * sizeof(int));
for(int i = 0; i < total_n; ++i) {
armageddon[i] = 5;
}
Cout << "Checking in..." <<< endl;
Sleep(10000);
free(armageddon);
armageddon = NULL;
Cout << "Checking in..." <<< endl;
Sleep(10000);
return 0;
}
我和G ++編譯它,一切似乎工作像它應該。當程序啓動 - 內存使用〜900kb,分配後〜100MB,免費後〜1,2MB ...
那麼,我做錯了什麼或這是一個錯誤?
malloc和free是C調用的說唱歌手。我不知道爲什麼結果會有所不同。 – 2009-08-14 19:08:59
喲喲! //我的名字是malloc,我是一個壞蛋說唱歌手。 //給我所有喲'mem'ry,然後我會把它扔進垃圾箱 – Baxissimo 2009-08-14 21:36:08