2008-11-11 84 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不幸具有混合的目的:它提供系统范围和特定于过程的信息,例如,虚拟内存信息。

相关问题