#include <time.h>#include <windows.h>#include <iostream>#define TOM(byteValue) (byteValue/1024/1024)using namespace std;int main(){ time_t nowTime; unsigned short FreeMem = 0; MEMORYSTATUS MemStat; while(1) { MemStat.dwLength = sizeof(); GlobalMemoryStatus(&MemStat); FreeMem = TOM(MemStat.dwAvailPhys); time(&nowTime); cout << ctime(&nowTime); cout << "Current Free Mem: " << FreeMem << "M" << endl; if (FreeMem < 500) cout << "execute your program here" << endl; Sleep(2000);/* cout << MemStat.dwMemoryLoad << endl; cout << "percent of memory in use:" << TOM(MemStat.dwMemoryLoad) << endl; cout << "bytes of physical memory:" << TOM(MemStat.dwTotalPhys) << endl; cout << "free physical memory bytes:" << TOM(MemStat.dwAvailPhys) << endl; cout << "bytes of paging file:" << MemStat.dwTotalPageFile << endl; cout << "free bytes of paging file:" << MemStat.dwAvailPageFile << endl; cout << "user bytes of address space:" << MemStat.dwTotalVirtual << endl; cout << "free user bytes:" << MemStat.dwAvailVirtual << endl;*/ }
return 0;}
关于MEMORYSTATUS结构体信息,请参考MSDN