I would like to get the total memory allocated before and after I call a function in order to determine if I have freed everything correctly or not.
I'm doing this in C and I'm very rusty so forgive me if this is a naive question. I'm looking for something similar to the C# GC.GetTotalMemory(true) and this is in Windows for now.
Right now I am using PROCESS_MEMORY_COUNTERS_EX
and GetProcessMemoryInfo(...)
, before and after calling the function but I can't make heads or tails of the output because if I go into the function and comment out a call to free(...)
then it will give me the same results (after is always larger). Here is what I have right now...
GetProcessMemoryInfo(hProc, &before, sizeof(before));
r = c->function();
GetProcessMemoryInfo(hProc, &after, sizeof(after));
if(r->result != 0) {
printf("error: %s\r\n", c->name);
printf(" %s\r\n", r->message);
printf(" %s (%d)\r\n", r->file, r->line);
failed++;
}
else if(after.PrivateUsage > before.PrivateUsage) {
printf("memory leak: %s\r\n", c->name);
printf(" %d kb\r\n", after.PrivateUsage - before.PrivateUsage);
failed++;
}
else succeeded++;
With a result like this:
after.PrivateUsage - before.PrivateUsage = 12288
If I go and comment out some calls to free I get the same result. How can I actually determine the current total size of memory that I have allocated using malloc?