I want record the memory usage of my program. Here is a example.
#include <stdio.h>
#include <windows.h>
#include <psapi.h>
void getMemInfo(){
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
printf("%ld, %ldn", virtualMemUsedByMe, physMemUsedByMe);
}
int main()
{
double a[1000];
getMemInfo();
double *b = malloc(sizeof(double)*1000);
getMemInfo();
free(b);
getMemInfo();
return 0;
}
I compile it and run it three times;
Well. Now I have few questions.
- Why does the usage of my program [or my program output] change every time? Did I use the wrong parameters of
pmc
? - How to record the memory usage of c program in time before and after allocating new memory?
- I want to optimize a c project. So I should use which parameter of
pmc
as a benchmark?
Refer:
- Collecting Memory Usage Information For a Process
- c++ – How to determine CPU and memory consumption from inside a process – Stack Overflow
4