[EDITED: inclusion of program runtime, file size]
For windows only: here is some code that you can use to get what you want. This implementation only returns PeakWorkingSize, but I included a commented-out copy of the structure containing all the values you can get, with minor changes. This will compile and build in ANSI C if you enable psapi.lib (part of the Windows SDK installation, free download here )
#include <windows.h> #include <ansi_c.h> #include <psapi.h> time_t GetMemUsage(void); int main(int argc, char *argv[]) { DWORD start, elapsed; // for program execution time size_t memory; //for cpu usage; DWORD filesize=0; //for exe file size FILE *fp; char buf[260]; int i; start = GetTickCount(); sprintf(buf, ".\\%s", argv[0]); fp = fopen(buf, "r"); filesize = GetFileSize(fp, NULL); for(i=0;i<1000000;i++); //so ticks will be more than zero memory = GetMemUsage(); fclose(fp); elapsed = GetTickCount() - start; //note, possible rollover, return 0; } time_t GetMemUsage(void) { HANDLE hProcess; PROCESS_MEMORY_COUNTERS pmc; DWORD processID = GetCurrentProcessId(); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)); CloseHandle(hProcess); // typedef struct _PROCESS_MEMORY_COUNTERS { // DWORD cb; // DWORD PageFaultCount; // SIZE_T PeakWorkingSetSize; // SIZE_T WorkingSetSize; // SIZE_T QuotaPeakPagedPoolUsage; // SIZE_T QuotaPagedPoolUsage; // SIZE_T QuotaPeakNonPagedPoolUsage; // SIZE_T QuotaNonPagedPoolUsage; // SIZE_T PagefileUsage; // SIZE_T PeakPagefileUsage; // } PROCESS_MEMORY_COUNTERS; typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS; return pmc.PeakWorkingSetSize; }
source share