GetProcessMemoryInfo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bainaqianhong/article/details/51596288

GetProcessMemoryInfo

#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/1024 <<"/"<<pmc.PeakWorkingSetSize/1024<<" + "<<pmc.PagefileUsage/1024 <<"/"<<pmc.PeakPagefileUsage/1024 <<""<<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;
}





#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

int main(int argc,char **argv)
{
    HANDLE handle;
    handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if(handle == INVALID_HANDLE_VALUE)
    {
        printf("Gain snapshot falid\n");
        return -1;
    }

    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(pe);
    BOOL flag = Process32First(handle,&pe);
    while(flag){
        char buff[MAX_PATH];
        sprintf(buff,"%i---------%s",pe.th32ProcessID,pe.szExeFile);
        printf("%s\n",buff);
        flag = Process32Next(handle,&pe);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/bainaqianhong/article/details/51596288