2008-11-11 87 views

回答

43

一個好的起點是GetProcessMemoryInfo,它報告有關指定進程的各種內存信息。您可以通過GetCurrentProcess()作爲進程句柄來獲取有關調用進程的信息。

PROCESS_MEMORY_COUNTERSWorkingSetSize成員可能與任務管理器中的Mem Usage coulmn最接近,但它不會完全相同。我會嘗試不同的價值觀來找到最接近您需求的價值觀。

8

GetProcessMemoryInfo是你正在尋找的功能。 MSDN上的文檔將爲您指出正確的方向。從您傳入的PROCESS_MEMORY_COUNTERS結構中獲取想要的信息。

您需要包含psapi.h。

17

我想這就是你要找的人:

#include<windows.h> 
#include<stdio.h> 
#include<tchar.h> 

// Use to convert bytes to MB 
#define DIV 1048576 

// Use to convert bytes to MB 
//#define DIV 1024 

// Specify the width of the field in which to print the numbers. 
// The asterisk in the format specifier "%*I64d" takes an integer 
// argument and uses it to pad and right justify the number. 

#define WIDTH 7 

void _tmain() 
{ 
    MEMORYSTATUSEX statex; 

    statex.dwLength = sizeof (statex); 

    GlobalMemoryStatusEx (&statex); 


    _tprintf (TEXT("There is %*ld percent of memory in use.\n"),WIDTH, statex.dwMemoryLoad); 
    _tprintf (TEXT("There are %*I64d total Mbytes of physical memory.\n"),WIDTH,statex.ullTotalPhys/DIV); 
    _tprintf (TEXT("There are %*I64d free Mbytes of physical memory.\n"),WIDTH, statex.ullAvailPhys/DIV); 
    _tprintf (TEXT("There are %*I64d total Mbytes of paging file.\n"),WIDTH, statex.ullTotalPageFile/DIV); 
    _tprintf (TEXT("There are %*I64d free Mbytes of paging file.\n"),WIDTH, statex.ullAvailPageFile/DIV); 
    _tprintf (TEXT("There are %*I64d total Mbytes of virtual memory.\n"),WIDTH, statex.ullTotalVirtual/DIV); 
    _tprintf (TEXT("There are %*I64d free Mbytes of virtual memory.\n"),WIDTH, statex.ullAvailVirtual/DIV); 
    _tprintf (TEXT("There are %*I64d free Mbytes of extended memory.\n"),WIDTH, statex.ullAvailExtendedVirtual/DIV); 


} 
+2

這可能不是他想知道的,因爲這是測量系統使用的內存,而不是個別進程所消耗的內存。然而,知道也是有用的,所以我不會低估它。 – CashCow 2012-09-21 11:25:46

1

,以補充羅寧的回答,indead功能GlobalMemoryStatusEx給你正確的計數器導出虛擬內存使用調用進程:剛。減去ullAvailVirtualullTotalVirtual獲得分配的虛擬內存。你可以使用ProcessExplorer或其他的東西來檢查你的自我。

令人困惑的是,系統調用GlobalMemoryStatusEx不幸具有混合的目的:它提供系統範圍和特定於過程的信息,例如,虛擬內存信息。

相關問題