c - GetProcessMemoryInfo PROCESS_MEMORY_COUNTERS_EX.PrivateUsage always 0 -
i'm using getprocessmemoryinfo function determine process memory usage pid.
with regular process_memory_counters works fine, need privateusage member, exists in extended structure process_memory_counters_ex.
there couple of docs, moved me forcefully cast extended type basic one, otherwise sample wont compile.
i'm still able value basic members, such peakworkingsetsize, privateusage 0. i've tried redefine psapi_version - still nothing. program can't compiled psapi_version < 2.
here example.
#include <windows.h> #include <stdio.h> #include <tchar.h> #include <psapi.h> void _tmain (int argc, tchar *argv[]) { // use first argument pid dword processid = strtol(argv[1],0, 0); handle hprocess = openprocess( process_query_information | process_vm_read | synchronize, false, processid); process_memory_counters_ex pmc; zeromemory(&pmc, sizeof(process_memory_counters_ex)); // wait until process dead waitforsingleobject( hprocess , infinite ); getprocessmemoryinfo( hprocess, (process_memory_counters*)&pmc, sizeof(pmc) ); fprintf(stdout, " peakworkingsetsize : %d\n", pmc.peakworkingsetsize); fprintf(stdout, " privateusage : %d\n", pmc.privateusage); closehandle(hprocess); } i execute notepad.exe, put pid program above, , after close notepad , results privateusage 0 =( :
c:\utils>simple.exe 45656 peakworkingsetsize : 6377472 privateusage : 0 c:\utils> any suggestions why happening?
c:\utils>cl --version microsoft (r) c/c++ optimizing compiler version 17.00.61030 x86 copyright (c) microsoft corporation. rights reserved. running on win7x64.
i know pretty old question , won't need answer. close. asking private set memory after process have been closed, no memory process no longer exists. hence, private usage 0.
instead in best opinion, should have requested private set memory after fixed interval of time until process terminate. if keep interval low 1 millisecond have got near end memory of process.
example:
process_memory_counters_ex pmc; zeromemory(&pmc, sizeof(process_memory_counters_ex)); //do every millisecond until process terminates { zeromemory(&pmc, sizeof(process_memory_counters_ex)); getprocessmemoryinfo( hprocess, (process_memory_counters*)&pmc, sizeof(pmc) ); }while(waitforsingleobject( hprocess , 1)); // wait until process dead // waitforsingleobject( hprocess , infinite ); // getprocessmemoryinfo( hprocess, (process_memory_counters*)&pmc, sizeof(pmc) ); fprintf(stdout, " peakworkingsetsize : %d\n", pmc.peakworkingsetsize); fprintf(stdout, " privateusage (bytes): %d\n", pmc.privateusage); fprintf(stdout, " privateusage (kb) : %f\n",(float)pmc.privateusage/1024.0); closehandle(hprocess); after carrying out above changes. following output obtained
c:\>pidmemory.exe 3456 peakworkingsetsize : 12427264 privateusage (bytes): 2269184 privateusage (kb) : 2216.000000 this might sometime give 0 because in cases process has been terminated after while condition check. thus, giving 0. decent work around keep history of privateusage.
example,
int history=0; { zeromemory(&pmc, sizeof(process_memory_counters_ex)); getprocessmemoryinfo( hprocess, (process_memory_counters*)&pmc, sizeof(pmc) ); if(pmc.privateusage != 0) history = pmc.privateusage; }while(waitforsingleobject( hprocess , 1)); i hope have been useful.
Comments
Post a Comment