C++ 如何得到当前进程所占用的内存呢?【转】
转载
使用SDK的PSAPI (Process Status Helper)
PPROCESS_MEMORY_COUNTERS ppsmemCounters,
struct _PROCESS_MEMORY_COUNTERS {
SIZE_T PeakWorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS,
*PPROCESS_MEMORY_COUNTERS;
#pragma comment(lib,"psapi.lib")
void showMemoryInfo(void)
HANDLE handle=GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle,&pmc,sizeof(pmc));
cout<<"内存使用:"<<pmc.WorkingSetSize/1000 <<"K/"<<pmc.PeakWorkingSetSize/1000<<"K + "<<pmc.PagefileUsage/1000 <<"K/"<<pmc.PeakPagefileUsage/1000 <<"K"<<endl;
int main(int argc,char* argv)
cout<<"回收所有可回收的内存"<<endl;
EmptyWorkingSet(GetCurrentProcess());
for(int i=0;i<sizeof(buf)/sizeof(char*);i++)
for(int i=0;i<sizeof(buf)/sizeof(char*);i++)
cout<<"回收所有可回收的内存"<<endl;
EmptyWorkingSet(GetCurrentProcess());
内存使用:430K/1425K + 466K/466K
内存使用:446K/1425K + 679K/679K
内存使用:462K/1425K + 892K/892K
内存使用:454K/1425K + 692K/892K
内存使用:438K/1425K + 487K/892K
内存使用:110K/1425K + 360K/892K
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。