C++得到当前进程所占用的内存

血红的双手。 提交于 2020-01-22 12:05:21
//使用SDK的PSAPI (Process Status Helper)
//中的
//BOOL GetProcessMemoryInfo(
//  HANDLE Process,
//  PPROCESS_MEMORY_COUNTERS ppsmemCounters,
//  DWORD cb
//);
//
//typedef struct _PROCESS_MEMORY_COUNTERS {
//  DWORD cb;
//  DWORD PageFaultCount;
//  SIZE_T PeakWorkingSetSize; //峰值内存使用
//  SIZE_T WorkingSetSize; //内存使用
//  SIZE_T QuotaPeakPagedPoolUsage;
//  SIZE_T QuotaPagedPoolUsage;
//  SIZE_T QuotaPeakNonPagedPoolUsage;
//  SIZE_T QuotaNonPagedPoolUsage;
//  SIZE_T PagefileUsage; //虚拟内存使用
//  SIZE_T PeakPagefileUsage; //峰值虚拟内存使用
//} PROCESS_MEMORY_COUNTERS, 
//*PPROCESS_MEMORY_COUNTERS;



#include <iostream>
#include <windows.h>
#include <psapi.h>
#pragma comment(lib,"psapi.lib")
using namespace std;
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)
    {
    showMemoryInfo();
    cout<<"回收所有可回收的内存"<<endl;
    EmptyWorkingSet(GetCurrentProcess());
    showMemoryInfo();
    cout<<"开始动态分配内存"<<endl;
    char* buf[5];
    for(int i=0;i<sizeof(buf)/sizeof(char*);i++)
        {
        buf[i]=new char[102400];
        showMemoryInfo();
        }
    cout<<"开始释放内存"<<endl;
    for(int i=0;i<sizeof(buf)/sizeof(char*);i++)
        {
        delete buf[i];
        buf[i]=NULL;
        showMemoryInfo();
        }
    cout<<"回收所有可回收的内存"<<endl;
    EmptyWorkingSet(GetCurrentProcess());
    showMemoryInfo();
    return 0;
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!