转载自 http://blog.csdn.net/calculatorw/article/details/8192305
BOOL WINAPI GetProcessMemoryInfo( _In_ HANDLE Process, _Out_ PPROCESS_MEMORY_COUNTERS ppsmemCounters, _In_ DWORD cb );
Parameters
-
Process [in]
-
A handle to the process. The handle must have the PROCESS_QUERY_INFORMATIONorPROCESS_QUERY_LIMITED_INFORMATION access right and the PROCESS_VM_READ access right. For more information, seeProcess Security and Access Rights.
Windows Server 2003 and Windows XP: The handle must have the PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights.
ppsmemCounters [out]
-
A pointer to the PROCESS_MEMORY_COUNTERS orPROCESS_MEMORY_COUNTERS_EXstructure that receives information about the memory usage of the process.
cb [in]
-
The size of the ppsmemCounters structure, in bytes.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, callGetLastError.
Remarks
Starting with Windows 7 and Windows Server 2008 R2, Psapi.h establishes version numbers for the PSAPI functions. The PSAPI version number affects the name used to call the function and the library that a program must load.
If PSAPI_VERSION is 2 or greater, this function is defined asK32GetProcessMemoryInfo in Psapi.h and exported in Kernel32.lib and Kernel32.dll. IfPSAPI_VERSION is 1, this function is defined asGetProcessMemoryInfo in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that callsK32GetProcessMemoryInfo.
Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function asGetProcessMemoryInfo. To ensure correct resolution of symbols, add Psapi.lib to theTARGETLIBS macro and compile the program with -DPSAPI_VERSION=1. To use run-time dynamic linking, load Psapi.dll.
Examples
For an example, see Collecting Memory Usage Information for a Process.
Requirements
Minimum supported client | Windows XP [desktop apps only] |
---|---|
Minimum supported server | Windows Server 2003 [desktop apps only] |
Header |
|
Library |
|
DLL |
|
使用函数GetProcessMemoryInfo获取程序当前内存使用量的步骤如下:
1.在程序中添加#pragma comment(lib,"Psapi.lib"),将Psapi.lib包含进去,或者通过在工程的属性中添加Psapi.lib;
2.在系统中包含Psapi.h文件;
3.在需要查看内存的代码处添加如下代码:
HANDLE handle = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
printf("%d\r\n",pmc.WorkingSetSize);
其中pmc.WorkingSetSize就是程序在当前代码处的内存使用量。