Hi everyone,
I have to evaluate the performance of an algorithm (implemented as a C++ class), in terms of execution times and memory usage. I can easily handle the execution times, however I've got a few problems with the memory used by the class.
Let's suppose I have a getRAMUsage() function, which returns the amount of resident memory used by the whole program. Since there are also other components used in the program, and some of them need a non-negligible amount of memory, my problem is to find out how much memory ONLY MY COMPONENT needs.
So, here's the structure of my program so far:
Code:
// Initialize my component, and see how much more memory is allocated
long allocation_ram = getRAMUsage();
MyComponent *component = new MyComponent();
allocation_ram = getRAMUsage() - allocation_ram;
long total_ram_used = allocation_ram;
// Enter the processing loop
while(!stop_condition)
{
// Do some stuff...
// Run component and see how much more memory it allocates at each cycle
long cycle_ram = getRAMUsage();
component->doSomething();
cycle_ram = getRAMUsage() - cycle_ram;
total_ram_used += allocation_ram;
// Do some other stuff...
}
So, in the end, total_ram_used should indicate the amount of RAM allocated by my class during execution.
In my case, cycle_ram should be 0 all the time, since it allocates some memory, works with it, and frees it when it's done. This basically works (I mean the computation of the RAM usage by the component), but sometimes cycle_ram is a few MB, although the total RAM usage by the program (checked with 'top') doesn't change.
My explanation to this is that the runtime library might deallocate some memory allocated by doSomething() after the second call to getRAMUsage(). Does this make sense? Or maybe my approach is completely wrong? :-)
Thanks for your help!