GetProcessMemoryInfo

来源:互联网 发布:淘宝总销售额查询 编辑:程序博客网 时间:2024/05/16 19:39

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;}
0 0
原创粉丝点击