I am interested in monitoring memory usage for a process, much like in the Task Manager, given the process ID on windwos platform. After reading this question and answers and reading up on the PROCESS_MEMORY_COUNTERS I was able to come up with the following:
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include <iostream>
SIZE_T getMemoryUsage(DWORD Process_PID) {
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
PROCESS_MEMORY_COUNTERS PMC;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |PROCESS_VM_READ, FALSE, Process_PID);
GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS *)&PMC, sizeof(PMC));
SIZE_T physicalMemUsedByProc = PMC.WorkingSetSize;
return physicalMemUsedByProc;
}
However, it only measures Working Set, while what is shown in Task Manager as memory is Private Working set. My question is how can I obtain the Private Working set value if I have Working Set one? Is using PROCESS_MEMORY_COUTNERS a good approach, or should I consider something else for this problem?