利用GetProcessMemoryInfo 获取进程内存使用
2018-02-28 17:54
549 查看
GetProcessMemoryInfo function
Retrieves information about the memory usage of the specified process.Syntax
C++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_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right and the PROCESS_VM_READ access right. For more information, see Process 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 or PROCESS_MEMORY_COUNTERS_EX structure 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, call GetLastError.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 as K32GetProcessMemoryInfo in Psapi.h and exported in Kernel32.lib and Kernel32.dll. If PSAPI_VERSION is 1, this function is defined as GetProcessMemoryInfo 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 as GetProcessMemoryInfo. To ensure correct resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program with -DPSAPI_VERSION=1. To use run-time dynamic linking, load Psapi.dll.Examples
To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo function to obtain information about the memory usage of a process.C++#include <windows.h> #include <stdio.h> #include <psapi.h> // To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS // and compile with -DPSAPI_VERSION=1 void PrintMemoryInfo( DWORD processID ) { HANDLE hProcess; PROCESS_MEMORY_COUNTERS pmc; // Print the process identifier. printf( "\nProcess ID: %u\n", processID ); // Print information about the memory usage of the process. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); if (NULL == hProcess) return; if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) { printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount ); printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize ); printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize ); printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage ); printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage ); printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage ); printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage ); printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage ); } CloseHandle( hProcess ); } int main( void ) { // Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) { return 1; } // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the memory usage for each process for ( i = 0; i < cProcesses; i++ ) { PrintMemoryInfo( aProcesses[i] ); } return 0; }The main function obtains a list of processes by using the EnumProcesses function. For each process, main calls the PrintMemoryInfo function, passing the process identifier. PrintMemoryInfo in turn calls the OpenProcess function to obtain the process handle. If OpenProcess fails, the output shows only the process identifier. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Finally, PrintMemoryInfo calls the GetProcessMemoryInfo function to obtain the memory usage information.
相关文章推荐
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 利用GetProcessMemoryInfo 获取进程内存使用
- 获取对应进程的内存使用情况(GetProcessMemoryInfo)
- 查看进程所用的内存(使用GetWindowThreadProcessId取得进程ID,OpenProcess打开进程和GetProcessMemoryInfo取得内存信息)
- cocos2d-x获取程序当前内存使用量 GetProcessMemoryInfo 函数
- 获取进程内存信息 GetProcessMemoryInfo (p/invoke)
- GetProcessMemoryInfo API取得进程所用的内存
- 用GetProcessMemoryInfo 函数 获取内存
- 通过性能计数器(Process)来获取某个进程的CPU使用率、内存使用量、磁盘读写速率
- [转]在vs2008上编译获取cpu代码出现的错误- error LNK2019: 无法解析的外部符号 _GetProcessMemoryInfo@12
- 使用ProcessInfo获取进程信息
- get_user_pages — 获取用户区进程使用内存的某个页
- 获取信息的有关Windows API(最有意思是OpenProcess和GetProcessMemoryInfo)
- 利用java获取计算机cpu利用率和内存使用信息